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/12 19:56:16 UTC
svn commit: r1513210 - in /sis/branches/JDK7/core:
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/util/
Author: desruisseaux
Date: Mon Aug 12 17:56:16 2013
New Revision: 1513210
URL: http://svn.apache.org/r1513210
Log:
More tests regarding the declared type of GeographicBoundingBox longitudes and latitudes.
Modified:
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/SpecialCases.java
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/TypeValuePolicy.java
sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/SpecialCasesTest.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java
Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java?rev=1513210&r1=1513209&r2=1513210&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java [UTF-8] Mon Aug 12 17:56:16 2013
@@ -349,24 +349,6 @@ class PropertyAccessor {
}
/**
- * Sets the type of the given property. This method is invoked by the {@link SpecialCases} constructor only,
- * and should never be invoked in any other context. This restriction exists because {@code PropertyAcessor}
- * shall be immutable after construction.
- *
- * <p>Subclasses that invoke this method shall also override the {@link #get(int, Object)} and
- * {@link #set(int, Object, Object, int)} methods.</p>
- *
- * @param name The name of the property for which to modify the element type.
- * @param type The new element type for the named property.
- * @return The index of the modified property.
- */
- final int setType(final String name, final Class<?> type) {
- final int index = indexOf(name, true);
- elementTypes[index] = type;
- return index;
- }
-
- /**
* Adds the given (name, index) pair to {@link #mapping}, making sure we don't
* overwrite an existing entry with different value.
*/
@@ -553,7 +535,7 @@ class PropertyAccessor {
* @param policy The kind of type to return.
* @return The type of property values, or {@code null} if unknown.
*/
- final Class<?> type(final int index, final TypeValuePolicy policy) {
+ Class<?> type(final int index, final TypeValuePolicy policy) {
if (index >= 0 && index < standardCount) {
switch (policy) {
case ELEMENT_TYPE: {
@@ -617,7 +599,8 @@ class PropertyAccessor {
final Method getter = getters[index];
ValueRange range = null;
if (implementation != type) {
- if (Number.class.isAssignableFrom(elementType)) try {
+ final int e = Numbers.getEnumConstant(elementType);
+ if (e >= Numbers.BYTE && e <= Numbers.DOUBLE) try {
range = implementation.getMethod(getter.getName(), (Class<?>[]) null)
.getAnnotation(ValueRange.class);
} catch (NoSuchMethodException error) {
Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/SpecialCases.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/SpecialCases.java?rev=1513210&r1=1513209&r2=1513210&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/SpecialCases.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/SpecialCases.java [UTF-8] Mon Aug 12 17:56:16 2013
@@ -51,10 +51,10 @@ final class SpecialCases extends Propert
SpecialCases(final Citation standard, final Class<?> type, final Class<?> implementation) {
super(standard, type, implementation);
assert isSpecialCase(type) : type;
- westBoundLongitude = setType("westBoundLongitude", Longitude.class);
- eastBoundLongitude = setType("eastBoundLongitude", Longitude.class);
- southBoundLatitude = setType("southBoundLatitude", Latitude.class);
- northBoundLatitude = setType("northBoundLatitude", Latitude.class);
+ westBoundLongitude = indexOf("westBoundLongitude", true);
+ eastBoundLongitude = indexOf("eastBoundLongitude", true);
+ southBoundLatitude = indexOf("southBoundLatitude", true);
+ northBoundLatitude = indexOf("northBoundLatitude", true);
}
/**
@@ -68,6 +68,27 @@ final class SpecialCases extends Propert
}
/**
+ * Delegates to {@link PropertyAccessor#type(int)}, then substitutes the type for the properties
+ * handled in a special way.
+ */
+ @Override
+ Class<?> type(final int index, final TypeValuePolicy policy) {
+ Class<?> type = super.type(index, policy);
+ switch (policy) {
+ case PROPERTY_TYPE:
+ case ELEMENT_TYPE: {
+ if (index == westBoundLongitude || index == eastBoundLongitude) {
+ type = Longitude.class;
+ } else if (index == southBoundLatitude || index == northBoundLatitude) {
+ type = Latitude.class;
+ }
+ break;
+ }
+ }
+ return type;
+ }
+
+ /**
* Delegates to {@link PropertyAccessor#get(int, Object)}, then substitutes the value for the properties
* handled in a special way.
*/
Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/TypeValuePolicy.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/TypeValuePolicy.java?rev=1513210&r1=1513209&r2=1513210&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/TypeValuePolicy.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/TypeValuePolicy.java [UTF-8] Mon Aug 12 17:56:16 2013
@@ -32,23 +32,32 @@ package org.apache.sis.metadata;
public enum TypeValuePolicy {
/**
* The type of a property, as inferred from the
- * {@linkplain java.lang.reflect.Method#getReturnType() return type} of the property method.
- * Collections are not handled in any special way; if the return type is a collection, then
- * the value is {@code Collection.class} (or a subclass).
+ * {@linkplain java.lang.reflect.Method#getReturnType() return type} of the property method
+ * defined in the interface.
+ *
+ * <p><b>Notes:</b></p>
+ * <ul>
+ * <li>Collections are not handled in any special way: if the return type is a collection,
+ * then the property type is {@code Collection.class} or any other declared return type.</li>
+ * <li>As a special case, values of type {@code double} (the primitive type) in
+ * {@link org.opengis.metadata.extent.GeographicBoundingBox} are wrapped in
+ * {@link org.apache.sis.measure.Longitude} and {@link org.apache.sis.measure.Latitude}
+ * objects instead of {@link Double}.</li>
+ * </ul>
*/
PROPERTY_TYPE,
/**
- * The type of a property, or type of elements if the property is a collection. This is the
- * same than {@link #PROPERTY_TYPE} except that collections are handled in a special way:
- * if the property is a collection, then the value is the type of <em>elements</em> in that
- * collection.
+ * The specialized type of a property, or type of elements if the property is a collection.
+ * This is the same type than {@link #PROPERTY_TYPE} except for the following:
*
- * {@note Current implementation has an additional slight difference: if the getter method
- * in the implementation class declares a more specific return value than the getter
- * method in the interface, and if the setter method (if any) expects the same specialized
- * type, then <code>ELEMENT_TYPE</code> will use that specialized type. This is different
- * than <code>PROPERTY_TYPE</code> which always use the type declared in the interface.}
+ * <ul>
+ * <li>If the property is a collection, then the element type is the type of <em>elements</em> in that
+ * collection. For example if the property type is {@code Collection<String>}, then the element type
+ * is {@code String}.</li>
+ * <li>If the implementation declares a more specific property type than the interface (as allowed by
+ * <cite>covariant return type</cite>), then the element type will be that specialized type.</li>
+ * </ul>
*/
ELEMENT_TYPE,
Modified: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/SpecialCasesTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/SpecialCasesTest.java?rev=1513210&r1=1513209&r2=1513210&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/SpecialCasesTest.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/SpecialCasesTest.java [UTF-8] Mon Aug 12 17:56:16 2013
@@ -16,17 +16,20 @@
*/
package org.apache.sis.metadata;
+import org.opengis.util.InternationalString;
+import org.opengis.metadata.ExtendedElementInformation;
import org.opengis.metadata.extent.GeographicBoundingBox;
import org.apache.sis.measure.Latitude;
import org.apache.sis.measure.Longitude;
+import org.apache.sis.measure.NumberRange;
import org.apache.sis.metadata.iso.citation.HardCodedCitations;
import org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox;
-import org.apache.sis.test.DependsOn;
import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.DependsOn;
import org.apache.sis.test.TestCase;
import org.junit.Test;
-import static org.junit.Assert.*;
+import static org.apache.sis.test.Assert.*;
/**
@@ -52,7 +55,7 @@ public final strictfp class SpecialCases
/**
* An arbitrary bounding box to be used for testing purpose.
*/
- private final DefaultGeographicBoundingBox box;
+ private DefaultGeographicBoundingBox box;
/**
* Creates a new test case.
@@ -60,10 +63,24 @@ public final strictfp class SpecialCases
public SpecialCasesTest() {
accessor = new SpecialCases(HardCodedCitations.ISO_19115,
GeographicBoundingBox.class, DefaultGeographicBoundingBox.class);
+ }
+
+ /**
+ * Initializes the {@link #box} field.
+ * Invoked only by the tests that need an actual metadata instance.
+ */
+ private void createBox() {
box = new DefaultGeographicBoundingBox(-20, 30, -10, 40);
}
/**
+ * Invokes {@link SpecialCases#type(int)} and ensure that the result is equals to the expected value.
+ */
+ private void assertTypeEquals(final String name, final Class<?> expected) {
+ assertEquals(name, expected, accessor.type(accessor.indexOf(name, true), TypeValuePolicy.ELEMENT_TYPE));
+ }
+
+ /**
* Invokes {@link SpecialCases#get(int, Object)} and ensure that the result is equals to the expected value.
*/
private void assertPropertyEquals(final String name, final Object expected) {
@@ -89,10 +106,23 @@ public final strictfp class SpecialCases
}
/**
+ * Tests {@link SpecialCases#type(int, TypeValuePolicy)}.
+ */
+ @Test
+ public void testType() {
+ assertTypeEquals("westBoundLongitude", Longitude.class);
+ assertTypeEquals("eastBoundLongitude", Longitude.class);
+ assertTypeEquals("southBoundLatitude", Latitude.class);
+ assertTypeEquals("northBoundLatitude", Latitude.class);
+ assertTypeEquals("extentTypeCode", Boolean.class);
+ }
+
+ /**
* Tests {@link SpecialCases#get(int, Object)}.
*/
@Test
public void testGet() {
+ createBox();
assertPropertyEquals("westBoundLongitude", new Longitude(-20));
assertPropertyEquals("eastBoundLongitude", new Longitude( 30));
assertPropertyEquals("southBoundLatitude", new Latitude (-10));
@@ -106,6 +136,7 @@ public final strictfp class SpecialCases
@Test
@DependsOnMethod("testGet")
public void testSet() {
+ createBox();
assertPreviousEquals("westBoundLongitude", new Longitude(-20), new Longitude(-15));
assertPreviousEquals("eastBoundLongitude", new Longitude( 30), new Longitude( 25));
assertPreviousEquals("southBoundLatitude", new Latitude (-10), new Latitude ( -5));
@@ -126,6 +157,7 @@ public final strictfp class SpecialCases
@Test
@DependsOnMethod("testSet")
public void testSetAsPrimitive() {
+ createBox();
assertPreviousEquals("westBoundLongitude", new Longitude(-20), -14.0);
assertPreviousEquals("eastBoundLongitude", new Longitude( 30), 26 );
assertPreviousEquals("southBoundLatitude", new Latitude (-10), -7f );
@@ -144,6 +176,7 @@ public final strictfp class SpecialCases
@Test
@DependsOnMethod("testSet")
public void testAppend() {
+ createBox();
assertAppendResultEquals("westBoundLongitude", null, new Longitude(-20));
assertAppendResultEquals("eastBoundLongitude", null, new Longitude( 24));
assertAppendResultEquals("southBoundLatitude", null, new Latitude ( -6));
@@ -156,4 +189,17 @@ public final strictfp class SpecialCases
assertEquals("northBoundLatitude", 40, box.getNorthBoundLatitude(), STRICT);
assertEquals("extentTypeCode", Boolean.TRUE, box.getInclusion());
}
+
+ /**
+ * Tests {@link SpecialCases#information(int)}.
+ */
+ @Test
+ public void testPropertyInformation() {
+ final ExtendedElementInformation info = accessor.information(accessor.indexOf("westBoundLongitude", true));
+ final InternationalString domain = info.getDomainValue();
+ assertInstanceOf("Expected numerical information about range.", NumberRange.class, domain);
+ final NumberRange<?> range = (NumberRange) domain;
+ assertEquals(-180, range.getMinDouble(), STRICT);
+ assertEquals(+180, range.getMaxDouble(), STRICT);
+ }
}
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java?rev=1513210&r1=1513209&r2=1513210&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java [UTF-8] Mon Aug 12 17:56:16 2013
@@ -483,15 +483,16 @@ public final class Numbers extends Stati
}
/**
- * Casts a number to the specified class. The class must by one of {@link Byte},
+ * Casts a number to the specified type. The target type can be one of {@link Byte},
* {@link Short}, {@link Integer}, {@link Long}, {@link Float}, {@link Double},
* {@link BigInteger} or {@link BigDecimal}.
* This method makes the following choice:
*
* <ul>
- * <li>If the given type is {@code Double.class}, then this method returns
+ * <li>If the given value is {@code null} or an instance of the given type, then it is returned unchanged.</li>
+ * <li>Otherwise if the given type is {@code Double.class}, then this method returns
* <code>{@linkplain Double#valueOf(double) Double.valueOf}(number.doubleValue())</code>;</li>
- * <li>If the given type is {@code Float.class}, then this method returns
+ * <li>Otherwise if the given type is {@code Float.class}, then this method returns
* <code>{@linkplain Float#valueOf(float) Float.valueOf}(number.floatValue())</code>;</li>
* <li>And likewise for all remaining known types.</li>
* </ul>