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.event;
19  
20  /**
21   * Class which represents a single attribute of a {@link ContextEvent}. The
22   * attribute is uniquely specified 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       * Creates an 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 (name == null)
45              throw new NullPointerException("Name cannot be null.");
46          if (name.trim().isEmpty())
47              throw new IllegalArgumentException(
48                      "Name cannot be an empty or blank string.");
49          if (value == null)
50              throw new NullPointerException("Value cannot be null.");
51  
52          this.name = name;
53          this.value = value;
54      }
55  
56      /**
57       * Returns the name of this {@link Attribute}.
58       * 
59       * @return the name of this {@link Attribute}.
60       */
61      public String getName() {
62          return this.name;
63      }
64  
65      /**
66       * Returns the value of this {@link Attribute}.
67       * 
68       * @return the value of this {@link Attribute}.
69       */
70      public String getValue() {
71          return this.value;
72      }
73  
74      /**
75       * Returns hash code for this {@link Attribute}. The hash code for an
76       * {@link Attribute} object is computed as:
77       * 
78       * <pre>
79       * name * 31 + value
80       * </pre>
81       * 
82       * using integer arithmetic.
83       * 
84       * @return a hash code value for this {@link Attribute}.
85       */
86      @Override
87      public int hashCode() {
88          return this.name.hashCode() * 31 + this.value.hashCode();
89      }
90  
91      /**
92       * Compares this {@link Attribute} to the specified {@link Object}. The
93       * result is <code>true</code> if and only if the argument is not
94       * <code>null</code> and is an {@link Attribute} object that has same name
95       * and value as this {@link Attribute}.
96       * 
97       * @param obj the {@link Object} to compare this {@link Attribute} against.
98       * @return <code>true</code> if {@link Attribute}s are equal;
99       *         <code>false</code> otherwise.
100      */
101     @Override
102     public boolean equals(final Object obj) {
103         boolean result = false;
104 
105         if (obj != null)
106             if (this == obj)
107                 result = true;
108             else if (obj instanceof Attribute) {
109                 final Attribute other = (Attribute) obj;
110 
111                 result = this.name.equals(other.name)
112                         && this.value.equals(other.value);
113             }
114 
115         return result;
116     }
117 }