001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * This package contains documentation from OpenGIS specifications.
018: * OpenGIS consortium's work is fully acknowledged here.
019: */
020: package org.geotools.referencing;
021:
022: // J2SE dependencies
023: import java.util.AbstractMap;
024: import java.util.Collections;
025: import java.util.HashSet;
026: import java.util.Set;
027:
028: // OpenGIS dependencies
029: import org.opengis.referencing.IdentifiedObject;
030: import org.opengis.referencing.operation.CoordinateOperation;
031:
032: // Geotools dependencies
033: import org.geotools.util.MapEntry;
034: import org.geotools.referencing.operation.AbstractCoordinateOperation;
035:
036: /**
037: * An immutable map fetching all properties from the specified identified object.
038: * Calls to {@code get} methods are forwarded to the appropriate
039: * {@link IdentifiedObject} method.
040: *
041: * @since 2.1
042: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/referencing/Properties.java $
043: * @version $Id: Properties.java 28264 2007-12-05 21:53:08Z desruisseaux $
044: * @author Martin Desruisseaux
045: */
046: final class Properties extends AbstractMap {
047: /**
048: * The object where all properties come from.
049: */
050: private final IdentifiedObject info;
051:
052: /**
053: * The entries set. Will be constructed only when first needed.
054: */
055: private transient Set entries;
056:
057: /**
058: * Creates new properties from the specified identified object.
059: */
060: public Properties(final IdentifiedObject info) {
061: this .info = info;
062: AbstractIdentifiedObject.ensureNonNull("info", info);
063: }
064:
065: /**
066: * Returns true if this map contains a mapping for the specified key.
067: */
068: public boolean containsKey(final Object key) {
069: return get(key) != null;
070: }
071:
072: /**
073: * Returns the value to which this map maps the specified key.
074: * Returns null if the map contains no mapping for this key.
075: */
076: public Object get(final Object key) {
077: if (key instanceof String) {
078: final String s = ((String) key).trim();
079: for (int i = 0; i < KEYS.length; i++) {
080: if (KEYS[i].equalsIgnoreCase(s)) {
081: return get(i);
082: }
083: }
084: }
085: return null;
086: }
087:
088: /**
089: * Returns the value to which this map maps the specified index.
090: * Returns null if the map contains no mapping for this index.
091: */
092: private Object get(final int key) {
093: switch (key) {
094: case 0:
095: return info.getName();
096: case 1:
097: return info.getIdentifiers().toArray(
098: AbstractIdentifiedObject.EMPTY_IDENTIFIER_ARRAY);
099: case 2:
100: return info.getAlias().toArray(
101: AbstractIdentifiedObject.EMPTY_ALIAS_ARRAY);
102: case 3:
103: return info.getRemarks();
104: case 4:
105: return (info instanceof CoordinateOperation) ? ((CoordinateOperation) info)
106: .getScope()
107: : null;
108: case 5:
109: return (info instanceof CoordinateOperation) ? ((CoordinateOperation) info)
110: .getValidArea()
111: : null;
112: case 6:
113: return (info instanceof CoordinateOperation) ? ((CoordinateOperation) info)
114: .getOperationVersion()
115: : null;
116: case 7:
117: return (info instanceof CoordinateOperation) ? ((CoordinateOperation) info)
118: .getPositionalAccuracy()
119: .toArray(
120: AbstractCoordinateOperation.EMPTY_ACCURACY_ARRAY)
121: : null;
122: default:
123: return null;
124: }
125: }
126:
127: /**
128: * The keys to search for. <STRONG>The index of each element in this array
129: * must matches the index searched by {@link #get(int)}.</STRONG>
130: *
131: * @todo Add properties for {@link IdentifiedObject} sub-interfaces.
132: */
133: private static final String[] KEYS = {
134: /*[0]*/IdentifiedObject.NAME_KEY,
135: /*[1]*/IdentifiedObject.IDENTIFIERS_KEY,
136: /*[2]*/IdentifiedObject.ALIAS_KEY,
137: /*[3]*/IdentifiedObject.REMARKS_KEY,
138: /*[4]*/CoordinateOperation.SCOPE_KEY,
139: /*[5]*/CoordinateOperation.DOMAIN_OF_VALIDITY_KEY,
140: /*[6]*/CoordinateOperation.OPERATION_VERSION_KEY,
141: /*[7]*/CoordinateOperation.COORDINATE_OPERATION_ACCURACY_KEY };
142:
143: /**
144: * Returns a set view of the mappings contained in this map.
145: */
146: public Set entrySet() {
147: if (entries == null) {
148: entries = new HashSet(Math.round(KEYS.length / 0.75f) + 1,
149: 0.75f);
150: for (int i = 0; i < KEYS.length; i++) {
151: final Object value = get(i);
152: if (value != null) {
153: entries.add(new MapEntry(KEYS[i], value));
154: }
155: }
156: entries = Collections.unmodifiableSet(entries);
157: }
158: return entries;
159: }
160: }
|