View Javadoc

1   /***
2    * Copyright (c) 2003, 2004, Chess iT
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    * 
8    * - Redistributions of source code must retain the above copyright notice, this
9    *   list of conditions and the following disclaimer.
10   *
11   * - Redistributions in binary form must reproduce the above copyright notice,
12   *   this list of conditions and the following disclaimer in the documentation
13   *   and/or other materials provided with the distribution.
14   *
15   * - Neither the name of Chess iT, nor the names of its contributors may be used 
16   *   to endorse or promote products derived from this software without specific
17   *   prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
29   * POSSIBILITY OF SUCH DAMAGE.
30   * 
31   */
32  package nl.chess.it.util.config;
33  
34  /***
35   * Simple place holder that can be used to display the value of a property. Most properties are read
36   * from a Properties, then the original values and key-name will be stored as well. For hard-coded
37   * properties (i.e., public getMethods that don't use the Properties).
38   * 
39   * @author Guus Bosman (Chess iT)
40   * @version $Revision: 1.1.1.1 $
41   */
42  public class PropertyDisplayItem implements Comparable {
43  
44      /***
45       * Never <code>null</code>.
46       */
47      private final String methodName;
48      private final String originalValue;
49      private final String parseValue;
50      private final String propertyName;
51  
52      /***
53       * Creates a new PropertyDisplayItem object.
54       * 
55       * @param methodName Name of the method. Cannot be <code>null</code>.
56       * @param propertyName Name of the property. Can be <code>null</code>.
57       * @param originalValue Original value of the property. Can be <code>null</code>.
58       * @param parseValue Value as interpreted by Java Config. This is a String representation. Can
59       *            be <code>null</code>.
60       */
61      public PropertyDisplayItem(String methodName, String propertyName, String originalValue, String parseValue) {
62          if (methodName == null) {
63              throw new NullPointerException("methodName cannot be null here.");
64          }
65  
66          this.methodName = methodName;
67          this.propertyName = propertyName;
68          this.originalValue = originalValue;
69          this.parseValue = parseValue;
70      }
71  
72      public String toString() {
73          String result = methodName + " = " + parseValue;
74  
75          if (propertyName == null) {
76              result += " (hard-coded)";
77          } else {
78              result += ", from property " + propertyName + " = "
79                      + (originalValue == null ? "<null>" : "'" + originalValue + "'");
80          }
81  
82          return result;
83      }
84  
85      /***
86       * Name of the method for the property.
87       * 
88       * @return String. Never <code>null</code>.
89       */
90      public String getMethodName() {
91          return methodName;
92      }
93  
94      /***
95       * Original, unparsed value of the element in the property file.
96       * 
97       * @return String. Might be <code>null</code> if the element isn't read from the properties.
98       */
99      public String getOriginalValue() {
100         return originalValue;
101     }
102 
103     /***
104      * Parsed value of the element in the property file.
105      * 
106      * @return String. Never <code>null</code>.
107      */
108     public String getParseValue() {
109         return parseValue;
110     }
111 
112     /***
113      * Name of the element in the property file.
114      * 
115      * @return String. Might be <code>null</code> if the element isn't read from the properties.
116      */
117     public String getPropertyName() {
118         return propertyName;
119     }
120 
121     public int hashCode() {
122         return (methodName.hashCode());
123     }
124 
125     public boolean equals(Object o) {
126         if (o == this) {
127             return true;
128         }
129 
130         if (o == null) {
131             return false;
132         }
133 
134         if (o instanceof PropertyDisplayItem) {
135             PropertyDisplayItem other = (PropertyDisplayItem) o;
136 
137             return (other.methodName.equals(methodName) && areEquals(propertyName, other.propertyName)
138                     && areEquals(originalValue, other.originalValue) && areEquals(parseValue, other.parseValue));
139         } else {
140             return false;
141         }
142     }
143 
144     public int compareTo(Object o) {
145         PropertyDisplayItem rhs = (PropertyDisplayItem) o;
146         int result = compareStrings(methodName, rhs.methodName);
147         if (result != 0) return result;
148         result = compareStrings(propertyName, rhs.propertyName);
149         if (result != 0) return result;
150         result = compareStrings(originalValue, rhs.originalValue);
151         if (result != 0) return result;
152         result = compareStrings(parseValue, rhs.parseValue);
153         return result;
154     }
155 
156     private boolean areEquals(String string1, String string2) {
157         if (string1 == null) {
158             if (string2 == null) {
159                 return true;
160             } else {
161                 return false;
162             }
163         }
164         return string1.equals(string2);
165     }
166 
167     private int compareStrings(String string1, String string2) {
168         if (string1 == string2) return 0;
169         if (string1 == null) return -1;
170         if (string2 == null) return 1;
171         return (string1.compareTo(string2));
172     }
173 }