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/07/30 15:16:49 UTC
svn commit: r1508432 - in /sis/branches/JDK7/core/sis-metadata/src:
main/java/org/apache/sis/metadata/AbstractMetadata.java
main/java/org/apache/sis/metadata/ValueExistencePolicy.java
test/java/org/apache/sis/metadata/ValueMapTest.java
Author: desruisseaux
Date: Tue Jul 30 13:16:48 2013
New Revision: 1508432
URL: http://svn.apache.org/r1508432
Log:
Metadata java.util.Map view shall check for NilObjects.
Modified:
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/AbstractMetadata.java
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java
sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java
Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/AbstractMetadata.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/AbstractMetadata.java?rev=1508432&r1=1508431&r2=1508432&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/AbstractMetadata.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/AbstractMetadata.java [UTF-8] Tue Jul 30 13:16:48 2013
@@ -112,14 +112,15 @@ public abstract class AbstractMetadata i
}
/**
- * Returns {@code true} if this metadata contains only {@code null} or empty properties.
- * A property is considered empty in any of the following cases:
+ * Returns {@code true} if this metadata contains only {@code null},
+ * {@linkplain org.apache.sis.xml.NilObject nil} or empty properties.
+ * A non-null and non-nil property is considered empty in any of the following cases:
*
* <ul>
* <li>An empty {@linkplain CharSequence character sequences}.</li>
* <li>An {@linkplain java.util.Collection#isEmpty() empty collection} or an empty array.</li>
- * <li>A collection or array containing only {@code null} or empty elements.</li>
- * <li>An other metadata object containing only {@code null} or empty properties.</li>
+ * <li>A collection or array containing only {@code null}, nil or empty elements.</li>
+ * <li>An other metadata object containing only {@code null}, nil or empty properties.</li>
* </ul>
*
* Note that empty properties can be removed by calling the {@link ModifiableMetadata#prune()}
Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java?rev=1508432&r1=1508431&r2=1508432&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java [UTF-8] Tue Jul 30 13:16:48 2013
@@ -19,26 +19,34 @@ package org.apache.sis.metadata;
import java.util.Map;
import java.util.Collection;
import java.lang.reflect.Array;
+import org.apache.sis.xml.NilObject;
+import org.apache.sis.xml.NilReason;
/**
- * Whatever {@link MetadataStandard#asValueMap MetadataStandard.asValueMap(…)} shall contain
- * entries for null or empty values. By default the map does not provide
- * {@linkplain java.util.Map.Entry entries} for {@code null} metadata properties or
+ * Whatever {@link MetadataStandard#asValueMap MetadataStandard.asValueMap(…)} shall contain entries for null,
+ * {@linkplain org.apache.sis.xml.NilObject nil} or empty values. By default the value map does not provide
+ * {@linkplain java.util.Map.Entry entries} for {@code null} metadata properties, nil objects or
* {@linkplain java.util.Collection#isEmpty() empty collections}.
- * This enumeration allows control on this behavior.
+ * This enumeration allows to control this behavior.
+ *
+ * {@section Difference between null and nil}
+ * A null property is a reference which is {@code null} in the Java sense.
+ * Null references can be used for missing properties when no information is provided about why the property is missing.
+ * On the other hand, a nil object is a placeholder for a missing property similar in purpose to {@code null} references,
+ * except that an explanation about why the property is missing can be attached to those objects.
+ * Those explanations can be obtained by calls to the {@link org.apache.sis.xml.NilReason#forObject(Object)} method.
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-3.03)
- * @version 0.3
+ * @version 0.4
* @module
*
* @see MetadataStandard#asValueMap(Object, KeyNamePolicy, ValueExistencePolicy)
*/
public enum ValueExistencePolicy {
/**
- * Includes all entries in the map, including those having a null value or an
- * empty collection.
+ * Includes all entries in the map, including those having a null value or an empty collection.
*/
ALL() {
/** Never skip values. */
@@ -55,7 +63,10 @@ public enum ValueExistencePolicy {
/**
* Includes only the non-null properties.
+ * {@link org.apache.sis.xml.NilObject}s are included.
* Collections are included no matter if they are empty or not.
+ *
+ * <p>The set of {@code NON_NULL} properties is a subset of {@link #ALL} properties.</p>
*/
NON_NULL() {
/** Skips all null values. */
@@ -71,8 +82,29 @@ public enum ValueExistencePolicy {
},
/**
- * Includes only the properties that are non-null and non empty.
- * A non-null property is considered empty if:
+ * Includes only the non-null and non-nil properties.
+ * Collections are included no matter if they are empty or not.
+ *
+ * <p>The set of {@code NON_NIL} properties is a subset of {@link #NON_NULL} properties.</p>
+ *
+ * @since 0.4
+ */
+ NON_NIL() {
+ /** Skips all null or nil values. */
+ @Override boolean isSkipped(final Object value) {
+ return (value == null) || (value instanceof NilObject) || NilReason.forObject(value) != null;
+ }
+
+ /** Substitutes empty collections by a null singleton element, but not
+ null references since they are supposed to be skipped by this policy. */
+ @Override boolean substituteByNullElement(final Collection<?> values) {
+ return (values != null) && values.isEmpty();
+ }
+ },
+
+ /**
+ * Includes only the properties that are non-null, non-nil and non empty.
+ * A non-null and non-nil property is considered empty in any of the following cases:
*
* <ul>
* <li>It is a character sequence containing only {@linkplain Character#isWhitespace(int) whitespaces}.</li>
@@ -82,6 +114,8 @@ public enum ValueExistencePolicy {
* </ul>
*
* This is the default behavior of {@link AbstractMetadata#asMap()}.
+ *
+ * <p>The set of {@code NON_EMPTY} properties is a subset of {@link #NON_NIL} properties.</p>
*/
NON_EMPTY() {
/** Skips all null or empty values. */
@@ -118,11 +152,13 @@ public enum ValueExistencePolicy {
* we would need to add a check against infinite recursivity.</p>
*/
static boolean isNullOrEmpty(final Object value) {
- return value == null
- || ((value instanceof CharSequence) && isEmpty((CharSequence) value))
- || ((value instanceof Collection<?>) && ((Collection<?>) value).isEmpty())
- || ((value instanceof Map<?,?>) && ((Map<?,?>) value).isEmpty())
- || (value.getClass().isArray() && Array.getLength(value) == 0);
+ if (value == null) return true;
+ if (value instanceof NilObject) return true;
+ if (value instanceof CharSequence) return isEmpty((CharSequence) value);
+ if (value instanceof Collection<?>) return ((Collection<?>) value).isEmpty();
+ if (value instanceof Map<?,?>) return ((Map<?,?>) value).isEmpty();
+ if (value.getClass().isArray()) return Array.getLength(value) == 0;
+ return NilReason.forObject(value) != null;
}
/**
Modified: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java?rev=1508432&r1=1508431&r2=1508432&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java [UTF-8] Tue Jul 30 13:16:48 2013
@@ -20,6 +20,7 @@ import java.util.Map;
import java.util.Collection;
import org.opengis.util.InternationalString;
import org.opengis.metadata.Identifier;
+import org.apache.sis.xml.NilReason;
import org.apache.sis.util.iso.SimpleInternationalString;
import org.apache.sis.metadata.iso.citation.DefaultCitation;
import org.apache.sis.metadata.iso.citation.DefaultResponsibleParty;
@@ -45,7 +46,7 @@ import static org.apache.sis.test.TestUt
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-3.00)
- * @version 0.3
+ * @version 0.4
* @module
*
* @see MetadataStandardTest#testValueMap()
@@ -75,6 +76,7 @@ public final strictfp class ValueMapTest
* {@preformat text
* Citation
* ├─Title…………………………………………………… Undercurrent
+ * ├─Edition……………………………………………… <nil:unknown>
* ├─Cited Responsible Parties
* │ └─Individual Name……………… Testsuya Toyoda
* └─ISBN……………………………………………………… 9782505004509
@@ -92,6 +94,7 @@ public final strictfp class ValueMapTest
author.setIndividualName("Testsuya Toyoda");
citation.getCitedResponsibleParties().add(author);
citation.setISBN("9782505004509");
+ citation.setEdition(NilReason.UNKNOWN.createNilObject(InternationalString.class));
return MetadataStandard.ISO_19115.asValueMap(citation, KeyNamePolicy.JAVABEANS_PROPERTY, ValueExistencePolicy.NON_EMPTY);
}
@@ -101,9 +104,10 @@ public final strictfp class ValueMapTest
@Test
public void testGet() {
final Map<String,Object> map = createCitation();
- assertEquals("Undercurrent", map.get("title").toString());
- assertEquals(singletonList(author), map.get("citedResponsibleParties"));
- assertEquals("9782505004509", map.get("ISBN"));
+ assertEquals("Undercurrent", map.get("title").toString());
+ assertEquals(singletonList(author), map.get("citedResponsibleParties"));
+ assertEquals("9782505004509", map.get("ISBN"));
+ assertNull ("NilObject shall be excluded.", map.get("edition"));
/*
* The ISBN shall also be visible as an identifier.
*/
@@ -116,7 +120,7 @@ public final strictfp class ValueMapTest
/**
* Tests the {@link ValueMap#entrySet()} method.
- * The metadata to be tested is:
+ * The expected metadata is:
*
* {@preformat text
* Citation
@@ -145,7 +149,34 @@ public final strictfp class ValueMapTest
/**
* Tests the {@link ValueMap#entrySet()} method for the same metadata than {@link #testEntrySet()},
- * but asking for all non-null entries including the empty collections.
+ * but asking for all non-null and non-nil entries including the empty collections.
+ */
+ @Test
+ @DependsOnMethod("testEntrySet")
+ public void testEntrySetForNonNil() {
+ final Map<String,Object> map = createCitation();
+ final Map<String,Object> all = MetadataStandard.ISO_19115.asValueMap(citation,
+ KeyNamePolicy.JAVABEANS_PROPERTY, ValueExistencePolicy.NON_NIL);
+ assertFalse("Null values shall be excluded.", map.containsKey("alternateTitles"));
+ assertTrue ("Null values shall be included.", all.containsKey("alternateTitles"));
+ assertFalse("Nil objects shall be excluded.", map.containsKey("edition"));
+ assertFalse("Nil objects shall be excluded.", all.containsKey("edition"));
+ assertTrue ("'all' shall be a larger map than 'map'.", all.entrySet().containsAll(map.entrySet()));
+ assertFalse("'all' shall be a larger map than 'map'.", map.entrySet().containsAll(all.entrySet()));
+ assertArrayEquals(new SimpleEntry<?,?>[] {
+ new SimpleEntry<>("title", title),
+ new SimpleEntry<>("alternateTitles", emptyList()),
+ new SimpleEntry<>("dates", emptyList()),
+ new SimpleEntry<>("identifiers", citation.getIdentifiers()),
+ new SimpleEntry<>("citedResponsibleParties", singletonList(author)),
+ new SimpleEntry<>("presentationForms", emptySet()),
+ new SimpleEntry<>("ISBN", "9782505004509")
+ }, all.entrySet().toArray());
+ }
+
+ /**
+ * Tests the {@link ValueMap#entrySet()} method for the same metadata than {@link #testEntrySet()},
+ * but asking for all non-null entries including nil objects and the empty collections.
*/
@Test
@DependsOnMethod("testEntrySet")
@@ -153,14 +184,17 @@ public final strictfp class ValueMapTest
final Map<String,Object> map = createCitation();
final Map<String,Object> all = MetadataStandard.ISO_19115.asValueMap(citation,
KeyNamePolicy.JAVABEANS_PROPERTY, ValueExistencePolicy.NON_NULL);
- assertFalse("Null values should be excluded.", map.containsKey("alternateTitles"));
- assertTrue ("Null values should be included.", all.containsKey("alternateTitles"));
- assertTrue ("'all' should be a larger map than 'map'.", all.entrySet().containsAll(map.entrySet()));
- assertFalse("'all' should be a larger map than 'map'.", map.entrySet().containsAll(all.entrySet()));
+ assertFalse("Null values shall be excluded.", map.containsKey("alternateTitles"));
+ assertTrue ("Null values shall be included.", all.containsKey("alternateTitles"));
+ assertFalse("Nil objects shall be excluded.", map.containsKey("edition"));
+ assertTrue ("Nil objects shall be included.", all.containsKey("edition"));
+ assertTrue ("'all' shall be a larger map than 'map'.", all.entrySet().containsAll(map.entrySet()));
+ assertFalse("'all' shall be a larger map than 'map'.", map.entrySet().containsAll(all.entrySet()));
assertArrayEquals(new SimpleEntry<?,?>[] {
new SimpleEntry<>("title", title),
new SimpleEntry<>("alternateTitles", emptyList()),
new SimpleEntry<>("dates", emptyList()),
+ new SimpleEntry<>("edition", NilReason.UNKNOWN.createNilObject(InternationalString.class)),
new SimpleEntry<>("identifiers", citation.getIdentifiers()),
new SimpleEntry<>("citedResponsibleParties", singletonList(author)),
new SimpleEntry<>("presentationForms", emptySet()),
@@ -178,15 +212,15 @@ public final strictfp class ValueMapTest
final Map<String,Object> map = createCitation();
final Map<String,Object> all = MetadataStandard.ISO_19115.asValueMap(citation,
KeyNamePolicy.JAVABEANS_PROPERTY, ValueExistencePolicy.ALL);
- assertFalse("Null values should be excluded.", map.containsKey("alternateTitles"));
- assertTrue ("Null values should be included.", all.containsKey("alternateTitles"));
- assertTrue ("'all' should be a larger map than 'map'.", all.entrySet().containsAll(map.entrySet()));
- assertFalse("'all' should be a larger map than 'map'.", map.entrySet().containsAll(all.entrySet()));
+ assertFalse("Null values shall be excluded.", map.containsKey("alternateTitles"));
+ assertTrue ("Null values shall be included.", all.containsKey("alternateTitles"));
+ assertTrue ("'all' shall be a larger map than 'map'.", all.entrySet().containsAll(map.entrySet()));
+ assertFalse("'all' shall be a larger map than 'map'.", map.entrySet().containsAll(all.entrySet()));
assertArrayEquals(new SimpleEntry<?,?>[] {
new SimpleEntry<>("title", title),
new SimpleEntry<>("alternateTitles", emptyList()),
new SimpleEntry<>("dates", emptyList()),
- new SimpleEntry<>("edition", null),
+ new SimpleEntry<>("edition", NilReason.UNKNOWN.createNilObject(InternationalString.class)),
new SimpleEntry<>("editionDate", null),
new SimpleEntry<>("identifiers", citation.getIdentifiers()),
new SimpleEntry<>("citedResponsibleParties", singletonList(author)),