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.Arg;
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 this.applicationId = applicationId;
93 this.path = Arg.checkNotBlank("path", path);
94 }
95
96 // ****************************** Inner Classes *****************************
97
98 // ********************************* Methods ********************************
99
100 // --- init -----------------------------------------------------------------
101
102 // --- get&set --------------------------------------------------------------
103
104 /**
105 * Returns the identifier of the application archive that contains the
106 * property. The value may be <code>null</code> if no archive contains the
107 * information. This is e.g. the case if the property is provided by a data
108 * source or other external information system.
109 *
110 * @return the identifier of the application archive that contains the
111 * property. May be <code>null</code>.
112 */
113 @CheckForNull
114 public ApplicationId getApplicationId()
115 {
116 return applicationId;
117 }
118
119 /**
120 * Returns the identifier of the property location within the application. The
121 * path may serve as an ID like a JNDI name or name the path to a properties
122 * file within the {@link #getApplicationId() archive}.
123 * <p>
124 * This value must not be <code>null</code>.
125 * </p>
126 *
127 * @return the identifier of the property location within the application.
128 */
129 public String getPath()
130 {
131 return path;
132 }
133
134 // --- business -------------------------------------------------------------
135
136 // --- object basics --------------------------------------------------------
137
138 @Override
139 public int compareTo(final PropertyLocation o)
140 {
141 int compare = ObjectUtils.compare(applicationId, o.applicationId);
142 if (compare == 0)
143 {
144 compare = path.compareTo(o.path);
145 }
146
147 return compare;
148 }
149
150 /**
151 * Returns the hash code of the object.
152 *
153 * @return the hash code.
154 */
155 @Override
156 public int hashCode()
157 {
158 int result = 17;
159 result = 37 * result + ObjectUtils.hashCode(applicationId);
160 result = 37 * result + path.hashCode();
161
162 return result;
163 }
164
165 /**
166 * Returns <code>true</code> if the given object is semantically equal to the
167 * given object, <code>false</code> otherwise.
168 *
169 * @param object the instance to compare to.
170 * @return <code>true</code> if the given object is semantically equal to the
171 * given object, <code>false</code> otherwise.
172 */
173 @Override
174 public boolean equals(final Object object)
175 {
176 if (this == object)
177 {
178 return true;
179 }
180 else if (object == null || getClass() != object.getClass())
181 {
182 return false;
183 }
184
185 final PropertyLocation other = (PropertyLocation) object;
186
187 return (ObjectUtils.equals(applicationId, other.applicationId) && path
188 .equals(other.path));
189 }
190
191 /**
192 * Returns the string representation of the object.
193 *
194 * @return the string representation of the object.
195 */
196 @Override
197 public String toString()
198 {
199 final StringBuilder buffer = new StringBuilder();
200
201 if (applicationId != null)
202 {
203 buffer.append(applicationId).append(':');
204 }
205
206 buffer.append(path);
207
208 return buffer.toString();
209 }
210 }