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 javax.annotation.CheckForNull;
21
22 import org.apache.commons.lang.ObjectUtils;
23
24 import de.smartics.properties.api.config.domain.key.ApplicationId;
25 import de.smartics.util.lang.Arguments;
26
27 /**
28 * Identifies the location of a property.
29 */
30 public final class PropertyLocation implements Serializable,
31 Comparable<PropertyLocation>
32 {
33 // ********************************* Fields *********************************
34
35 // --- constants ------------------------------------------------------------
36
37 /**
38 * The class version identifier.
39 */
40 private static final long serialVersionUID = 1L;
41
42 // --- members --------------------------------------------------------------
43
44 /**
45 * The identifier of the application archive that contains the property. The
46 * value may be <code>null</code> if no archive contains the information. This
47 * is e.g. the case if the property is provided by a data source or other
48 * external information system.
49 *
50 * @serial
51 */
52 private final ApplicationId applicationId;
53
54 /**
55 * The identifier of the property location within the application. The path
56 * may serve as an ID like a JNDI name or name the path to a properties file
57 * within the {@link #getApplicationId() archive}.
58 * <p>
59 * This value must not be <code>null</code>.
60 * </p>
61 *
62 * @serial
63 */
64 private final String path;
65
66 // ****************************** Initializer *******************************
67
68 // ****************************** Constructors ******************************
69
70 /**
71 * Convenience constructor if no application identifier is available.
72 *
73 * @param path the identifier of the property location within the application.
74 * @throws IllegalArgumentException if {@code path} is <code>null</code>.
75 */
76 public PropertyLocation(final String path) throws IllegalArgumentException
77 {
78 this(null, path);
79 }
80
81 /**
82 * Default constructor.
83 *
84 * @param applicationId the identifier of the application archive that
85 * contains the property.
86 * @param path the identifier of the property location within the application.
87 * @throws IllegalArgumentException if {@code path} is <code>null</code>.
88 */
89 public PropertyLocation(final ApplicationId applicationId, final String path)
90 throws IllegalArgumentException
91 {
92 Arguments.checkNotBlank("path", path);
93 this.applicationId = applicationId;
94 this.path = path;
95 }
96
97 // ****************************** Inner Classes *****************************
98
99 // ********************************* Methods ********************************
100
101 // --- init -----------------------------------------------------------------
102
103 // --- get&set --------------------------------------------------------------
104
105 /**
106 * Returns the identifier of the application archive that contains the
107 * property. The value may be <code>null</code> if no archive contains the
108 * information. This is e.g. the case if the property is provided by a data
109 * source or other external information system.
110 *
111 * @return the identifier of the application archive that contains the
112 * property. May be <code>null</code>.
113 */
114 @CheckForNull
115 public ApplicationId getApplicationId()
116 {
117 return applicationId;
118 }
119
120 /**
121 * Returns the identifier of the property location within the application. The
122 * path may serve as an ID like a JNDI name or name the path to a properties
123 * file within the {@link #getApplicationId() archive}.
124 * <p>
125 * This value must not be <code>null</code>.
126 * </p>
127 *
128 * @return the identifier of the property location within the application.
129 */
130 public String getPath()
131 {
132 return path;
133 }
134
135 // --- business -------------------------------------------------------------
136
137 // --- object basics --------------------------------------------------------
138
139 @Override
140 public int compareTo(final PropertyLocation o)
141 {
142 int compare = ObjectUtils.compare(applicationId, o.applicationId);
143 if (compare == 0)
144 {
145 compare = path.compareTo(o.path);
146 }
147
148 return compare;
149 }
150
151 /**
152 * Returns the hash code of the object.
153 *
154 * @return the hash code.
155 */
156 @Override
157 public int hashCode()
158 {
159 int result = 17;
160 result = 37 * result + ObjectUtils.hashCode(applicationId);
161 result = 37 * result + path.hashCode();
162
163 return result;
164 }
165
166 /**
167 * Returns <code>true</code> if the given object is semantically equal to the
168 * given object, <code>false</code> otherwise.
169 *
170 * @param object the instance to compare to.
171 * @return <code>true</code> if the given object is semantically equal to the
172 * given object, <code>false</code> otherwise.
173 */
174 @Override
175 public boolean equals(final Object object)
176 {
177 if (this == object)
178 {
179 return true;
180 }
181 else if (object == null || getClass() != object.getClass())
182 {
183 return false;
184 }
185
186 final PropertyLocation other = (PropertyLocation) object;
187
188 return (ObjectUtils.equals(applicationId, other.applicationId) && path
189 .equals(other.path));
190 }
191
192 /**
193 * Returns the string representation of the object.
194 *
195 * @return the string representation of the object.
196 */
197 @Override
198 public String toString()
199 {
200 final StringBuilder buffer = new StringBuilder();
201
202 if (applicationId != null)
203 {
204 buffer.append(applicationId).append(':');
205 }
206
207 buffer.append(path);
208
209 return buffer.toString();
210 }
211 }