sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1398623 - in /sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util: collection/ resources/
Date Tue, 16 Oct 2012 02:41:42 GMT
Author: desruisseaux
Date: Tue Oct 16 02:41:42 2012
New Revision: 1398623

URL: http://svn.apache.org/viewvc?rev=1398623&view=rev
Log:
Fixed an other safety hole: the 'ListIterator.set|add' methods need to check element validity.
Since this addition make the method body bigger, abandon the tabular formatting; reformat
the inner classes in the classical way.

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedArrayList.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedHashMap.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedHashSet.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedArrayList.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedArrayList.java?rev=1398623&r1=1398622&r2=1398623&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedArrayList.java
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedArrayList.java
Tue Oct 16 02:41:42 2012
@@ -22,6 +22,7 @@ import java.util.ListIterator;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.NoSuchElementException;
 import net.jcip.annotations.ThreadSafe;
 import org.apache.sis.util.Decorator;
 import org.apache.sis.util.resources.Errors;
@@ -169,29 +170,113 @@ public class CheckedArrayList<E> extends
 
     /**
      * A synchronized iterator with a check for write permission prior element removal.
+     * This class wraps the iterator provided by {@link ArrayList#iterator()}, and is
+     * also the base class for the wrapper around {@link ArrayList#listIterator()}.
+     *
+     * @see CheckedArrayList#iterator()
      */
     @ThreadSafe
     @Decorator(Iterator.class)
