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/06/27 23:46:53 UTC
svn commit: r1497577 - 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/collection/
Author: desruisseaux
Date: Thu Jun 27 21:46:53 2013
New Revision: 1497577
URL: http://svn.apache.org/r1497577
Log:
Use CodeListSet / EnumSet in metadata objects when appropriate.
Modified:
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/TreeNodeChildrenTest.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/CodeListSet.java
Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java?rev=1497577&r1=1497576&r2=1497577&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java [UTF-8] Thu Jun 27 21:46:53 2013
@@ -18,13 +18,16 @@ package org.apache.sis.metadata;
import java.util.Set;
import java.util.List;
+import java.util.EnumSet;
import java.util.Collection;
import java.util.Collections;
import java.util.NoSuchElementException;
+import java.lang.reflect.Modifier;
import org.opengis.util.CodeList;
import org.apache.sis.util.ThreadSafe;
import org.apache.sis.util.logging.Logging;
import org.apache.sis.util.resources.Errors;
+import org.apache.sis.util.collection.CodeListSet;
import org.apache.sis.internal.util.CheckedHashSet;
import org.apache.sis.internal.util.CheckedArrayList;
@@ -383,7 +386,7 @@ public abstract class ModifiableMetadata
} else {
final int capacity = source.size();
if (useSet(elementType)) {
- target = new CheckedHashSet<>(elementType, capacity);
+ target = createSet(elementType, capacity);
} else {
target = new CheckedArrayList<>(elementType, capacity);
}
@@ -455,7 +458,7 @@ public abstract class ModifiableMetadata
final Collection<E> target;
final int capacity = source.size();
if (useSet(elementType)) {
- target = new CheckedHashSet<>(elementType, capacity);
+ target = createSet(elementType, capacity);
} else {
target = new CheckedArrayList<>(elementType, capacity);
}
@@ -482,7 +485,7 @@ public abstract class ModifiableMetadata
}
final Collection<E> collection;
if (useSet(elementType)) {
- collection = new CheckedHashSet<>(elementType, INITIAL_CAPACITY);
+ collection = createSet(elementType, INITIAL_CAPACITY);
} else {
collection = new CheckedArrayList<>(elementType, INITIAL_CAPACITY);
}
@@ -562,7 +565,7 @@ public abstract class ModifiableMetadata
final boolean isModifiable = isModifiable();
if (useSet(elementType)) {
if (isModifiable) {
- return new CheckedHashSet<>(elementType, INITIAL_CAPACITY);
+ return createSet(elementType, INITIAL_CAPACITY);
} else {
return Collections.emptySet();
}
@@ -576,17 +579,28 @@ public abstract class ModifiableMetadata
}
/**
+ * Creates a modifiable set for elements of the given type. This method will create an {@link EnumSet},
+ * {@link CodeListSet} or {@link LinkedHashSet} depending on the {@code elementType} argument.
+ */
+ @SuppressWarnings({"unchecked","rawtypes"})
+ private <E> Set<E> createSet(final Class<E> elementType, final int capacity) {
+ if (Enum.class.isAssignableFrom(elementType)) {
+ return EnumSet.noneOf((Class) elementType);
+ }
+ if (CodeList.class.isAssignableFrom(elementType) && Modifier.isFinal(elementType.getModifiers())) {
+ return new CodeListSet((Class) elementType);
+ }
+ return new CheckedHashSet<>(elementType, capacity);
+ }
+
+ /**
* Returns {@code true} if we should use a {@link Set} instead than a {@link List}
* for elements of the given type.
*/
private <E> boolean useSet(final Class<E> elementType) {
final Class<? extends Collection<E>> type = collectionType(elementType);
- if (Set.class.isAssignableFrom(type)) {
- return true;
- }
- if (List.class.isAssignableFrom(type)) {
- return false;
- }
+ if (Set .class == (Class) type) return true;
+ if (List.class == (Class) type) return false;
throw new NoSuchElementException(Errors.format(Errors.Keys.UnsupportedType_1, type));
}
Modified: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/TreeNodeChildrenTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/TreeNodeChildrenTest.java?rev=1497577&r1=1497576&r2=1497577&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/TreeNodeChildrenTest.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/TreeNodeChildrenTest.java [UTF-8] Thu Jun 27 21:46:53 2013
@@ -200,9 +200,9 @@ public final strictfp class TreeNodeChil
"Second alternate title",
"Third alternate title", // After addition
"New edition", // After "addition" (actually change).
+ "PresentationForm[IMAGE_DIGITAL]", // After addition
"PresentationForm[MAP_DIGITAL]",
"PresentationForm[MAP_HARDCOPY]",
- "PresentationForm[IMAGE_DIGITAL]", // After addition
"Some other details"
};
toAdd.setValue(TableColumn.IDENTIFIER, "edition");
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/CodeListSet.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/CodeListSet.java?rev=1497577&r1=1497576&r2=1497577&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/CodeListSet.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/CodeListSet.java [UTF-8] Thu Jun 27 21:46:53 2013
@@ -126,7 +126,7 @@ public class CodeListSet<E extends CodeL
* those new elements will <em>not</em> be in this set.
*
* @param elementType The type of code list elements to be included in this set.
- * @param fill {@code true} for filling the set with all known elements if the given type,
+ * @param fill {@code true} for filling the set with all known elements of the given type,
* or {@code false} for leaving the set empty.
* @throws IllegalArgumentException If the given class is not final.
*/