View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.coap;
21  
22  import java.util.Arrays;
23  
24  /**
25   * A CoAP message option.
26   * 
27   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
28   */
29  public class CoapOption {
30  
31      private final CoapOptionType type;
32  
33      private final byte[] data;
34  
35      /**
36       * Create a CoAP option
37       * 
38       * @param type the type of the option
39       * @param data the content of the option
40       */
41      public CoapOption(CoapOptionType type, byte[] data) {
42          super();
43          this.type = type;
44          this.data = Arrays.copyOf(data, data.length);
45      }
46  
47      public CoapOptionType getType() {
48          return type;
49      }
50  
51      public byte[] getData() {
52          return data;
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public String toString() {
60          StringBuilder builder = new StringBuilder();
61          builder.append("CoapOption [type=").append(type).append(", data=").append(Arrays.toString(data)).append("]");
62          return builder.toString();
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public int hashCode() {
70          final int prime = 31;
71          int result = 1;
72          result = prime * result + Arrays.hashCode(data);
73          result = prime * result + ((type == null) ? 0 : type.hashCode());
74          return result;
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public boolean equals(Object obj) {
82          if (this == obj) {
83              return true;
84          }
85  
86          if (obj == null) {
87              return false;
88          }
89  
90          if (getClass() != obj.getClass()) {
91              return false;
92          }
93  
94          CoapOption other = (CoapOption) obj;
95  
96          if (!Arrays.equals(data, other.data)) {
97              return false;
98          }
99  
100         if (type != other.type) {
101             return false;
102         }
103 
104         return true;
105     }
106 }