View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.util;
19  
20  /**
21   * Representation of a single attribute. The attribute is uniquely specified
22   * with a name and value.
23   * 
24   * @author sanjin
25   */
26  public class Attribute {
27  
28      private final String name;
29      private final String value;
30  
31      /**
32       * Create instance of {@link Attribute} with specified name and value.
33       * 
34       * @param name the name of the {@link Attribute}.
35       * @param value the value for this {@link Attribute}.
36       * @throws NullPointerException if specified name or value is
37       *         <code>null</code>.
38       * @throws IllegalArgumentException if specified name is an empty or blank
39       *         string.
40       */
41      public Attribute(final String name, final String value) {
42          super();
43  
44          if (null == name) {
45              throw new NullPointerException("Name cannot be null.");
46          }
47          if (name.trim().isEmpty()) {
48              throw new IllegalArgumentException(
49                      "Name cannot be an empty or blank string.");
50          }
51          if (null == value) {
52              throw new NullPointerException("Value cannot be null.");
53          }
54  
55          this.name = name;
56          this.value = value;
57      }
58  
59      /**
60       * Returns the name of this {@link Attribute}.
61       * 
62       * @return the name of this {@link Attribute}.
63       */
64      public String getName() {
65          return this.name;
66      }
67  
68      /**
69       * Returns the value of this {@link Attribute}.
70       * 
71       * @return the value of this {@link Attribute}.
72       */
73      public String getValue() {
74          return this.value;
75      }
76  
77      /**
78       * Returns hash code for this {@link Attribute}. The hash code for an
79       * {@link Attribute} object is computed as:
80       * 
81       * <pre>
82       * name * 31 + value
83       * </pre>
84       * 
85       * using integer arithmetic.
86       * 
87       * @return a hash code value for this {@link Attribute}.
88       */
89      @Override
90      public int hashCode() {
91          return (this.name.hashCode() * 31) + this.value.hashCode();
92      }
93  
94      /**
95       * Compares this {@link Attribute} to the specified {@link Object}. The
96       * result is <code>true</code> if and only if the argument is not
97       * <code>null</code> and is an {@link Attribute} object that has same name
98       * as this {@link Attribute}.
99       * 
100      * @param obj the {@link Object} to compare this {@link Attribute} against.
101      * @return <code>true</code> if {@link Attribute}s are equal;
102      *         <code>false</code> otherwise.
103      */
104     @Override
105     public boolean equals(final Object obj) {
106         boolean result = false;
107 
108         if (null != obj) {
109             if (this == obj) {
110                 result = true;
111             } else if (obj instanceof Attribute) {
112                 final Attribute other = (Attribute) obj;
113 
114                 result = this.name.equals(other.name);
115             }
116         }
117 
118         return result;
119     }
120 }