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/04/18 18:02:02 UTC
svn commit: r1469393 - in /sis/branches/JDK7:
sis-metadata/src/main/java/org/apache/sis/metadata/
sis-metadata/src/test/java/org/apache/sis/metadata/
sis-utility/src/main/java/org/apache/sis/internal/simple/
Author: desruisseaux
Date: Thu Apr 18 16:02:01 2013
New Revision: 1469393
URL: http://svn.apache.org/r1469393
Log:
Added the public MetadataStandard.asInformationMap method.
Added:
sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/InformationMap.java (with props)
Modified:
sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataMap.java
sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyInformation.java
sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java
sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PropertyInformationTest.java
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/simple/SimpleReferenceIdentifier.java
Added: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/InformationMap.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/InformationMap.java?rev=1469393&view=auto
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/InformationMap.java (added)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/InformationMap.java Thu Apr 18 16:02:01 2013
@@ -0,0 +1,93 @@
+/*
+ * 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.metadata;
+
+import java.util.Map;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import org.opengis.metadata.ExtendedElementInformation;
+
+
+/**
+ * Map of information for a given implementation class. This map is read-only.
+ * All values in this map are instances of {@link PropertyInformation}.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3 (derived from geotk-3.04)
+ * @version 0.3
+ * @module
+ *
+ * @see PropertyInformation
+ * @see MetadataStandard#asInformationMap(Class, KeyNamePolicy)
+ */
+final class InformationMap extends MetadataMap<ExtendedElementInformation> {
+ /**
+ * Creates an information map for the specified accessor.
+ *
+ * @param accessor The accessor to use for the metadata.
+ * @param keyNames Determines the string representation of keys in the map.
+ */
+ InformationMap(final PropertyAccessor accessor, final KeyNamePolicy keyNames) {
+ super(accessor, keyNames);
+ }
+
+ /**
+ * Returns {@code true} if this map contains a mapping for the specified key.
+ */
+ @Override
+ public boolean containsKey(final Object key) {
+ return get(key) != null;
+ }
+
+ /**
+ * Returns the information to which the specified key is mapped,
+ * or {@code null} if this map contains no mapping for the key.
+ */
+ @Override
+ public ExtendedElementInformation get(final Object key) {
+ if (key instanceof String) {
+ return accessor.information(accessor.indexOf((String) key, false));
+ }
+ return null;
+ }
+
+ /**
+ * Returns an iterator over the entries contained in this map.
+ */
+ @Override
+ final Iterator<Map.Entry<String,ExtendedElementInformation>> iterator() {
+ return new Iter() {
+ private int index;
+
+ @Override
+ public boolean hasNext() {
+ return index < accessor.count();
+ }
+
+ @Override
+ public Map.Entry<String,ExtendedElementInformation> next() {
+ final ExtendedElementInformation value = accessor.information(index);
+ if (value == null) {
+ // PropertyAccessor.information(int) never return null if the index is valid.
+ throw new NoSuchElementException();
+ }
+ index++;
+ return new SimpleImmutableEntry<>(accessor.name(index, keyPolicy), value);
+ }
+ };
+ }
+}
Propchange: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/InformationMap.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/InformationMap.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataMap.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataMap.java?rev=1469393&r1=1469392&r2=1469393&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataMap.java [UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataMap.java [UTF-8] Thu Apr 18 16:02:01 2013
@@ -59,9 +59,14 @@ abstract class MetadataMap<V> extends Ab
/**
* Returns the number of elements in this map.
+ * The default implementation returns {@link PropertyAccessor#count()}, which is okay only if
+ * all metadata defined by the standard are included in the map. Subclasses shall override
+ * this method if their map contain only a subset of all possible metadata elements.
*/
@Override
- public abstract int size();
+ public int size() {
+ return accessor.count();
+ }
/**
* Returns a view of the mappings contained in this map. Subclasses shall override this method
Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java?rev=1469393&r1=1469392&r2=1469393&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java [UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java [UTF-8] Thu Apr 18 16:02:01 2013
@@ -24,10 +24,13 @@ import java.util.Collection;
import java.util.Iterator;
import net.jcip.annotations.ThreadSafe;
import org.opengis.metadata.citation.Citation;
+import org.opengis.metadata.ExtendedElementInformation;
+import org.opengis.referencing.ReferenceIdentifier;
import org.apache.sis.util.Classes;
import org.apache.sis.util.ComparisonMode;
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.collection.TreeTable;
+import org.apache.sis.util.collection.CheckedContainer;
import org.apache.sis.internal.util.SystemListener;
import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
@@ -420,6 +423,55 @@ public class MetadataStandard {
}
/**
+ * Returns information about a metadata type as {@link Map}. The returned map contains information
+ * inferred from the ISO names, the {@link org.opengis.annotation.Obligation} enumeration and the
+ * {@link org.apache.sis.measure.ValueRange} annotations.
+ *
+ * <p>In the particular case of Apache SIS implementation, all values in the information map
+ * additionally implement the following interfaces:</p>
+ * <ul>
+ * <li>{@link ReferenceIdentifier} with the following properties:
+ * <ul>
+ * <li>The {@linkplain ReferenceIdentifier#getAuthority() authority} is this metadata standard {@linkplain #getCitation() citation}.</li>
+ * <li>The {@linkplain ReferenceIdentifier#getCodeSpace() codespace} is the standard name of the interface that contain the property.</li>
+ * <li>The {@linkplain ReferenceIdentifier#getCode() code} is the standard name of the property.</li>
+ * </ul>
+ * </li>
+ * <li>{@link CheckedContainer} with the following properties:
+ * <ul>
+ * <li>The {@linkplain CheckedContainer#getElementType() element type} is the type of property values
+ * as defined by {@link TypeValuePolicy#ELEMENT_TYPE}.</li>
+ * </ul>
+ * </li>
+ * </ul>
+ *
+ * In addition, for each map entry the value returned by {@link ExtendedElementInformation#getDomainValue()}
+ * may optionally be an instance of any of the following classes:
+ *
+ * <ul>
+ * <li>{@link org.apache.sis.measure.NumberRange} if the valid values are constrained to some specific range.</li>
+ * </ul>
+ *
+ * @param type The metadata interface or implementation class.
+ * @param keyNames Determines the string representation of map keys.
+ * @return The restrictions that are violated by the given metadata instance,
+ * or all restrictions if {@code metadata} is a {@link Class}.
+ * @throws ClassCastException if the given type doesn't implement a metadata
+ * interface of the expected package.
+ */
+ public Map<String,ExtendedElementInformation> asInformationMap(Class<?> type,
+ final KeyNamePolicy keyNames) throws ClassCastException
+ {
+ ensureNonNull("type", type);
+ ensureNonNull("keyNames", keyNames);
+ final Class<?> implementation = getImplementation(type);
+ if (implementation != null) {
+ type = implementation;
+ }
+ return new InformationMap(getAccessor(type, true), keyNames);
+ }
+
+ /**
* Returns a view of the specified metadata object as a {@link Map}.
* The map is backed by the metadata object using Java reflection, so changes in the
* underlying metadata object are immediately reflected in the map and conversely.
Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyInformation.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyInformation.java?rev=1469393&r1=1469392&r2=1469393&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyInformation.java [UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyInformation.java [UTF-8] Thu Apr 18 16:02:01 2013
@@ -16,6 +16,7 @@
*/
package org.apache.sis.metadata;
+import java.util.Locale;
import java.util.Collection;
import java.util.Collections;
import java.lang.reflect.Method;
@@ -40,7 +41,7 @@ import org.apache.sis.util.logging.Loggi
* Description of a metadata property inferred from Java reflection.
* For a given metadata instances (typically an {@link AbstractMetadata} subclasses,
* but other types are allowed), instances of {@code PropertyInformation} are obtained
- * indirectly by the {@link MetadataStandard#getElementInformation(Object, KeyNamePolicy)} method.
+ * indirectly by the {@link MetadataStandard#asInformationMap(Class, KeyNamePolicy)} method.
*
* @param <T> The value type, either the method return type if not a collection,
* or the type of elements in the collection otherwise.
@@ -50,7 +51,8 @@ import org.apache.sis.util.logging.Loggi
* @version 0.3
* @module
*
- * @see MetadataStandard#getElementInformation(Object, KeyNamePolicy)
+ * @see InformationMap
+ * @see MetadataStandard#asInformationMap(Class, KeyNamePolicy)
* @see <a href="https://issues.apache.org/jira/browse/SIS-80">SIS-80</a>
*/
@Immutable
@@ -265,7 +267,7 @@ final class PropertyInformation<T> exten
* In the particular case of SIS implementation, this method may return a subclass of {@link NumberRange}.
*/
@Override
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({"unchecked","rawtypes"})
public InternationalString getDomainValue() {
Object domain = domainValue;
if (domain != null) {
@@ -356,4 +358,25 @@ final class PropertyInformation<T> exten
public final int hashCode() {
return (parent.hashCode() + 31 * code.hashCode()) ^ (int) serialVersionUID;
}
+
+ /**
+ * Invoked by {@link #toString()} in order to append additional information
+ * after the identifier.
+ */
+ @Override
+ protected void appendToString(final StringBuilder buffer) {
+ buffer.append(" : ").append(Types.getCodeLabel(getDataType()))
+ .append(", ").append(getObligation().name().toLowerCase(Locale.US))
+ .append(", maxOccurs=");
+ final int n = getMaximumOccurrence();
+ if (n != Integer.MAX_VALUE) {
+ buffer.append(n);
+ } else {
+ buffer.append('∞');
+ }
+ final InternationalString domainValue = getDomainValue();
+ if (domainValue != null) {
+ buffer.append(", domain=").append(domainValue);
+ }
+ }
}
Modified: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java?rev=1469393&r1=1469392&r2=1469393&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java [UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java [UTF-8] Thu Apr 18 16:02:01 2013
@@ -22,6 +22,7 @@ import java.util.HashSet;
import java.util.Collection;
import org.opengis.metadata.citation.Citation;
import org.opengis.metadata.quality.Completeness;
+import org.opengis.metadata.ExtendedElementInformation;
import org.apache.sis.metadata.iso.citation.DefaultCitation;
import org.apache.sis.metadata.iso.citation.HardCodedCitations;
import org.apache.sis.metadata.iso.quality.AbstractCompleteness;
@@ -44,7 +45,7 @@ import static org.opengis.test.Assert.*;
* @version 0.3
* @module
*/
-@DependsOn(PropertyAccessorTest.class)
+@DependsOn({PropertyAccessorTest.class, PropertyInformationTest.class})
public final strictfp class MetadataStandardTest extends TestCase {
/**
* Tests {@link MetadataStandard#getInterface(Class)}.
@@ -116,8 +117,7 @@ public final strictfp class MetadataStan
/**
* Tests the {@link MetadataStandard#asMap(Object, KeyNamePolicy, ValueExistencePolicy)} implementation.
- * Note: this test duplicates {@link PropertyMapTest}, but is done here again because other tests in this
- * class depend on it.
+ * Note: this test duplicates {@link PropertyMapTest}, but is done here again as an integration test.
*/
@Test
public void testMap() {
@@ -153,6 +153,19 @@ public final strictfp class MetadataStan
}
/**
+ * Tests the {@link MetadataStandard#asInformationMap(Class, KeyNamePolicy)} implementation.
+ * Note: this test duplicates {@link PropertyInformationTest}, but is done here again as an
+ * integration test.
+ */
+ @Test
+ public void testInformationMap() {
+ final Map<String,ExtendedElementInformation> map = MetadataStandard.ISO_19115.asInformationMap(
+ Citation.class, KeyNamePolicy.JAVABEANS_PROPERTY);
+ PropertyInformationTest.validateTitle(map.get("title"));
+ PropertyInformationTest.validatePresentationForm(map.get("presentationForms"));
+ }
+
+ /**
* Tests {@link MetadataStandard#hashCode(Object)} using {@link HashSet} as the reference
* implementation for computing hash code values. The hash code is defined as the sum of
* hash code values of all non-empty properties, plus the hash code of the interface.
Modified: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PropertyInformationTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PropertyInformationTest.java?rev=1469393&r1=1469392&r2=1469393&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PropertyInformationTest.java [UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PropertyInformationTest.java [UTF-8] Thu Apr 18 16:02:01 2013
@@ -31,6 +31,7 @@ import org.apache.sis.util.collection.Ch
import org.apache.sis.measure.ValueRange;
import org.apache.sis.measure.Range;
import org.apache.sis.test.TestCase;
+import org.apache.sis.test.DependsOnMethod;
import org.junit.Test;
import static org.apache.sis.test.Assert.*;
@@ -137,7 +138,7 @@ public final strictfp class PropertyInfo
* @throws NoSuchMethodException Should never happen.
*/
@Test
- public void testGetDomainValue() throws NoSuchMethodException {
+ public void testGetDomainValue() throws NoSuchMethodException {
final ExtendedElementInformation information = new PropertyInformation<>(HardCodedCitations.ISO_19115,
"maxRelativeHumidity", EnvironmentalRecord.class.getMethod("getMaxRelativeHumidity"), Double.class,
DefaultEnvironmentalRecord.class.getMethod("getMaxRelativeHumidity").getAnnotation(ValueRange.class));
@@ -153,6 +154,22 @@ public final strictfp class PropertyInfo
}
/**
+ * Tests the {@link PropertyInformation#toString()} method.
+ * All information in the expected strings have been validated by previous tests in this class.
+ *
+ * @throws NoSuchMethodException Should never happen.
+ */
+ @Test
+ @DependsOnMethod({"testTitle", "testPresentationForm"})
+ public void testToString() throws NoSuchMethodException {
+ assertEquals("PropertyInformation[“CI_Citation:title” : Character string, mandatory, maxOccurs=1]",
+ create(InternationalString.class, "getTitle", "title").toString());
+
+ assertEquals("PropertyInformation[“CI_Citation:presentationForm” : Codelist, optional, maxOccurs=∞]",
+ create(PresentationForm.class, "getPresentationForms", "presentationForm").toString());
+ }
+
+ /**
* Tests serialization.
*
* @throws NoSuchMethodException Should never happen.
Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/simple/SimpleReferenceIdentifier.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/simple/SimpleReferenceIdentifier.java?rev=1469393&r1=1469392&r2=1469393&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/simple/SimpleReferenceIdentifier.java [UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/simple/SimpleReferenceIdentifier.java [UTF-8] Thu Apr 18 16:02:01 2013
@@ -175,10 +175,21 @@ public class SimpleReferenceIdentifier i
if (open) {
buffer.append('”');
}
+ appendToString(buffer);
return buffer.append(']').toString();
}
/**
+ * Invoked by {@link #toString()} in order to allow subclasses to add additional information.
+ * This method is invoked just before the final {@code ']'} is appended to the buffer.
+ *
+ * @param buffer A buffer filled with the {@link #toString()} characters,
+ * that subclasses can update.
+ */
+ protected void appendToString(final StringBuilder buffer) {
+ }
+
+ /**
* Returns a pseudo Well Known Text for this identifier.
* While this method is not defined in the {@link ReferenceIdentifier} interface, it is often
* defined in related interfaces like {@link org.opengis.referencing.IdentifiedObject}.