-    private class Iter<E,I extends Iterator<E>> implements Iterator<E>
{
-        final I iterator;
-        Iter(final I iterator)                   {this.iterator = iterator;}
-        @Override public final boolean hasNext() {synchronized (getLock()) {return      
           iterator.hasNext();}}
-        @Override public final E       next()    {synchronized (getLock()) {return      
           iterator.next();}}
-        @Override public final void    remove()  {synchronized (getLock()) {checkWritePermission();
iterator.remove();}}
+    private class Iter<I extends Iterator<E>> implements Iterator<E> {
+        /** The {@link ArrayList} iterator. */
+        protected final I iterator;
+
+        /** Creates a new wrapper for the given {@link ArrayList} iterator. */
+        Iter(final I iterator) {
+            this.iterator = iterator;
+        }
+
+        /** Returns {@code true} if there is more elements in the iteration. */
+        @Override
+        public final boolean hasNext() {
+            synchronized (getLock()) {
+                return iterator.hasNext();
+            }
+        }
+
+        /** Returns the next element in the iteration. */
+        @Override
+        public final E next() throws NoSuchElementException {
+            synchronized (getLock()) {
+                return iterator.next();
+            }
+        }
+
+        /** Removes the previous element if the enclosing {@link CheckedArrayList} allows
write operations. */
+        @Override
+        public final void remove() throws UnsupportedOperationException {
+            synchronized (getLock()) {
+                checkWritePermission();
+                iterator.remove();
+            }
+        }
     }
 
     /**
      * A synchronized list iterator with a check for write permission prior element removal.
+     * This class wraps the iterator provided by {@link ArrayList#listIterator()}.
+     *
+     * @see CheckedArrayList#listIterator()
+     * @see CheckedArrayList#listIterator(int)
      */
     @Decorator(ListIterator.class)
-    private class ListIter<E> extends Iter<E,ListIterator<E>> implements
ListIterator<E> {
-        ListIter(final ListIterator<E> iterator) {super(iterator);}
-        @Override public int     nextIndex()     {synchronized (getLock()) {return      
           iterator.nextIndex();}}
-        @Override public int     previousIndex() {synchronized (getLock()) {return      
           iterator.previousIndex();}}
-        @Override public boolean hasPrevious()   {synchronized (getLock()) {return      
           iterator.hasPrevious();}}
-        @Override public E       previous()      {synchronized (getLock()) {return      
           iterator.previous();}}
-        @Override public void    set(final E e)  {synchronized (getLock()) {checkWritePermission();
iterator.set(e);}}
-        @Override public void    add(final E e)  {synchronized (getLock()) {checkWritePermission();
iterator.add(e);}}
+    private class ListIter extends Iter<ListIterator<E>> implements ListIterator<E>
{
+        /** Creates a new wrapper for the given {@link ArrayList} list iterator. */
+        ListIter(final ListIterator<E> iterator) {
+            super(iterator);
+        }
+
+        /** Returns the index of the element to be returned by {@link #next()}. */
+        @Override
+        public int nextIndex() {
+            synchronized (getLock()) {
+                return iterator.nextIndex();
+            }
+        }
+
+        /** Returns the index of the element to be returned by {@link #previous()}. */
+        @Override
+        public int previousIndex() {
+            synchronized (getLock()) {
+                return iterator.previousIndex();
+            }
+        }
+
+        /** Returns {@code true} if there is elements before current position. */
+        @Override
+        public boolean hasPrevious() {
+            synchronized (getLock()) {
+                return iterator.hasPrevious();
+            }
+        }
+
+        /** Returns the previous element in the iteration. */
+        @Override
+        public E previous() throws NoSuchElementException {
+            synchronized (getLock()) {
+                return iterator.previous();
+            }
+        }
+
+        /** See the {@link CheckedArrayList#set(int, Object)} method contract. */
+        @Override
+        public void set(final E element) throws IllegalArgumentException, UnsupportedOperationException
{
+            ensureValid(element);
+            synchronized (getLock()) {
+                checkWritePermission();
+                iterator.set(element);
+            }
+        }
+
+        /** See the {@link CheckedArrayList#add(Object)} method contract. */
+        @Override
+        public void add(final E element) throws IllegalArgumentException, UnsupportedOperationException
{
+            ensureValid(element);
+            synchronized (getLock()) {
+                checkWritePermission();
+                iterator.add(element);
+            }
+        }
     }
 
     /**
@@ -215,7 +300,7 @@ public class CheckedArrayList<E> extends
     @Override
     public ListIterator<E> listIterator() {
         synchronized (getLock()) {
-            return new ListIter<>(super.listIterator());
+            return new ListIter(super.listIterator());
         }
     }
 
@@ -228,7 +313,7 @@ public class CheckedArrayList<E> extends
     @Override
     public ListIterator<E> listIterator(final int index) {
         synchronized (getLock()) {
-            return new ListIter<>(super.listIterator(index));
+            return new ListIter(super.listIterator(index));
         }
     }
 

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedHashMap.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedHashMap.java?rev=1398623&r1=1398622&r2=1398623&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedHashMap.java
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedHashMap.java
Tue Oct 16 02:41:42 2012
@@ -22,6 +22,7 @@ import java.util.Iterator;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashMap;
+import java.util.NoSuchElementException;
 import net.jcip.annotations.ThreadSafe;
 import org.apache.sis.util.Decorator;
 import org.apache.sis.util.resources.Errors;
@@ -344,15 +345,43 @@ public class CheckedHashMap<K,V> extends
 
     /**
      * A synchronized iterator with a check for write permission prior element removal.
+     * This class wraps the iterator provided by the {@link LinkedHashMap} views.
      */
     @ThreadSafe
     @Decorator(Iterator.class)
     private final class Iter<E> implements Iterator<E> {
-        final Iterator<E> iterator;
-        Iter(final Iterator<E> iterator)   {this.iterator = iterator;}
-        @Override public boolean hasNext() {synchronized (getLock()) {return            
     iterator.hasNext();}}
-        @Override public E       next()    {synchronized (getLock()) {return            
     iterator.next();}}
-        @Override public void    remove()  {synchronized (getLock()) {checkWritePermission();
iterator.remove();}}
+        /** The {@link LinkedHashMap} iterator. */
+        private final Iterator<E> iterator;
+
+        /** Creates a new wrapper for the given {@link LinkedHashMap} iterator. */
+        Iter(final Iterator<E> iterator) {
+            this.iterator = iterator;
+        }
+
+        /** Returns {@code true} if there is more elements in the iteration. */
+        @Override
+        public boolean hasNext() {
+            synchronized (getLock()) {
+                return iterator.hasNext();
+            }
+        }
+
+        /** Returns the next element in the iteration. */
+        @Override
+        public E next() throws NoSuchElementException {
+            synchronized (getLock()) {
+                return iterator.next();
+            }
+        }
+
+        /** Removes the previous element if the enclosing {@link CheckedHashMap} allows write
operations. */
+        @Override
+        public void remove() throws UnsupportedOperationException {
+            synchronized (getLock()) {
+                checkWritePermission();
+                iterator.remove();
+            }
+        }
     }
 
     /**
@@ -363,34 +392,141 @@ public class CheckedHashMap<K,V> extends
     @ThreadSafe
     @Decorator(Collection.class)
     private class Sync<E> implements Collection<E> {
-        /**
-         * The {@link Map#keySet()}, {@link Map#values()} or {@link Map#entrySet()} view.
-         */
+        /** The {@link Map#keySet()}, {@link Map#values()} or {@link Map#entrySet()} view.
*/
         private final Collection<E> view;
 
-        /**
-         * Create a new synchronized wrapper for the given view.
-         */
+        /** Create a new synchronized wrapper for the given view. */
         Sync(final Collection<E> view) {
             this.view = view;
         }
 
-        @Override public final void     clear()                           {synchronized (getLock())
{                               view.clear      ( );}}
-        @Override public final int      size()                            {synchronized (getLock())
{                        return view.size       ( );}}
-        @Override public final boolean  isEmpty()                         {synchronized (getLock())
{                        return view.isEmpty    ( );}}
-        @Override public final boolean  contains(Object o)                {synchronized (getLock())
{                        return view.contains   (o);}}
-        @Override public final boolean  containsAll(Collection<?> c)      {synchronized
(getLock()) {                        return view.containsAll(c);}}
-        @Override public final boolean  add(E e)                          {synchronized (getLock())
{checkWritePermission(); return view.add        (e);}}
-        @Override public final boolean  addAll(Collection<? extends E> c) {synchronized
(getLock()) {checkWritePermission(); return view.addAll     (c);}}
-        @Override public final boolean  remove(Object o)                  {synchronized (getLock())
{checkWritePermission(); return view.remove     (o);}}
-        @Override public final boolean  removeAll(Collection<?> c)        {synchronized
(getLock()) {checkWritePermission(); return view.removeAll  (c);}}
-        @Override public final boolean  retainAll(Collection<?> c)        {synchronized
(getLock()) {checkWritePermission(); return view.retainAll  (c);}}
-        @Override public final Object[] toArray()                         {synchronized (getLock())
{                        return view.toArray    ( );}}
-        @Override public final <T> T[]  toArray(T[] a)                    {synchronized
(getLock()) {                        return view.toArray    (a);}}
-        @Override public final String   toString()                        {synchronized (getLock())
{                        return view.toString   ( );}}
-        @Override public final boolean  equals(Object o)                  {synchronized (getLock())
{                        return view.equals     (o);}}
-        @Override public final int      hashCode()                        {synchronized (getLock())
{                        return view.hashCode   ( );}}
-        @Override public final Iterator<E> iterator()                     {synchronized
(getLock()) {return new Iter<>(view.iterator());}}
+        /** Returns a synchronized and checked iterator over the elements in this collection.
*/
+        @Override
+        public final Iterator<E> iterator() {
+            synchronized (getLock()) {
+                return new Iter<>(view.iterator());
+            }
+        }
+
+        /** Returns the number of elements in the collection. */
+        @Override
+        public final int size() {
+            synchronized (getLock()) {
+                return view.size();
+            }
+        }
+
+        /** Returns {@code true} if the collection is empty. */
+        @Override
+        public final boolean isEmpty() {
+            synchronized (getLock()) {
+                return view.isEmpty();
+            }
+        }
+
+        /** Returns {@code true} if the collection contains the given element. */
+        @Override
+        public final boolean contains(final Object element) {
+            synchronized (getLock()) {
+                return view.contains(element);
+            }
+        }
+
+        /** Returns {@code true} if the collection contains all elements of the given collection.
*/
+        @Override
+        public final boolean containsAll(final Collection<?> collection) {
+            synchronized (getLock()) {
+                return view.containsAll(collection);
+            }
+        }
+
+        /** Always unsupported operation in hash map views. */
+        @Override
+        public final boolean add(final E element) throws UnsupportedOperationException {
+            throw new UnsupportedOperationException(Errors.format(Errors.Keys.UnsupportedOperation_1,
"add"));
+        }
+
+        /** Always unsupported operation in hash map views. */
+        @Override
+        public final boolean addAll(final Collection<? extends E> collection) throws
UnsupportedOperationException {
+            throw new UnsupportedOperationException(Errors.format(Errors.Keys.UnsupportedOperation_1,
"addAll"));
+        }
+
+        /** Remove the given element if the enclosing {@link CheckedHashMap} supports write
operations. */
+        @Override
+        public final boolean remove(final Object element) throws UnsupportedOperationException
{
+            synchronized (getLock()) {
+                checkWritePermission();
+                return view.remove(element);
+            }
+        }
+
+        /** Remove the given elements if the enclosing {@link CheckedHashMap} supports write
operations. */
+        @Override
+        public final boolean removeAll(final Collection<?> collection) throws UnsupportedOperationException
{
+            synchronized (getLock()) {
+                checkWritePermission();
+                return view.removeAll(collection);
+            }
+        }
+
+        /** Retains only the given elements if the enclosing {@link CheckedHashMap} supports
write operations. */
+        @Override
+        public final boolean retainAll(final Collection<?> collection) throws UnsupportedOperationException
{
+            synchronized (getLock()) {
+                checkWritePermission();
+                return view.retainAll(collection);
+            }
+        }
+
+        /** Removes all elements from the collection. */
+        @Override
+        public final void clear() throws UnsupportedOperationException {
+            synchronized (getLock()) {
+                checkWritePermission();
+                view.clear();
+            }
+        }
+
+        /** Returns the elements in an array. */
+        @Override
+        public final Object[] toArray() {
+            synchronized (getLock()) {
+                return view.toArray();
+            }
+        }
+
+        /** Returns the elements in an array. */
+        @Override
+        public final <T> T[] toArray(final T[] array) {
+            synchronized (getLock()) {
+                return view.toArray(array);
+            }
+        }
+
+        /** Returns a string representation of the elements. */
+        @Override
+        public final String toString() {
+            synchronized (getLock()) {
+                return view.toString();
+            }
+        }
+
+        /** Compare this collection with the given object for equality. */
+        @Override
+        public final boolean equals(final Object other) {
+            synchronized (getLock()) {
+                return view.equals(other);
+            }
+        }
+
+        /** Returns a hash code value for this collection. */
+        @Override
+        public final int hashCode() {
+            synchronized (getLock()) {
+                return view.hashCode();
+            }
+        }
     }
 
     /**
@@ -399,9 +535,7 @@ public class CheckedHashMap<K,V> extends
      */
     @Decorator(Set.class)
     private final class SyncSet<E> extends Sync<E> implements Set<E> {
-        /**
-         * Create a new synchronized wrapper for the given view.
-         */
+        /** Create a new synchronized wrapper for the given view. */
         SyncSet(final Set<E> set) {
             super(set);
         }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedHashSet.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedHashSet.java?rev=1398623&r1=1398622&r2=1398623&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedHashSet.java
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/CheckedHashSet.java
Tue Oct 16 02:41:42 2012
@@ -21,6 +21,7 @@ import java.util.Iterator;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashSet;
+import java.util.NoSuchElementException;
 import net.jcip.annotations.ThreadSafe;
 import org.apache.sis.util.Decorator;
 import org.apache.sis.util.resources.Errors;
@@ -168,15 +169,45 @@ public class CheckedHashSet<E> extends L
 
     /**
      * A synchronized iterator with a check for write permission prior element removal.
+     * This class wraps the iterator provided by {@link LinkedHashSet#iterator()}.
+     *
+     * @see CheckedHashSet#iterator()
      */
     @ThreadSafe
     @Decorator(Iterator.class)
-    private final class Iter<E> implements Iterator<E> {
-        final Iterator<E> iterator;
-        Iter(final Iterator<E> iterator)   {this.iterator = iterator;}
-        @Override public boolean hasNext() {synchronized (getLock()) {return            
     iterator.hasNext();}}
-        @Override public E       next()    {synchronized (getLock()) {return            
     iterator.next();}}
-        @Override public void    remove()  {synchronized (getLock()) {checkWritePermission();
iterator.remove();}}
+    private final class Iter implements Iterator<E> {
+        /** The {@link LinkedHashSet} iterator. */
+        private final Iterator<E> iterator;
+
+        /** Creates a new wrapper for the given {@link LinkedHashSet} iterator. */
+        Iter(final Iterator<E> iterator) {
+            this.iterator = iterator;
+        }
+
+        /** Returns {@code true} if there is more elements in the iteration. */
+        @Override
+        public boolean hasNext() {
+            synchronized (getLock()) {
+                return iterator.hasNext();
+            }
+        }
+
+        /** Returns the next element in the iteration. */
+        @Override
+        public E next() throws NoSuchElementException {
+            synchronized (getLock()) {
+                return iterator.next();
+            }
+        }
+
+        /** Removes the previous element if the enclosing {@link CheckedHashSet} allows write
operations. */
+        @Override
+        public void remove() throws UnsupportedOperationException {
+            synchronized (getLock()) {
+                checkWritePermission();
+                iterator.remove();
+            }
+        }
     }
 
     /**
@@ -187,7 +218,7 @@ public class CheckedHashSet<E> extends L
     @Override
     public Iterator<E> iterator() {
         synchronized (getLock()) {
-            return new Iter<>(super.iterator());
+            return new Iter(super.iterator());
         }
     }
 

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java?rev=1398623&r1=1398622&r2=1398623&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
Tue Oct 16 02:41:42 2012
@@ -132,6 +132,11 @@ public final class Errors extends Indexe
         public static final int UnexpectedArgumentDimension_3 = 5;
 
         /**
+         * The ‘{0}’ operation is unsupported.
+         */
+        public static final int UnsupportedOperation_1 = 20;
+
+        /**
          * A value is already defined for “{0}”.
          */
         public static final int ValueAlreadyDefined_1 = 13;

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties?rev=1398623&r1=1398622&r2=1398623&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
Tue Oct 16 02:41:42 2012
@@ -31,6 +31,7 @@ NotAPrimitiveWrapper_1          = Class 
 NullArgument_1                  = Argument \u2018{0}\u2019 shall not be null.
 RecursiveCreateCallForKey_1     = Recursive call while creating an object for the \u201c{0}\u201d
key.
 UnexpectedArgumentDimension_3   = Argument \u2018{0}\u2019 has {1} dimensions, while {2}
was expected.
+UnsupportedOperation_1          = The \u2018{0}\u2019 operation is unsupported.
 ValueAlreadyDefined_1           = A value is already defined for \u201c{0}\u201d.
 ValueNotGreaterThanZero_2       = Value \u2018{0}\u2019={1} is invalid. Expected a number
greater than 0.
 ValueOutOfRange_4               = Value \u2018{0}\u2019={1} is invalid. Expected a value
in the [{2} \u2026 {3}] range.

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties?rev=1398623&r1=1398622&r2=1398623&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
Tue Oct 16 02:41:42 2012
@@ -31,6 +31,7 @@ NotAPrimitiveWrapper_1          = La cla
 NullArgument_1                  = L\u2019argument \u2018{0}\u2019 ne doit pas \u00eatre nul.
 RecursiveCreateCallForKey_1     = Appel r\u00e9cursif lors de la cr\u00e9ation d\u2019un
objet pour la cl\u00e9 \u201c{0}\u201d.
 UnexpectedArgumentDimension_3   = L\u2019argument \u2018{0}\u2019 a {1} dimensions, alors
qu\u2019on en attendait {2}.
+UnsupportedOperation_1          = L\u2019op\u00e9ration \u2018{0}\u2019 n\u2019est pas support\u00e9e.
 ValueAlreadyDefined_1           = Une valeur est d\u00e9j\u00e0 d\u00e9finie pour \u201c{0}\u201d.
 ValueNotGreaterThanZero_2       = La valeur \u2018{0}\u2019={1} n\u2019est pas valide. On
attendait un nombre positif non-nul.
 ValueOutOfRange_4               = La valeur \u2018{0}\u2019={1} est invalide. Une valeur
dans la plage [{2} \u2026 {3}] \u00e9tait attendue.



Mime
View raw message