View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.api.config.domain;
17  
18  import java.io.Serializable;
19  
20  import org.apache.commons.lang.ObjectUtils;
21  
22  import de.smartics.util.lang.Arguments;
23  
24  /**
25   * Stores information of a property.
26   */
27  public class Property implements Serializable, Comparable<Property>
28  {
29    // ********************************* Fields *********************************
30  
31    // --- constants ------------------------------------------------------------
32  
33    /**
34     * The class version identifier.
35     */
36    private static final long serialVersionUID = 1L;
37  
38    // --- members --------------------------------------------------------------
39  
40    /**
41     * The optional source where the property value has been defined. This is an
42     * arbitrary name that defines the origin in its context.
43     *
44     * @serial
45     */
46    private final PropertyLocation source;
47  
48    /**
49     * The name of the property. Must not blank.
50     *
51     * @serial
52     */
53    private final String name;
54  
55    /**
56     * The value of the property. May be <code>null</code>.
57     *
58     * @serial
59     */
60    private final String value;
61  
62    // ****************************** Initializer *******************************
63  
64    // ****************************** Constructors ******************************
65  
66    /**
67     * Default constructor.
68     *
69     * @param source the optional source where the property value has been
70     *          defined.
71     * @param name the name of the property.
72     * @param value the value of the property.
73     * @throws NullPointerException if {@code source} is <code>null</code>.
74     * @throws IllegalArgumentException if {@code name} is blank.
75     */
76    public Property(final PropertyLocation source, final String name,
77        final String value) throws NullPointerException, IllegalArgumentException
78    {
79      Arguments.checkNotNull("source", source);
80      Arguments.checkNotBlank("name", name);
81  
82      this.source = source;
83      this.name = name;
84      this.value = value;
85    }
86  
87    /**
88     * Convenience constructor that translates the value to a String.
89     *
90     * @param source the optional source where the property value has been
91     *          defined.
92     * @param name the name of the property.
93     * @param value the value of the property.
94     * @throws IllegalArgumentException if {@code name} is blank.
95     */
96    public Property(final PropertyLocation source, final String name,
97        final Object value) throws IllegalArgumentException
98    {
99      this(source, name, ObjectUtils.toString(value, null));
100   }
101 
102   /**
103    * Convenience constructor that translates the value to a String.
104    *
105    * @param source the optional source where the property value has been
106    *          defined.
107    * @param name the name of the property.
108    * @param value the value of the property.
109    * @throws IllegalArgumentException if {@code name} is blank.
110    */
111   public Property(final String source, final String name, final Object value)
112     throws IllegalArgumentException
113   {
114     this(new PropertyLocation(source), name, ObjectUtils.toString(value, null));
115   }
116 
117   // ****************************** Inner Classes *****************************
118 
119   // ********************************* Methods ********************************
120 
121   // --- init -----------------------------------------------------------------
122 
123   // --- get&set --------------------------------------------------------------
124 
125   /**
126    * Returns the optional source where the property value has been defined. This
127    * is an arbitrary name that defines the origin in its context.
128    *
129    * @return the optional source where the property value has been defined.
130    */
131   public final PropertyLocation getSource()
132   {
133     return source;
134   }
135 
136   /**
137    * Returns the name of the property. Must not blank.
138    *
139    * @return the name of the property.
140    */
141   public final String getName()
142   {
143     return name;
144   }
145 
146   /**
147    * Returns the value of the property. May be <code>null</code>.
148    *
149    * @return the value of the property.
150    */
151   public final String getValue()
152   {
153     return value;
154   }
155 
156   // --- business -------------------------------------------------------------
157 
158   // --- object basics --------------------------------------------------------
159 
160   /**
161    * {@inheritDoc}
162    *
163    * @see java.lang.Comparable#compareTo(java.lang.Object)
164    */
165   @Override
166   public final int compareTo(final Property o)
167   {
168     int result = name.compareTo(o.name);
169     if (result == 0)
170     {
171       result =
172           ObjectUtils.compare(ObjectUtils.toString(value, null),
173               ObjectUtils.toString(o.value, null));
174       if (result == 0)
175       {
176         result = ObjectUtils.compare(source, o.source);
177       }
178     }
179     return result;
180   }
181 
182   /**
183    * Returns the hash code of the object.
184    *
185    * @return the hash code.
186    */
187   @Override
188   public final int hashCode()
189   {
190     return name.hashCode();
191   }
192 
193   /**
194    * Returns <code>true</code> if the given object is semantically equal to the
195    * given object, <code>false</code> otherwise.
196    *
197    * @param object the instance to compare to.
198    * @return <code>true</code> if the given object is semantically equal to the
199    *         given object, <code>false</code> otherwise.
200    */
201   @Override
202   public final boolean equals(final Object object)
203   {
204     if (this == object)
205     {
206       return true;
207     }
208     else if (object == null || getClass() != object.getClass())
209     {
210       return false;
211     }
212 
213     final Property other = (Property) object;
214 
215     return (name.equals(other.name) && ObjectUtils.equals(value, other.value) && ObjectUtils
216         .equals(source, other.source));
217   }
218 
219   /**
220    * Returns the string representation of the object.
221    *
222    * @return the string representation of the object.
223    */
224   @Override
225   public String toString()
226   {
227     return name + '=' + value + " (" + source + ')';
228   }
229 }