1 | /*
|
---|
2 | * GeoTools - The Open Source Java GIS Toolkit
|
---|
3 | * http://geotools.org
|
---|
4 | *
|
---|
5 | * (C) 2004-2008, Open Source Geospatial Foundation (OSGeo)
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Lesser General Public
|
---|
9 | * License as published by the Free Software Foundation;
|
---|
10 | * version 2.1 of the License.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Lesser General Public License for more details.
|
---|
16 | *
|
---|
17 | * This package contains documentation from OpenGIS specifications.
|
---|
18 | * OpenGIS consortium's work is fully acknowledged here.
|
---|
19 | */
|
---|
20 | package org.geotools.util;
|
---|
21 |
|
---|
22 | import java.util.Collections;
|
---|
23 | import java.util.List;
|
---|
24 |
|
---|
25 | import org.opengis.util.GenericName;
|
---|
26 | import org.opengis.util.InternationalString;
|
---|
27 | import org.opengis.util.NameSpace;
|
---|
28 | import org.opengis.util.ScopedName;
|
---|
29 |
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Identifier within a name space for a local object. This could be the target object of the
|
---|
33 | * {@link GenericName}, or a pointer to another name space (with a new {@link GenericName})
|
---|
34 | * one step closer to the target of the identifier.
|
---|
35 | *
|
---|
36 | * @since 2.1
|
---|
37 | *
|
---|
38 | * @source $URL: http://svn.osgeo.org/geotools/branches/2.7.x/modules/library/metadata/src/main/java/org/geotools/util/LocalName.java $
|
---|
39 | * @version $Id: LocalName.java 37298 2011-05-25 05:16:15Z mbedward $
|
---|
40 | * @author Martin Desruisseaux (IRD)
|
---|
41 | *
|
---|
42 | * @see NameFactory
|
---|
43 | */
|
---|
44 | public class LocalName extends org.geotools.util.GenericName implements org.opengis.util.LocalName {
|
---|
45 | /**
|
---|
46 | * Serial number for interoperability with different versions.
|
---|
47 | */
|
---|
48 | private static final long serialVersionUID = -5627125375582385822L;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * The view of this object as a scoped name.
|
---|
52 | */
|
---|
53 | private final ScopedName asScopedName;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * The name, either as a {@link String} or an {@link InternationalString}.
|
---|
57 | */
|
---|
58 | private final CharSequence name;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * The name as a string.
|
---|
62 | * If not provided, will be built only when first needed.
|
---|
63 | */
|
---|
64 | private transient String asString;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * The name as an international string.
|
---|
68 | * If not provided, will be built only when first needed.
|
---|
69 | */
|
---|
70 | private transient InternationalString asInternationalString;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * The sequence of local name for this {@linkplain GenericName generic name}.
|
---|
74 | * Since this object is itself a locale name, this list is always a singleton
|
---|
75 | * containing only {@code this}. It will be built only when first needed.
|
---|
76 | */
|
---|
77 | private transient List<org.opengis.util.LocalName> parsedNames;
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Constructs a local name from the specified string with no scope.
|
---|
81 | * If the specified name is an {@link InternationalString}, then the
|
---|
82 | * <code>{@linkplain InternationalString#toString(java.util.Locale) toString}(null)</code>
|
---|
83 | * method will be used in order to fetch an unlocalized name. Otherwise, the
|
---|
84 | * <code>{@linkplain CharSequence#toString toString}()</code> method will be used.
|
---|
85 | *
|
---|
86 | * @param name The local name (never {@code null}).
|
---|
87 | */
|
---|
88 | public LocalName(final CharSequence name) {
|
---|
89 | this(null, name);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Constructs a local name from the specified international string.
|
---|
94 | *
|
---|
95 | * This constructor is not public since it can't be used from outside
|
---|
96 | * of {@link org.geotools.util.ScopedName} constructor (otherwise some
|
---|
97 | * methods in this class may have the wrong semantic).
|
---|
98 | *
|
---|
99 | * @param asScopedName The view of this object as a scoped name.
|
---|
100 | * @param name The local name (never {@code null}).
|
---|
101 | */
|
---|
102 | LocalName(final ScopedName asScopedName, final CharSequence name) {
|
---|
103 | this.asScopedName = asScopedName;
|
---|
104 | this.name = validate(name);
|
---|
105 | AbstractInternationalString.ensureNonNull("name", name);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Returns the scope (name space) of this generic name.
|
---|
110 | * This method is protected from overriding by the user.
|
---|
111 | */
|
---|
112 | private GenericName getInternalScope() {
|
---|
113 | if (asScopedName != null) {
|
---|
114 | final NameSpace scope = asScopedName.scope();
|
---|
115 | if (scope != null) {
|
---|
116 | return scope.name();
|
---|
117 | }
|
---|
118 | }
|
---|
119 | return null;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Returns the scope (name space) of this generic name.
|
---|
124 | *
|
---|
125 | * @deprecated Replaced by {@link #scope}.
|
---|
126 | */
|
---|
127 | @Deprecated
|
---|
128 | public GenericName getScope() {
|
---|
129 | return getInternalScope();
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Returns the scope (name space) in which this name is local. The scope is set on creation
|
---|
134 | * and is not modifiable. The scope of a name determines where a name "starts". For instance,
|
---|
135 | * if a name has a {@linkplain #depth depth} of two ({@code "util.GenericName"}) and is
|
---|
136 | * associated with a {@linkplain NameSpace name space} having the name {@code "org.opengis"},
|
---|
137 | * then the fully qualified name would be {@code "org.opengis.util.GenericName"}.
|
---|
138 | *
|
---|
139 | * @since 2.3
|
---|
140 | *
|
---|
141 | * @todo To be strict, maybe we should returns {@code null} if there is no namespace.
|
---|
142 | * Current implementation returns a namespace instance whith a null name. This
|
---|
143 | * behavior is for transition from legacy API to later ISO 19103 revision and
|
---|
144 | * may change in future GeoTools version.
|
---|
145 | */
|
---|
146 | public NameSpace scope() {
|
---|
147 | return (asScopedName!=null) ? asScopedName.scope() : super.scope();
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Returns the depth, which is always 1 for a local name.
|
---|
152 | *
|
---|
153 | * @since 2.3
|
---|
154 | */
|
---|
155 | public int depth() {
|
---|
156 | return 1;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Returns the sequence of local name for this {@linkplain GenericName generic name}.
|
---|
161 | * Since this object is itself a locale name, this method always returns a singleton
|
---|
162 | * containing only {@code this}.
|
---|
163 | */
|
---|
164 | public List<org.opengis.util.LocalName> getParsedNames() {
|
---|
165 | // No need to sychronize: it is not a big deal if this object is built twice.
|
---|
166 | if (parsedNames == null) {
|
---|
167 | parsedNames = Collections.singletonList((org.opengis.util.LocalName) this);
|
---|
168 | }
|
---|
169 | return parsedNames;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Since this object is already a local name, this method always returns {@code this}.
|
---|
174 | */
|
---|
175 | @Override
|
---|
176 | public org.opengis.util.LocalName head() {
|
---|
177 | return this;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Since this object is already a local name, this method always returns {@code this}.
|
---|
182 | */
|
---|
183 | @Override
|
---|
184 | public org.opengis.util.LocalName tip() {
|
---|
185 | return this;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Returns a view of this object as a scoped name,
|
---|
190 | * or {@code null} if this name has no scope.
|
---|
191 | *
|
---|
192 | * @deprecated Replaced by {@link #toFullyQualifiedName}.
|
---|
193 | */
|
---|
194 | @Deprecated
|
---|
195 | public ScopedName asScopedName() {
|
---|
196 | return asScopedName;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Returns a view of this name as a fully-qualified name. The {@linkplain #scope scope}
|
---|
201 | * of a fully qualified name must be {@linkplain NameSpace#isGlobal global}. This method
|
---|
202 | * never returns {@code null}.
|
---|
203 | *
|
---|
204 | * @since 2.3
|
---|
205 | */
|
---|
206 | public GenericName toFullyQualifiedName() {
|
---|
207 | if (asScopedName == null) {
|
---|
208 | return this;
|
---|
209 | }
|
---|
210 | return asScopedName;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Returns this name expanded with the specified scope. One may represent this operation
|
---|
215 | * as a concatenation of the specified {@code name} with {@code this}. In pseudo-code,
|
---|
216 | * the following relationships must hold:
|
---|
217 | * <p>
|
---|
218 | * <ul>
|
---|
219 | * <li><code>push(<var>name</var>).getParsedList() ==
|
---|
220 | * <var>name</var>.getParsedList().addAll({@linkplain #getParsedNames()})</code></li>
|
---|
221 | * <li><code>push(<var>name</var>).scope() == <var>name</var>.{@linkplain #scope()}</code></li>
|
---|
222 | * </ul>
|
---|
223 | * <p>
|
---|
224 | * <strong>Note:</strong> Those conditions can be understood in terms of the Java
|
---|
225 | * {@link Object#equals equals} method instead of the Java identity comparator {@code ==}.
|
---|
226 | *
|
---|
227 | * @since 2.3
|
---|
228 | *
|
---|
229 | * @todo Not yet implemented.
|
---|
230 | */
|
---|
231 | public ScopedName push(GenericName scope) {
|
---|
232 | throw new UnsupportedOperationException("Not yet implemented");
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Returns a locale-independant string representation of this local name.
|
---|
237 | * This string do not includes the scope, which is consistent with the
|
---|
238 | * {@linkplain #getParsedNames parsed names} definition.
|
---|
239 | */
|
---|
240 | @Override
|
---|
241 | public String toString() {
|
---|
242 | if (asString == null) {
|
---|
243 | if (name instanceof InternationalString) {
|
---|
244 | // We really want the 'null' locale, not the system default one.
|
---|
245 | asString = ((InternationalString) name).toString(null);
|
---|
246 | } else {
|
---|
247 | asString = name.toString();
|
---|
248 | }
|
---|
249 | }
|
---|
250 | return asString;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Returns a local-dependent string representation of this locale name.
|
---|
255 | */
|
---|
256 | @Override
|
---|
257 | public InternationalString toInternationalString() {
|
---|
258 | if (asInternationalString == null) {
|
---|
259 | if (name instanceof InternationalString) {
|
---|
260 | asInternationalString = (InternationalString) name;
|
---|
261 | } else {
|
---|
262 | asInternationalString = new SimpleInternationalString(name.toString());
|
---|
263 | }
|
---|
264 | }
|
---|
265 | return asInternationalString;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Compares this name with the specified object for order. Returns a negative integer,
|
---|
270 | * zero, or a positive integer as this name lexicographically precedes, is equals to,
|
---|
271 | * or follows the specified object. The comparaison is case-insensitive.
|
---|
272 | */
|
---|
273 | @Override
|
---|
274 | public int compareTo(final GenericName object) {
|
---|
275 | return toString().compareToIgnoreCase(object.toString());
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Compares this local name with the specified object for equality.
|
---|
280 | */
|
---|
281 | @Override
|
---|
282 | public boolean equals(final Object object) {
|
---|
283 | if (object == this) {
|
---|
284 | return true;
|
---|
285 | }
|
---|
286 | if (object!=null && object.getClass().equals(getClass())) {
|
---|
287 | final LocalName that = (LocalName) object;
|
---|
288 | // Do not use 'asScopedName' in order to avoid never-ending loop.
|
---|
289 | return Utilities.equals(this.getInternalScope(), that.getInternalScope()) &&
|
---|
290 | Utilities.equals(this.name, that.name);
|
---|
291 | }
|
---|
292 | return false;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Returns a hash code value for this local name.
|
---|
297 | */
|
---|
298 | @Override
|
---|
299 | public int hashCode() {
|
---|
300 | int code = (int)serialVersionUID;
|
---|
301 | // Do not use 'asScopedName' in order to avoid never-ending loop.
|
---|
302 | if (name != null) code ^= name.hashCode();
|
---|
303 | return code;
|
---|
304 | }
|
---|
305 | }
|
---|