sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1470057 - in /sis/trunk: ./ sis-metadata/src/main/java/org/apache/sis/metadata/ sis-metadata/src/main/java/org/apache/sis/metadata/iso/ sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/ sis-metadata/src/main/java/org/apache/...
Date Fri, 19 Apr 2013 21:52:37 GMT
Author: desruisseaux
Date: Fri Apr 19 21:52:36 2013
New Revision: 1470057

URL: http://svn.apache.org/r1470057
Log:
Merge from the JDK6 branch.

Added:
    sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/PrunerTest.java
      - copied unchanged from r1470056, sis/branches/JDK6/sis-metadata/src/test/java/org/apache/sis/metadata/PrunerTest.java
    sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/iso/citation/DefaultCitationDateTest.java
      - copied unchanged from r1470056, sis/branches/JDK6/sis-metadata/src/test/java/org/apache/sis/metadata/iso/citation/DefaultCitationDateTest.java
    sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/iso/citation/DefaultCitationTest.java
      - copied unchanged from r1470056, sis/branches/JDK6/sis-metadata/src/test/java/org/apache/sis/metadata/iso/citation/DefaultCitationTest.java
Modified:
    sis/trunk/   (props changed)
    sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
    sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java
    sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/Pruner.java
    sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java
    sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java
    sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/DefaultIdentifier.java
    sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/DefaultCitation.java
    sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/extent/DefaultGeographicDescription.java
    sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java
    sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java
    sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/iso/citation/HardCodedCitations.java
    sis/trunk/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
    sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java

Propchange: sis/trunk/
------------------------------------------------------------------------------
  Merged /sis/branches/JDK7:r1469957-1470054
  Merged /sis/branches/JDK6:r1469962-1470056

