sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1595029 - in /sis/branches/JDK8/core/sis-feature/src: main/java/org/apache/sis/feature/PropertySingleton.java test/java/org/apache/sis/feature/PropertySingletonTest.java
Date Thu, 15 May 2014 20:12:44 GMT
Author: desruisseaux
Date: Thu May 15 20:12:44 2014
New Revision: 1595029

URL: http://svn.apache.org/r1595029
Log:
Modified PropertySingleton for its new use in DefaultAttribute.getValues() (later method not
yet provided).

Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/PropertySingleton.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/PropertySingletonTest.java

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/PropertySingleton.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/PropertySingleton.java?rev=1595029&r1=1595028&r2=1595029&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/PropertySingleton.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/PropertySingleton.java
[UTF-8] Thu May 15 20:12:44 2014
@@ -16,7 +16,6 @@
  */
 package org.apache.sis.feature;
 
-import java.util.Map;
 import java.util.AbstractList;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
@@ -27,59 +26,37 @@ import static org.apache.sis.util.Argume
 
 
 /**
- * A list containing 0 or 1 attribute. This implementation is used in the very common case
where a
- * {@link DefaultFeature} accepts at most one attribute for a given name. Its main purpose
is to
- * reduce the amount of objects in memory, compared to using an {@link java.util.ArrayList}.
+ * A list containing 0 or 1 value. This implementation is used in the very common case where
a
+ * {@link DefaultAttribute} accepts at most one value. Its main purpose is to reduce the
amount
+ * of objects in memory compared to {@link java.util.ArrayList}.
  *
  * <p>There is no need to keep long-lived references to instances of this class.
  * Instances can be recreated when needed.</p>
  *
- * {@section Non serialization}
- * This class is intentionally not serializable, since serializing this instance would imply
serializing the whole
- * map of attributes if we want to keep the <cite>change in this list are reflected
in the feature</cite> contract.
- *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.5
  * @version 0.5
  * @module
  */
