You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2013/08/21 19:03:28 UTC
svn commit: r1516223 - in
/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing:
CRS.java Properties.java
Author: desruisseaux
Date: Wed Aug 21 17:03:27 2013
New Revision: 1516223
URL: http://svn.apache.org/r1516223
Log:
Ported a class to be needed for IdentifiedObject implementation.
Added:
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/Properties.java (with props)
Modified:
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java
Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java?rev=1516223&r1=1516222&r2=1516223&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/CRS.java [UTF-8] Wed Aug 21 17:03:27 2013
@@ -27,7 +27,7 @@ import static org.apache.sis.util.Argume
/**
* Static methods working on {@linkplain CoordinateReferenceSystem Coordinate Reference Systems}.
*
- * @author Martin Desruisseaux (IRD, Geomatys)
+ * @author Martin Desruisseaux (IRD, Geomatys)
* @since 0.3 (derived from geotk-2.1)
* @version 0.3
* @module
Added: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/Properties.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/Properties.java?rev=1516223&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/Properties.java (added)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/Properties.java [UTF-8] Wed Aug 21 17:03:27 2013
@@ -0,0 +1,314 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.referencing;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.HashMap;
+import java.util.AbstractMap;
+import java.util.AbstractSet;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.io.Serializable;
+import org.opengis.util.GenericName;
+import org.opengis.referencing.datum.Datum;
+import org.opengis.referencing.ReferenceSystem;
+import org.opengis.referencing.IdentifiedObject;
+import org.opengis.referencing.ReferenceIdentifier;
+import org.opengis.referencing.operation.CoordinateOperation;
+import org.opengis.metadata.quality.PositionalAccuracy;
+
+
+/**
+ * An immutable map fetching all properties from the specified identified object.
+ * Calls to {@code get} methods are forwarded to the appropriate {@link IdentifiedObject} method.
+ * This map does not contain null value. Collections are converted to arrays.
+ *
+ * <p>This map is read-only. Whether it is serializable, immutable or thread-safe depends if the
+ * underlying {@code IdentifiedObject} instance is itself serializable, immutable or thread-safe.</p>
+ *
+ * @author Martin Desruisseaux (IRD)
+ * @since 0.4 (derived from geotk-2.0)
+ * @version 0.4
+ * @module
+ */
+final class Properties extends AbstractMap<String,Object> implements Serializable {
+ /**
+ * For cross-version compatibility.
+ */
+ private static final long serialVersionUID = 6391635771714311314L;
+
+ /**
+ * The keys to search for. The index of each element in this array must matches the index searched
+ * by {@link #get(IdentifiedObject, int)}. In other words, this array performs the reverse mapping
+ * of {@link #INDICES}.
+ */
+ private static final String[] KEYS = {
+ /*[0]*/ IdentifiedObject .NAME_KEY,
+ /*[1]*/ IdentifiedObject .IDENTIFIERS_KEY,
+ /*[2]*/ IdentifiedObject .ALIAS_KEY,
+ /*[3]*/ IdentifiedObject .REMARKS_KEY,
+ /*[4]*/ CoordinateOperation .SCOPE_KEY, // same in Datum and ReferenceSystem
+ /*[5]*/ CoordinateOperation .DOMAIN_OF_VALIDITY_KEY, // same in Datum and ReferenceSystem
+ /*[6]*/ CoordinateOperation .OPERATION_VERSION_KEY,
+ /*[7]*/ CoordinateOperation .COORDINATE_OPERATION_ACCURACY_KEY
+ };
+
+ /**
+ * The mapping from key names to the index expected by the {@link #get(IdentifiedObject, int)} method.
+ * This map shall not be modified after construction (for multi-thread safety without synchronization).
+ */
+ private static final Map<String,Integer> INDICES = new HashMap<>(16);
+ static {
+ for (int i=0; i<KEYS.length; i++) {
+ if (INDICES.put(KEYS[i], i) != null) {
+ throw new AssertionError(i);
+ }
+ }
+ }
+
+ /**
+ * Returns the value to which this map maps the specified index.
+ * Returns null if the map contains no mapping for the given index.
+ *
+ * @param info The object from which to get the property value.
+ * @param key The property index, as one of the values in the {@link #INDICES} map.
+ */
+ private static Object get(final IdentifiedObject info, final int key) {
+ switch (key) {
+ case 0: {
+ return info.getName();
+ }
+ case 1: {
+ final Collection<ReferenceIdentifier> c = info.getIdentifiers();
+ if (c != null) {
+ final int size = c.size();
+ if (size != 0) {
+ return c.toArray(new ReferenceIdentifier[size]);
+ }
+ }
+ break;
+ }
+ case 2: {
+ final Collection<GenericName> c = info.getAlias();
+ if (c != null) {
+ final int size = c.size();
+ if (size != 0) {
+ return c.toArray(new GenericName[size]);
+ }
+ }
+ break;
+ }
+ case 3: {
+ return info.getRemarks();
+ }
+ case 4: {
+ if (info instanceof ReferenceSystem) {
+ return ((ReferenceSystem) info).getScope();
+ } else if (info instanceof Datum) {
+ return ((Datum) info).getScope();
+ } else if (info instanceof CoordinateOperation) {
+ return ((CoordinateOperation) info).getScope();
+ }
+ break;
+ }
+ case 5: {
+ if (info instanceof ReferenceSystem) {
+ return ((ReferenceSystem) info).getDomainOfValidity();
+ } else if (info instanceof Datum) {
+ return ((Datum) info).getDomainOfValidity();
+ } else if (info instanceof CoordinateOperation) {
+ return ((CoordinateOperation) info).getDomainOfValidity();
+ }
+ break;
+ }
+ case 6: {
+ if (info instanceof CoordinateOperation) {
+ return ((CoordinateOperation) info).getOperationVersion();
+ }
+ break;
+ }
+ case 7: {
+ if (info instanceof CoordinateOperation) {
+ final Collection<PositionalAccuracy> c = ((CoordinateOperation) info).getCoordinateOperationAccuracy();
+ if (c != null) {
+ final int size = c.size();
+ if (size != 0) {
+ return c.toArray(new PositionalAccuracy[size]);
+ }
+ }
+ }
+ break;
+ }
+ default: throw new AssertionError(key);
+ }
+ return null;
+ }
+
+ /**
+ * The object where all properties come from.
+ */
+ final IdentifiedObject info;
+
+ /**
+ * Creates new properties from the specified identified object.
+ */
+ Properties(final IdentifiedObject info) {
+ this.info = info;
+ }
+
+ /**
+ * Returns {@code false} if this map contains at least one element, or {@code true} otherwise.
+ */
+ @Override
+ public boolean isEmpty() {
+ for (int i=0; i<KEYS.length; i++) {
+ if (get(info, i) != null) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Returns the number of non-null properties in this map.
+ */
+ @Override
+ public int size() {
+ int n = 0;
+ for (int i=0; i<KEYS.length; i++) {
+ if (get(info, i) != null) {
+ n++;
+ }
+ }
+ return n;
+ }
+
+ /**
+ * Returns true if this map contains a mapping for the specified key.
+ */
+ @Override
+ public boolean containsKey(final Object key) {
+ return get(key) != null;
+ }
+
+ /**
+ * Returns the value to which this map maps the specified key.
+ * Returns {@code null} if the map contains no mapping for this key.
+ */
+ @Override
+ public Object get(final Object key) {
+ final Integer i = INDICES.get(key);
+ return (i != null) ? get(info, i) : null;
+ }
+
+ /**
+ * Returns a set view of the mappings contained in this map.
+ */
+ @Override
+ public Set<Entry<String,Object>> entrySet() {
+ return new EntrySet();
+ }
+
+ /**
+ * The view returned by {@link #entrySet()}.
+ */
+ private final class EntrySet extends AbstractSet<Entry<String,Object>> {
+ /** Creates a new instance. */
+ EntrySet() {
+ }
+
+ /** Delegates to the enclosing map. */
+ @Override
+ public boolean isEmpty() {
+ return Properties.this.isEmpty();
+ }
+
+ /** Delegates to the enclosing map. */
+ @Override
+ public int size() {
+ return Properties.this.size();
+ }
+
+ /** Iterates over the {@link #KEYS}, returning only the entry having a non-null value. */
+ @Override
+ public Iterator<Entry<String, Object>> iterator() {
+ return new Iter(info);
+ }
+ }
+
+ /**
+ * The iterator returned by {@link EntrySet#iterator()}.
+ */
+ private static final class Iter implements Iterator<Entry<String,Object>> {
+ /**
+ * A copy of the {@link Properties#info} reference.
+ */
+ private final IdentifiedObject info;
+
+ /**
+ * Index of the next element to return.
+ */
+ private int nextIndex;
+
+ /**
+ * Index of the value to be returned by {@link #next()}, or {@code null} if not yet computed.
+ */
+ private Object value;
+
+ /**
+ * Creates a new iterator wrapping the given object.
+ */
+ Iter(final IdentifiedObject info) {
+ this.info = info;
+ }
+
+ /**
+ * Returns {@code true} if there is a value to return.
+ */
+ @Override
+ public boolean hasNext() {
+ while (value == null) {
+ if (nextIndex == KEYS.length) {
+ return false;
+ }
+ value = get(info, nextIndex++);
+ }
+ return true;
+ }
+
+ /**
+ * Returns the next element.
+ */
+ @Override
+ public Entry<String, Object> next() {
+ if (hasNext()) {
+ return new SimpleImmutableEntry<>(KEYS[nextIndex-1], value);
+ }
+ throw new NoSuchElementException();
+ }
+
+ /**
+ * Unsupported operation, since this map is read-only.
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ }
+}
Propchange: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/Properties.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/Properties.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8