You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by no...@apache.org on 2009/08/11 15:32:06 UTC

svn commit: r803112 - in /incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter: MapAdapter.java SetAdapter.java

Author: noelgrandin
Date: Tue Aug 11 13:32:06 2009
New Revision: 803112

URL: http://svn.apache.org/viewvc?rev=803112&view=rev
Log:
(PIVOT-224) Support comparators in MapAdapter and SetAdapter

Modified:
    incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/MapAdapter.java
    incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/SetAdapter.java

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/MapAdapter.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/MapAdapter.java?rev=803112&r1=803111&r2=803112&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/MapAdapter.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/MapAdapter.java Tue Aug 11 13:32:06 2009
@@ -17,18 +17,20 @@
 package org.apache.pivot.collections.adapter;
 
 import java.io.Serializable;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.util.Comparator;
 import java.util.Iterator;
+import java.util.SortedMap;
 
 import org.apache.pivot.collections.Map;
 import org.apache.pivot.collections.MapListener;
 import org.apache.pivot.util.ImmutableIterator;
 import org.apache.pivot.util.ListenerList;
 
-
 /**
- * Implementation of the {@link Map} interface that is backed by an
- * instance of <tt>java.util.Map</tt>.
+ * Implementation of the {@link Map} interface that is backed by an instance of
+ * <tt>java.util.Map</tt>.
  */
 public class MapAdapter<K, V> implements Map<K, V>, Serializable {
     private static final long serialVersionUID = 0;
@@ -58,8 +60,7 @@
 
         if (update) {
             mapListeners.valueUpdated(this, key, previousValue);
-        }
-        else {
+        } else {
             mapListeners.valueAdded(this, key);
         }
 
@@ -88,7 +89,6 @@
         return map.containsKey(key);
     }
 
-
     public boolean isEmpty() {
         return map.isEmpty();
     }
@@ -97,11 +97,40 @@
         return map.size();
     }
 
+    @SuppressWarnings("unchecked")
     public Comparator<K> getComparator() {
+        if (this.map instanceof SortedMap) {
+            return ((SortedMap) this.map).comparator();
+        }
         return null;
     }
 
+    @SuppressWarnings("unchecked")
     public void setComparator(Comparator<K> comparator) {
+        // If the adapted map supports it, implement setComparator by
+        // constructing a new map
+        if (this.map instanceof SortedMap) {
+            try {
+                Constructor constructor = this.map.getClass().getConstructor(Comparator.class);
+                if (constructor != null) {
+                    java.util.Map<K, V> map = (java.util.Map) constructor.newInstance(comparator);
+                    map.putAll(this.map);
+                    this.map = map;
+                }
+            } catch (SecurityException exception) {
+                throw new RuntimeException(exception);
+            } catch (NoSuchMethodException exception) {
+                throw new RuntimeException(exception);
+            } catch (IllegalArgumentException exception) {
+                throw new RuntimeException(exception);
+            } catch (InstantiationException exception) {
+                throw new RuntimeException(exception);
+            } catch (IllegalAccessException exception) {
+                throw new RuntimeException(exception);
+            } catch (InvocationTargetException exception) {
+                throw new RuntimeException(exception);
+            }
+        }
         throw new UnsupportedOperationException();
     }
 

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/SetAdapter.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/SetAdapter.java?rev=803112&r1=803111&r2=803112&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/SetAdapter.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/collections/adapter/SetAdapter.java Tue Aug 11 13:32:06 2009
@@ -17,6 +17,8 @@
 package org.apache.pivot.collections.adapter;
 
 import java.io.Serializable;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.util.Comparator;
 import java.util.Iterator;
 
@@ -25,10 +27,9 @@
 import org.apache.pivot.util.ImmutableIterator;
 import org.apache.pivot.util.ListenerList;
 
-
 /**
- * Implementation of the {@link Set} interface that is backed by an
- * instance of <tt>java.util.Set</tt>.
+ * Implementation of the {@link Set} interface that is backed by an instance of
+ * <tt>java.util.Set</tt>.
  */
 public class SetAdapter<E> implements Set<E>, Serializable {
     private static final long serialVersionUID = 0;
@@ -93,11 +94,40 @@
         return set.size();
     }
 
+    @SuppressWarnings("unchecked")
     public Comparator<E> getComparator() {
+        if (this.set instanceof java.util.SortedSet) {
+            return ((java.util.SortedSet) this.set).comparator();
+        }
         return null;
     }
 
+    @SuppressWarnings("unchecked")
     public void setComparator(Comparator<E> comparator) {
+        // If the adapted set supports it, implement setComparator by
+        // constructing a new set
+        if (this.set instanceof java.util.SortedSet) {
+            try {
+                Constructor constructor = this.set.getClass().getConstructor(Comparator.class);
+                if (constructor != null) {
+                    java.util.SortedSet<E> set = (java.util.SortedSet) constructor.newInstance(comparator);
+                    set.addAll(this.set);
+                    this.set = set;
+                }
+            } catch (SecurityException exception) {
+                throw new RuntimeException(exception);
+            } catch (NoSuchMethodException exception) {
+                throw new RuntimeException(exception);
+            } catch (IllegalArgumentException exception) {
+                throw new RuntimeException(exception);
+            } catch (InstantiationException exception) {
+                throw new RuntimeException(exception);
+            } catch (IllegalAccessException exception) {
+                throw new UnsupportedOperationException(exception);
+            } catch (InvocationTargetException exception) {
+                throw new RuntimeException(exception);
+            }
+        }
         throw new UnsupportedOperationException();
     }