-final class PropertySingleton extends AbstractList<DefaultAttribute<?>> {
-    /**
-     * An empty list of attributes.
-     */
-    private static final DefaultAttribute<?>[] EMPTY = new DefaultAttribute<?>[0];
-
-    /**
-     * The type of the property element in this list.
-     */
-    private final AbstractIdentifiedType type;
-
+final class PropertySingleton<V> extends AbstractList<V> {
     /**
-     * The map of properties in which to look for the attributes.
-     * This is the same reference than {@link DefaultFeature#properties}.
+     * The property where to read and write the value.
      */
-    private final Map<String, Object> properties;
+    private final DefaultAttribute<V> property;
 
     /**
-     * The key for the attribute in the {@link #properties} map.
+     * Creates a new list for the value of the given property.
      */
-    private final String key;
-
-    /**
-     * Creates a new list for the attribute associated to the given key in the given map.
-     */
-    PropertySingleton(final AbstractIdentifiedType type, final Map<String, Object>
properties, final String key) {
-        this.type       = type;
-        this.properties = properties;
-        this.key        = key;
+    PropertySingleton(final DefaultAttribute<V> property) {
+        this.property = property;
     }
 
     /**
-     * Returns 1 or 0, depending on whether or not an attribute is associated to the key.
+     * Returns 1 or 0, depending on whether or not a value exists.
      */
     @Override
     public int size() {
-        return properties.get(key) != null ? 1 : 0;
+        return property.getValue() == null ? 0 : 1;
     }
 
     /**
@@ -87,7 +64,7 @@ final class PropertySingleton extends Ab
      */
     @Override
     public int indexOf(final Object element) {
-        return (element != null) && element.equals(properties.get(key)) ? 0 : -1;
+        return (element != null) && element.equals(property.getValue()) ? 0 : -1;
     }
 
     /**
@@ -99,89 +76,75 @@ final class PropertySingleton extends Ab
     }
 
     /**
-     * Returns the attribute associated to the key, if present.
+     * Returns the property value, if present.
      */
     @Override
-    public DefaultAttribute<?> get(final int index) {
+    public V get(final int index) {
         if (index == 0) {
-            final Object element = properties.get(key);
+            final V element = property.getValue();
             if (element != null) {
-                return (DefaultAttribute<?>) element;
+                return element;
             }
         }
         throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.IndexOutOfBounds_1,
index));
     }
 
     /**
-     * Sets the attribute associated to the key, if an instance already exists.
+     * Sets the property value, if an instance already exists.
      */
     @Override
-    public DefaultAttribute<?> set(final int index, final DefaultAttribute<?>
element) {
+    public V set(final int index, final V element) {
         ensureNonNull("element", element);
-        ensureValidType(element);
         if (index == 0) {
-            modCount++;
-            final Object previous = properties.put(key, element);
+            final V previous = property.getValue();
             if (previous != null) {
-                return (DefaultAttribute<?>) previous;
-            }
-            if (properties.remove(key) != element) {
-                throw new ConcurrentModificationException(key);
+                property.setValue(element);
+                modCount++;
+                return previous;
             }
         }
         throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.IndexOutOfBounds_1,
index));
     }
 
     /**
-     * Ensures that the give element is an instance of the expected type.
-     * The caller shall ensure that the element is non-null before to invoke this method.
+     * Sets the property value, if no instance existed prior this method call.
      */
-    private void ensureValidType(final DefaultAttribute<?> element) {
-        if (element.getType() != type) {
-            throw new IllegalArgumentException(Errors.format(Errors.Keys.MismatchedPropertyType_1,
type.getName()));
+    @Override
+    public void add(final int index, final V element) {
+        if (index != 0) {
+            throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.IndexOutOfBounds_1,
index));
         }
+        add(element);
     }
 
     /**
-     * Sets the attribute associated to the key, if no instance existed prior this method
call.
+     * Sets the property value, if no instance existed prior this method call.
      */
     @Override
-    public void add(final int index, final DefaultAttribute<?> element) {
+    public boolean add(final V element) {
         ensureNonNull("element", element);
-        ensureValidType(element);
-        if (index == 0) {
-            if (properties.putIfAbsent(key, element) == null) {
-                modCount++;
-                return;
-            }
-            throw new IllegalStateException(Errors.format(Errors.Keys.ElementAlreadyPresent_1,
key));
+        if (property.getValue() == null) {
+            property.setValue(element);
+            modCount++;
+            return true;
         }
-        throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.IndexOutOfBounds_1,
index));
+        throw new IllegalStateException(Errors.format(Errors.Keys.ElementAlreadyPresent_1,
property.getName()));
     }
 
     /**
-     * Sets the attribute associated to the key, if no instance existed prior this method
call.
-     */
-    @Override
-    public boolean add(final DefaultAttribute<?> element) {
-        add(0, element);
-        return true;
-    }
-
-    /**
-     * Removes the attribute associated to the key.
+     * Removes the property value.
      *
      * This method does not checks if the removal is allowed by the
      * {@linkplain DefaultAttributeType#getMinimumOccurs() cardinality}.
      * Such check can be performed by {@link DefaultFeature#validate()}.
      */
     @Override
-    public DefaultAttribute<?> remove(final int index) {
+    public V remove(final int index) {
         if (index == 0) {
-            final Object previous = properties.remove(key);
+            final V previous = property.getValue();
             if (previous != null) {
-                modCount++;
-                return (DefaultAttribute<?>) previous;
+                clear();
+                return previous;
             }
         }
         throw new IndexOutOfBoundsException(Errors.format(Errors.Keys.IndexOutOfBounds_1,
index));
@@ -192,17 +155,16 @@ final class PropertySingleton extends Ab
      * This method is for {@link Iter#remove()} implementation only.
      *
      * @param  c The expected {@link #modCount} value, for check against concurrent modification.
-     * @return {@code true} if the value has been removed.
      */
-    final boolean clear(final int c) {
+    final void clear(final int c) {
         if (c != modCount) {
-            throw new ConcurrentModificationException(key);
+            throw new ConcurrentModificationException(String.valueOf(property.getName()));
         }
-        return properties.remove(key) != null;
+        property.setValue(null);
     }
 
     /**
-     * Removes the attribute associated to the key.
+     * Removes the property value.
      *
      * This method does not checks if the removal is allowed by the
      * {@linkplain DefaultAttributeType#getMinimumOccurs() cardinality}.
@@ -210,8 +172,8 @@ final class PropertySingleton extends Ab
      */
     @Override
     public void clear() {
+        property.setValue(null);
         modCount++;
-        properties.remove(key);
     }
 
     /**
@@ -219,26 +181,26 @@ final class PropertySingleton extends Ab
      */
     @Override
     public Object[] toArray() {
-        final Object element = properties.get(key);
-        return (element == null) ? EMPTY : new DefaultAttribute<?>[] {(DefaultAttribute<?>)
element};
+        final V element = property.getValue();
+        return (element == null) ? new Object[0] : new Object[] {element};
     }
 
     /**
      * Returns an iterator over the unique element in this list.
      */
     @Override
-    public Iterator<DefaultAttribute<?>> iterator() {
-        return new Iter((DefaultAttribute<?>) properties.get(key), modCount);
+    public Iterator<V> iterator() {
+        return new Iter(property.getValue(), modCount);
     }
 
     /**
      * Implementation of the iterator returned by {@link PropertySingleton#iterator()}.
      */
-    private final class Iter implements Iterator<DefaultAttribute<?>> {
+    private final class Iter implements Iterator<V> {
         /**
-         * The attribute to return, or {@code null} if we reached the iteration end.
+         * The property value to return, or {@code null} if we reached the iteration end.
          */
-        private DefaultAttribute<?> element;
+        private V element;
 
         /**
          * Initial {@link PropertySingleton#modCount} value, for checks against concurrent
modifications.
@@ -248,7 +210,7 @@ final class PropertySingleton extends Ab
         /**
          * Creates a new iterator which will return the given attribute.
          */
-        Iter(final DefaultAttribute<?> element, final int c) {
+        Iter(final V element, final int c) {
             this.element = element;
             this.c = c;
         }
@@ -262,11 +224,11 @@ final class PropertySingleton extends Ab
         }
 
         /**
-         * Returns the singleton attribute, if present.
+         * Returns the singleton value, if present.
          */
         @Override
-        public DefaultAttribute<?> next() {
-            final DefaultAttribute<?> v = element;
+        public V next() {
+            final V v = element;
             if (v == null) {
                 throw new NoSuchElementException();
             }
@@ -279,9 +241,10 @@ final class PropertySingleton extends Ab
          */
         @Override
         public void remove() {
-            if (element != null || !clear(c)) {
+            if (element != null) {
                 throw new IllegalStateException();
             }
+            clear(c);
         }
     }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/PropertySingletonTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/PropertySingletonTest.java?rev=1595029&r1=1595028&r2=1595029&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/PropertySingletonTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/PropertySingletonTest.java
[UTF-8] Thu May 15 20:12:44 2014
@@ -17,8 +17,6 @@
 package org.apache.sis.feature;
 
 import java.util.Set;
-import java.util.Map;
-import java.util.HashMap;
 import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
@@ -41,39 +39,22 @@ import static org.apache.sis.test.TestUt
 @DependsOn(DefaultAttributeTest.class)
 public final strictfp class PropertySingletonTest extends TestCase {
     /**
-     * The key used for storing value in this test class.
-     */
-    private static final String KEY = "test key";
-
-    /**
      * The instance to test.
      */
-    private final PropertySingleton singleton;
-
-    /**
-     * The type of the attribute value in the {@link #singleton} list.
-     */
-    private final DefaultAttributeType<Integer> attributeType;
-
-    /**
-     * The map of properties given to the {@link #singleton} instance to test.
-     */
-    private final Map<String, Object> properties;
+    private final PropertySingleton<Integer> singleton;
 
     /**
-     * Arbitrary values added to {@link #properties} for making sure
+     * The attribute wrapped by the {@link #singleton} list.
      */
-    private final Map<String,?> otherValues;
+    private final DefaultAttribute<Integer> attribute;
 
     /**
      * Creates a new test case.
      */
     public PropertySingletonTest() {
-        otherValues   = singletonMap("other key", "other value");
-        properties    = new HashMap<>(otherValues);
-        attributeType = new DefaultAttributeType<>(singletonMap(DefaultAttributeType.NAME_KEY,
KEY),
-                                Integer.class, 0, 1, null);
-        singleton = new PropertySingleton(attributeType, properties, KEY);
+        attribute = new DefaultAttribute<>(new DefaultAttributeType<>(
+                singletonMap(DefaultAttributeType.NAME_KEY, "test"), Integer.class, 0, 1,
null));
+        singleton = new PropertySingleton<>(attribute);
     }
 
     /**
@@ -98,7 +79,6 @@ public final strictfp class PropertySing
         } catch (IndexOutOfBoundsException e) {
             assertNotNull(e.getMessage());
         }
-        assertEquals("Other values shall be unmodified.", otherValues, properties);
     }
 
     /**
@@ -107,10 +87,8 @@ public final strictfp class PropertySing
     @Test
     @DependsOnMethod("testEmpty")
     public void testSingleton() {
-        final DefaultAttribute<Integer> a1 = new DefaultAttribute<>(attributeType);
-        final DefaultAttribute<Integer> a2 = new DefaultAttribute<>(attributeType);
-        a1.setValue(1000);
-        a2.setValue(2000);
+        final Integer a1 = 1000;
+        final Integer a2 = 2000;
         assertEquals("indexOf",  -1, singleton.indexOf(a1));
         assertTrue  ("add",          singleton.add(a1));
         assertEquals("size",      1, singleton.size());
@@ -121,11 +99,10 @@ public final strictfp class PropertySing
         assertSame  ("set",      a1, singleton.set(0, a2));
         assertSame  ("get",      a2, singleton.get(0));
         assertSame  ("iterator", a2, getSingleton(singleton));
-        assertArrayEquals("toArray", new DefaultAttribute<?>[] {a2}, singleton.toArray());
+        assertArrayEquals("toArray", new Object[] {a2}, singleton.toArray());
 
         assertSame  ("remove",   a2, singleton.remove(0));
         assertEquals("size",      0, singleton.size());
-        assertEquals("Other values shall be unmodified.", otherValues, properties);
     }
 
     /**
@@ -134,15 +111,15 @@ public final strictfp class PropertySing
     @Test
     @DependsOnMethod("testSingleton")
     public void testMaximumOccurrence() {
-        final DefaultAttribute<Integer> a1 = new DefaultAttribute<>(attributeType);
-        final DefaultAttribute<Integer> a2 = new DefaultAttribute<>(attributeType);
+        final Integer a1 = 1000;
+        final Integer a2 = 2000;
         assertTrue("add", singleton.add(a1));
         try {
             assertTrue("add", singleton.add(a2));
             fail("Shall not be allowed to add more than 1 element.");
         } catch (IllegalStateException e) {
             final String message = e.getMessage();
-            assertTrue(message, message.contains(KEY));
+            assertTrue(message, message.contains("test"));
         }
     }
 
@@ -152,26 +129,10 @@ public final strictfp class PropertySing
     @Test
     @DependsOnMethod("testSingleton")
     public void testRemoveAll() {
-        final Set<DefaultAttribute<Integer>> attributes = singleton(new DefaultAttribute<>(attributeType));
+        final Set<Integer> attributes = singleton(1000);
         assertTrue (singleton.addAll(attributes));
         assertFalse(singleton.isEmpty());
         assertTrue (singleton.removeAll(attributes));
         assertTrue (singleton.isEmpty());
     }
-
-    /**
-     * Tests the attempt to add an attribute of the wrong type.
-     * {@link PropertySingleton} shall not allow this operation.
-     */
-    @Test
-    @DependsOnMethod("testSingleton")
-    public void testAddWrongType() {
-        final DefaultAttribute<Integer> a1 = DefaultAttributeTest.population();
-        try {
-            singleton.add(a1);
-        } catch (IllegalArgumentException e) {
-            final String message = e.getMessage();
-            assertTrue(message, message.contains(KEY));
-        }
-    }
 }



Mime
View raw message