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.Arg; 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 this.source = Arg.checkNotNull("source", source); 80 this.name = Arg.checkNotBlank("name", name); 81 this.value = value; 82 } 83 84 /** 85 * Convenience constructor that translates the value to a String. 86 * 87 * @param source the optional source where the property value has been 88 * defined. 89 * @param name the name of the property. 90 * @param value the value of the property. 91 * @throws IllegalArgumentException if {@code name} is blank. 92 */ 93 public Property(final PropertyLocation source, final String name, 94 final Object value) throws IllegalArgumentException 95 { 96 this(source, name, ObjectUtils.toString(value, null)); 97 } 98 99 /** 100 * Convenience constructor that translates the value to a String. 101 * 102 * @param source the optional source where the property value has been 103 * defined. 104 * @param name the name of the property. 105 * @param value the value of the property. 106 * @throws IllegalArgumentException if {@code name} is blank. 107 */ 108 public Property(final String source, final String name, final Object value) 109 throws IllegalArgumentException 110 { 111 this(new PropertyLocation(source), name, ObjectUtils.toString(value, null)); 112 } 113 114 /** 115 * Copy-constructor. 116 * 117 * @param property the property to copy. 118 */ 119 protected Property(final Property property) 120 { 121 this(property.source, property.name, property.value); 122 } 123 124 // ****************************** Inner Classes ***************************** 125 126 // ********************************* Methods ******************************** 127 128 // --- init ----------------------------------------------------------------- 129 130 // --- get&set -------------------------------------------------------------- 131 132 /** 133 * Returns the optional source where the property value has been defined. This 134 * is an arbitrary name that defines the origin in its context. 135 * 136 * @return the optional source where the property value has been defined. 137 */ 138 public final PropertyLocation getSource() 139 { 140 return source; 141 } 142 143 /** 144 * Returns the name of the property. Must not blank. 145 * 146 * @return the name of the property. 147 */ 148 public final String getName() 149 { 150 return name; 151 } 152 153 /** 154 * Returns the value of the property. May be <code>null</code>. 155 * 156 * @return the value of the property. 157 */ 158 public final String getValue() 159 { 160 return value; 161 } 162 163 // --- business ------------------------------------------------------------- 164 165 // --- object basics -------------------------------------------------------- 166 167 /** 168 * {@inheritDoc} 169 * 170 * @see java.lang.Comparable#compareTo(java.lang.Object) 171 */ 172 @Override 173 public final int compareTo(final Property o) 174 { 175 int result = name.compareTo(o.name); 176 if (result == 0) 177 { 178 result = 179 ObjectUtils.compare(ObjectUtils.toString(value, null), 180 ObjectUtils.toString(o.value, null)); 181 if (result == 0) 182 { 183 result = ObjectUtils.compare(source, o.source); 184 } 185 } 186 return result; 187 } 188 189 /** 190 * Returns the hash code of the object. 191 * 192 * @return the hash code. 193 */ 194 @Override 195 public final int hashCode() 196 { 197 return name.hashCode(); 198 } 199 200 /** 201 * Returns <code>true</code> if the given object is semantically equal to the 202 * given object, <code>false</code> otherwise. 203 * 204 * @param object the instance to compare to. 205 * @return <code>true</code> if the given object is semantically equal to the 206 * given object, <code>false</code> otherwise. 207 */ 208 @Override 209 public final boolean equals(final Object object) 210 { 211 if (this == object) 212 { 213 return true; 214 } 215 else if (object == null || getClass() != object.getClass()) 216 { 217 return false; 218 } 219 220 final Property other = (Property) object; 221 222 return (name.equals(other.name) && ObjectUtils.equals(value, other.value) && ObjectUtils 223 .equals(source, other.source)); 224 } 225 226 /** 227 * Returns the string representation of the object. 228 * 229 * @return the string representation of the object. 230 */ 231 @Override 232 public String toString() 233 { 234 return name + '=' + value + " (" + source + ')'; 235 } 236 }