Modified: sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java [UTF-8]
(original)
+++ sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java [UTF-8]
Fri Apr 19 21:52:36 2013
@@ -582,11 +582,11 @@ public abstract class ModifiableMetadata
     private final class MutableSet<E> extends CheckedHashSet<E> {
         private static final long serialVersionUID = 3032602282358733056L;
 
-        public MutableSet(Class<E> type) {
+        MutableSet(Class<E> type) {
             super(type, 4); // Use a small capacity because we typically have few elements.
         }
 
-        public MutableSet(Class<E> type, int capacity) {
+        MutableSet(Class<E> type, int capacity) {
             super(type, hashMapCapacity(capacity));
         }
 
@@ -614,11 +614,11 @@ public abstract class ModifiableMetadata
     private final class MutableList<E> extends CheckedArrayList<E> {
         private static final long serialVersionUID = 5800381255701183058L;
 
-        public MutableList(Class<E> type) {
+        MutableList(Class<E> type) {
             super(type, 4); // Use a small capacity because we typically have few elements.
         }
 
-        public MutableList(Class<E> type, int capacity) {
+        MutableList(Class<E> type, int capacity) {
             super(type, capacity);
         }
 

Modified: sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java [UTF-8]
(original)
+++ sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java [UTF-8]
Fri Apr 19 21:52:36 2013
@@ -18,13 +18,13 @@ package org.apache.sis.metadata;
 
 import java.util.Set;
 import java.util.Map;
+import java.util.List;
 import java.util.Arrays;
 import java.util.Locale;
 import java.util.HashMap;
 import java.util.IdentityHashMap;
 import java.util.Iterator;
 import java.util.Collection;
-import java.lang.reflect.Array;
 import java.lang.reflect.Method;
 import java.lang.reflect.InvocationTargetException;
 import net.jcip.annotations.ThreadSafe;
@@ -48,7 +48,9 @@ import org.apache.sis.util.resources.Err
 import org.apache.sis.xml.IdentifiedObject;
 
 import static org.apache.sis.metadata.PropertyComparator.*;
+import static org.apache.sis.metadata.ValueExistencePolicy.isNullOrEmpty;
 import static org.apache.sis.internal.util.Utilities.floatEpsilonEqual;
+import static org.apache.sis.internal.util.CollectionsExt.snapshot;
 import static org.apache.sis.internal.util.CollectionsExt.modifiableCopy;
 import static org.apache.sis.util.collection.Containers.hashMapCapacity;
 
@@ -668,7 +670,11 @@ final class PropertyAccessor {
                 if (getOld) {
                     old = get(getter, metadata);
                     if (old instanceof Collection<?>) {
-                        old = modifiableCopy((Collection<?>) old);
+                        if (old instanceof List<?>) {
+                            old = snapshot((List<?>) old);
+                        } else {
+                            old = modifiableCopy((Collection<?>) old);
+                        }
                     } else if (old instanceof Map<?,?>) {
                         old = modifiableCopy((Map<?,?>) old);
                     }
@@ -1032,19 +1038,4 @@ final class PropertyAccessor {
         }
         return count;
     }
-
-    /**
-     * Returns {@code true} if the specified object is null or an empty collection, array
or string.
-     *
-     * <p>This method intentionally does not inspect array or collection elements,
since this method
-     * is invoked from methods doing shallow copy or comparison. If we were inspecting elements,
-     * we would need to add a check against infinite recursivity.</p>
-     */
-    static boolean isNullOrEmpty(final Object value) {
-        return value == null
-                || ((value instanceof CharSequence)  && ((CharSequence) value).length()
== 0)
-                || ((value instanceof Collection<?>) && ((Collection<?>)
value).isEmpty())
-                || ((value instanceof Map<?,?>)      && ((Map<?,?>) value).isEmpty())
-                || (value.getClass().isArray()       && Array.getLength(value) ==
0);
-    }
 }

Modified: sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/Pruner.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/Pruner.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/Pruner.java [UTF-8] (original)
+++ sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/Pruner.java [UTF-8] Fri Apr
19 21:52:36 2013
@@ -23,6 +23,8 @@ import java.util.IdentityHashMap;
 import org.opengis.util.CodeList;
 import org.apache.sis.internal.util.CollectionsExt;
 
+import static org.apache.sis.metadata.ValueExistencePolicy.*;
+
 
 /**
  * Implementation of {@link AbstractMetadata#isEmpty()} and {@link ModifiableMetadata#prune()}
@@ -59,8 +61,7 @@ final class Pruner extends ThreadLocal<M
      * include empty (but non-null) values in order to allow us to set them to {@code null}.
      */
     private static Map<String, Object> asMap(final MetadataStandard standard, final
Object metadata, final boolean prune) {
-        return standard.asValueMap(metadata, KeyNamePolicy.JAVABEANS_PROPERTY, prune ?
-                ValueExistencePolicy.NON_NULL : ValueExistencePolicy.NON_EMPTY);
+        return standard.asValueMap(metadata, KeyNamePolicy.JAVABEANS_PROPERTY, prune ? NON_NULL
: NON_EMPTY);
     }
 
     /**
@@ -136,7 +137,7 @@ final class Pruner extends ThreadLocal<M
                 final Collection<?> values = CollectionsExt.toCollection(value);
                 for (final Iterator<?> it = values.iterator(); it.hasNext();) {
                     final Object element = it.next();
-                    if (!PropertyAccessor.isNullOrEmpty(element)) {
+                    if (!isNullOrEmpty(element)) {
                         /*
                          * If the value is not an empty "simple" property (null value, or
empty
                          * string, or an empty collection or array), check if it is an other

Modified: sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java
[UTF-8] (original)
+++ sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java
[UTF-8] Fri Apr 19 21:52:36 2013
@@ -16,6 +16,10 @@
  */
 package org.apache.sis.metadata;
 
+import java.util.Map;
+import java.util.Collection;
+import java.lang.reflect.Array;
+
 
 /**
  * Whatever {@link MetadataStandard#asValueMap MetadataStandard.asValueMap(…)} shall contain
@@ -49,5 +53,40 @@ public enum ValueExistencePolicy {
      * non-{@linkplain java.util.Collection#isEmpty() empty}.
      * This is the default behavior of {@link AbstractMetadata#asMap()}.
      */
-    NON_EMPTY
+    NON_EMPTY;
+
+
+    /**
+     * Returns {@code true} if the specified object is null or an empty collection, array
or string.
+     *
+     * <p>This method intentionally does not inspect array or collection elements,
since this method
+     * is invoked from methods doing shallow copy or comparison. If we were inspecting elements,
+     * 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);
+    }
+
+    /**
+     * Returns {@code true} if the given character sequence shall be considered empty.
+     * The current implementation returns {@code true} if the sequence contains only
+     * whitespaces in the sense of Java (i.e. ignoring line feeds, but not ignoring
+     * non-breaking spaces). The exact criterion is not a committed part of the API
+     * and may change in future SIS versions according experiences.
+     */
+    private static boolean isEmpty(final CharSequence value) {
+        final int length = value.length();
+        for (int i=0; i<length;) {
+            final int c = Character.codePointAt(value, i);
+            if (!Character.isWhitespace(c)) {
+                return false;
+            }
+            i += Character.charCount(c);
+        }
+        return true;
+    }
 }

Modified: sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java [UTF-8] (original)
+++ sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java [UTF-8] Fri
Apr 19 21:52:36 2013
@@ -68,7 +68,7 @@ final class ValueMap extends PropertyMap
      * Returns {@code true} if the given value should be ignored.
      */
     final boolean ignore(final Object value) {
-        return valuePolicy == ValueExistencePolicy.NON_EMPTY && PropertyAccessor.isNullOrEmpty(value);
+        return valuePolicy == ValueExistencePolicy.NON_EMPTY && ValueExistencePolicy.isNullOrEmpty(value);
     }
 
     /**
@@ -311,7 +311,7 @@ final class ValueMap extends PropertyMap
                         break;
                     }
                     case NON_EMPTY: {
-                        skip = PropertyAccessor.isNullOrEmpty(value);
+                        skip = ValueExistencePolicy.isNullOrEmpty(value);
                         break;
                     }
                     default: {

Modified: sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/DefaultIdentifier.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/DefaultIdentifier.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/DefaultIdentifier.java
[UTF-8] (original)
+++ sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/DefaultIdentifier.java
[UTF-8] Fri Apr 19 21:52:36 2013
@@ -84,14 +84,14 @@ public class DefaultIdentifier extends I
     /**
      * Creates an identifier initialized to the given authority and code.
      *
-     * @param code      The alphanumeric value identifying an instance in the namespace,
-     *                  or {@code null} if none.
      * @param authority The organization or party responsible for definition and maintenance
      *                  of the code, or {@code null} if none.
+     * @param code      The alphanumeric value identifying an instance in the namespace,
+     *                  or {@code null} if none.
      */
-    public DefaultIdentifier(final String code, final Citation authority) {
-        this.code = code;
+    public DefaultIdentifier(final Citation authority, final String code) {
         this.authority = authority;
+        this.code = code;
     }
 
     /**

Modified: sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/DefaultCitation.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/DefaultCitation.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/DefaultCitation.java
[UTF-8] (original)
+++ sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/citation/DefaultCitation.java
[UTF-8] Fri Apr 19 21:52:36 2013
@@ -503,7 +503,7 @@ public class DefaultCitation extends ISO
     @Override
     @XmlElement(name = "ISBN")
     public synchronized String getISBN() {
-        return isNullOrEmpty(identifiers) ? null : getIdentifierMap().getSpecialized(ISBN);
+        return isNullOrEmpty(identifiers) ? null : getIdentifierMap().get(ISBN);
     }
 
     /**
@@ -540,7 +540,7 @@ public class DefaultCitation extends ISO
     @Override
     @XmlElement(name = "ISSN")
     public synchronized String getISSN() {
-        return isNullOrEmpty(identifiers) ? null : getIdentifierMap().getSpecialized(ISSN);
+        return isNullOrEmpty(identifiers) ? null : getIdentifierMap().get(ISSN);
     }
 
     /**

Modified: sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/extent/DefaultGeographicDescription.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/extent/DefaultGeographicDescription.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/extent/DefaultGeographicDescription.java
[UTF-8] (original)
+++ sis/trunk/sis-metadata/src/main/java/org/apache/sis/metadata/iso/extent/DefaultGeographicDescription.java
[UTF-8] Fri Apr 19 21:52:36 2013
@@ -66,7 +66,7 @@ public class DefaultGeographicDescriptio
     public DefaultGeographicDescription(final Citation authority, final String code) {
         super(true);
         if (authority != null || code != null) {
-            this.geographicIdentifier = new DefaultIdentifier(code, authority);
+            this.geographicIdentifier = new DefaultIdentifier(authority, code);
         }
     }
 

Modified: sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java
[UTF-8] (original)
+++ sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java
[UTF-8] Fri Apr 19 21:52:36 2013
@@ -53,8 +53,12 @@ import static org.opengis.test.Assert.*;
  * @version 0.3
  * @module
  */
-@DependsOn({PropertyAccessorTest.class, InformationMapTest.class,
-    NameMapTest.class, TypeMapTest.class, ValueMapTest.class})
+@DependsOn({
+    PropertyAccessorTest.class,
+    InformationMapTest.class,
+    NameMapTest.class,
+    TypeMapTest.class,
+    ValueMapTest.class})
 public final strictfp class MetadataStandardTest extends TestCase {
     /**
      * Tests {@link MetadataStandard#getInterface(Class)}.

Modified: sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java [UTF-8]
(original)
+++ sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/ValueMapTest.java [UTF-8]
Fri Apr 19 21:52:36 2013
@@ -200,13 +200,13 @@ public final strictfp class ValueMapTest
     }
 
     /**
-     * Tests the {@link ValueMap#add(String,Object)} and {@link ValueMap#remove(Object)}
methods.
+     * Tests the {@link ValueMap#put(String,Object)} and {@link ValueMap#remove(Object)}
methods.
      * Note that this test is intentionally sensitive to iteration order.
      * That order shall be fixed by the {@code XmlType} annotation.
      */
     @Test
     @DependsOnMethod("testEntrySet")
-    public void testAddAndRemove() {
+    public void testPutAndRemove() {
         final Map<String,Object> map = createCitation();
         /*
          * Remove the ISBN value. Result shall be:

Modified: sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/iso/citation/HardCodedCitations.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/iso/citation/HardCodedCitations.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/iso/citation/HardCodedCitations.java
[UTF-8] (original)
+++ sis/trunk/sis-metadata/src/test/java/org/apache/sis/metadata/iso/citation/HardCodedCitations.java
[UTF-8] Fri Apr 19 21:52:36 2013
@@ -42,6 +42,22 @@ import static java.util.Collections.sing
  */
 public final strictfp class HardCodedCitations extends Static {
     /**
+     * The <a href="http://www.opengeospatial.org">Open Geospatial consortium</a>
organization.
+     * "Open Geospatial consortium" is the new name for "OpenGIS consortium".
+     * An {@linkplain Citation#getAlternateTitles() alternate title} for this citation is
"OGC"
+     * (according ISO 19115, alternate titles often contain abbreviations).
+     */
+    public static final DefaultCitation OGC;
+    static {
+        final DefaultCitation c = new DefaultCitation("Open Geospatial consortium");
+        c.setAlternateTitles(singleton(new SimpleInternationalString("OGC")));
+        c.setPresentationForms(singleton(PresentationForm.DOCUMENT_DIGITAL));
+        c.getIdentifiers().add(new DefaultIdentifier("OGC"));
+        c.freeze();
+        OGC = c;
+    }
+
+    /**
      * The <a href="http://www.iso.org/">International Organization for Standardization</a>
      * organization. An {@linkplain Citation#getAlternateTitles() alternate title} for this
      * citation is "ISO" (according ISO 19115, alternate titles often contain abbreviations).

Modified: sis/trunk/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
[UTF-8] (original)
+++ sis/trunk/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
[UTF-8] Fri Apr 19 21:52:36 2013
@@ -31,13 +31,18 @@ import org.junit.BeforeClass;
  */
 @Suite.SuiteClasses({
     org.apache.sis.internal.metadata.MetadataUtilitiesTest.class,
+    org.apache.sis.metadata.iso.citation.DefaultCitationDateTest.class,
+    org.apache.sis.metadata.iso.citation.DefaultCitationTest.class,
+
+    // Classes using Java reflection.
     org.apache.sis.metadata.PropertyInformationTest.class,
     org.apache.sis.metadata.PropertyAccessorTest.class,
     org.apache.sis.metadata.NameMapTest.class,
     org.apache.sis.metadata.TypeMapTest.class,
     org.apache.sis.metadata.InformationMapTest.class,
     org.apache.sis.metadata.ValueMapTest.class,
-    org.apache.sis.metadata.MetadataStandardTest.class
+    org.apache.sis.metadata.MetadataStandardTest.class,
+    org.apache.sis.metadata.PrunerTest.class
 })
 public final strictfp class MetadataTestSuite extends TestSuite {
     /**

Modified: sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java?rev=1470057&r1=1470056&r2=1470057&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java [UTF-8]
(original)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java [UTF-8]
Fri Apr 19 21:52:36 2013
@@ -280,6 +280,27 @@ public final class CollectionsExt extend
     }
 
     /**
+     * Returns a snapshot of the given list. The returned list will not be affected by changes
+     * in the given list after this method call. This method makes no guaranteed about whether
+     * the returned list is modifiable or not.
+     *
+     * @param  <E>  The type of elements in the list.
+     * @param  list The list for which to take a snapshot, or {@code null} if none.
+     * @return A snapshot of the given list, or {@code list} itself if null or unmodifiable.
+     */
+    @SuppressWarnings("unchecked")
+    public static <E> List<E> snapshot(final List<E> list) {
+        if (list != null && !(list instanceof UnmodifiableArrayList<?>)) {
+            switch (list.size()) {
+                case 0:  return Collections.emptyList();
+                case 1:  return Collections.singletonList(list.get(0));
+                default: return (List<E>) Arrays.asList(list.toArray());
+            }
+        }
+        return list;
+    }
+
+    /**
      * Returns the given value as a collection. Special cases:
      *
      * <ul>



Mime
View raw message