[portal-commits] JBoss Portal SVN: r8947 - in modules/common/trunk/common/src: main/java/org/jboss/portal/common/value and 2 other directories.

portal-commits at lists.jboss.org portal-commits at lists.jboss.org
Thu Nov 15 10:54:55 EST 2007


Author: julien at jboss.com
Date: 2007-11-15 10:54:55 -0500 (Thu, 15 Nov 2007)
New Revision: 8947

Added:
   modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SimpleMapAccessor.java
   modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/TypedMapTestCase.java
Removed:
   modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/TypedMapTestCase.java
Modified:
   modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapAccessor.java
   modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java
   modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/TypedMap.java
   modules/common/trunk/common/src/main/java/org/jboss/portal/common/value/Value.java
   modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ParameterMapTestCase.java
Log:
generified TypedMap + improved it

Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapAccessor.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapAccessor.java	2007-11-15 14:49:38 UTC (rev 8946)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/MapAccessor.java	2007-11-15 15:54:55 UTC (rev 8947)
@@ -30,14 +30,14 @@
  * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
  * @version $Revision: 7228 $
  */
-public interface MapAccessor
+public interface MapAccessor<K, V>
 {
    /**
     * Return the accessed map.
     *
-    * @param requestWriteable whether or not the Map will be accessed for a write-type (e.g., put, remove...) operation.
+    * @param writable whether or not the Map will be accessed for a write-type (e.g., put, remove...) operation.
     *                         This allows for lazy initialization via a create-on-write mechanism.
     * @return a non null map
     */
-   Map getMap(boolean requestWriteable);
+   Map<K, V> getMap(boolean writable);
 }

Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java	2007-11-15 14:49:38 UTC (rev 8946)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/ParameterMap.java	2007-11-15 15:54:55 UTC (rev 8947)
@@ -32,44 +32,67 @@
  * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
  * @version $Revision: 6671 $
  */
-public class ParameterMap extends TypedMap
+public class ParameterMap extends TypedMap<String, String[], String, String[]>
 {
 
-   /** If true returned values will be cloned when returned to the client. */
-   private boolean cloneInternalValue;
+   /** . */
+   private static final KeyConverter keyConv = new KeyConverter();
 
-   /** If true returned values will be cloned when provided by the client. */
-   private boolean cloneExternalValue;
+   /** . */
+   private static final ValueConverter valueConv1 = new ValueConverter(false, false);
 
-   public ParameterMap(boolean cloneInternalValue, boolean cloneExternalValue)
+   /** . */
+   private static final ValueConverter valueConv2 = new ValueConverter(false, true);
+
+   /** . */
+   private static final ValueConverter valueConv3 = new ValueConverter(true, false);
+
+   /** . */
+   private static final ValueConverter valueConv4 = new ValueConverter(true, true);
+
+   private static ValueConverter getValueConverter(boolean cloneInternalValue, boolean cloneExternalValue)
    {
-      this(new HashMap(), cloneInternalValue, cloneExternalValue);
+      if (cloneInternalValue)
+      {
+         if (cloneExternalValue)
+         {
+            return valueConv4;
+         }
+         else
+         {
+            return valueConv3;
+         }
+      }
+      else
+      {
+         if (cloneExternalValue)
+         {
+            return valueConv2;
+         }
+         else
+         {
+            return valueConv1;
+         }
+      }
    }
 
-   public ParameterMap(MapAccessor accessor, boolean cloneInternalValue, boolean cloneExternalValue)
-   {
-      super(accessor);
+   /** . */
+   private final boolean cloneInternalValue;
 
-      //
-      this.cloneInternalValue = cloneInternalValue;
-      this.cloneExternalValue = cloneExternalValue;
-   }
+   /** . */
+   private final boolean cloneExternalValue;
 
-   public ParameterMap(Map delegate, boolean cloneInternalValue, boolean cloneExternalValue)
+   public ParameterMap(boolean cloneInternalValue, boolean cloneExternalValue)
    {
-      super(delegate);
-
-      //
-      this.cloneInternalValue = cloneInternalValue;
-      this.cloneExternalValue = cloneExternalValue;
+      this(new HashMap<String, String[]>(), cloneInternalValue, cloneExternalValue);
    }
 
-   public ParameterMap(MapAccessor accessor)
+   public ParameterMap(MapAccessor<String, String[]> accessor)
    {
       this(accessor, false, false);
    }
 
-   public ParameterMap(Map delegate)
+   public ParameterMap(Map<String, String[]> delegate)
    {
       this(delegate, false, false);
    }
@@ -79,6 +102,24 @@
       this(false, false);
    }
 
+   public ParameterMap(MapAccessor<String, String[]> accessor, boolean cloneInternalValue, boolean cloneExternalValue)
+   {
+      super(accessor, keyConv, getValueConverter(cloneInternalValue, cloneExternalValue));
+
+      //
+      this.cloneInternalValue = cloneInternalValue;
+      this.cloneExternalValue = cloneExternalValue;
+   }
+
+   public ParameterMap(Map<String, String[]> delegate, boolean cloneInternalValue, boolean cloneExternalValue)
+   {
+      super(delegate, keyConv, getValueConverter(cloneInternalValue, cloneExternalValue));
+
+      //
+      this.cloneInternalValue = cloneInternalValue;
+      this.cloneExternalValue = cloneExternalValue;
+   }
+
    /**
     * Return true if values returned by the map are cloned.
     *
@@ -89,79 +130,94 @@
       return cloneInternalValue;
    }
 
-   /**
-    * Only accept non null string objects.
-    *
-    * @throws ClassCastException if the value is not an instance of string
-    */
-   protected void assertKeyValidity(Object value) throws ClassCastException
+   public boolean isCloneExternalValue()
    {
-      if (value instanceof String == false)
-      {
-         throw new ClassCastException("Key should be a String");
-      }
+      return cloneExternalValue;
    }
 
-   /**
-    * Only check are made to the value. The only valid values accepted
-    * are string arrays with non zero length and containing non null
-    * values.
-    *
-    * @throws NullPointerException if the value is null
-    * @throws ClassCastException if the value type is not an array of string
-    * @throws IllegalArgumentException if the array length is zero or one of the array value is null
-    */
-   protected Object getInternalValue(Object value) throws NullPointerException, ClassCastException, IllegalArgumentException
+   private static class KeyConverter extends Converter<String, String>
    {
-      if (value instanceof String[] == false)
+      protected String getInternal(String external) throws IllegalArgumentException, ClassCastException
       {
-         throw new ClassCastException("Value should be a String[]");
+         return external;
       }
-      String[] strings = (String[])value;
-      if (strings.length == 0)
+      protected String getExternal(String internal)
       {
-         throw new IllegalArgumentException("Array must not be zero length");
+         return internal;
       }
-
-      //
-      for (int i = strings.length - 1;i >= 0;i--)
+      protected boolean equals(String left, String right)
       {
-         if (strings[i] == null)
-         {
-            throw new IllegalArgumentException("No null entries allowed in String[]");
-         }
+         return left.equals(right);
       }
+   }
 
-      //
-      if (cloneExternalValue)
+   private static class ValueConverter extends Converter<String[], String[]>
+   {
+
+      /** . */
+      private final boolean cloneInternalValue;
+
+      /** . */
+      private final boolean cloneExternalValue;
+
+      private ValueConverter(boolean cloneInternalValue, boolean cloneExternalValue)
       {
-         strings = (String[])strings.clone();
+         this.cloneInternalValue = cloneInternalValue;
+         this.cloneExternalValue = cloneExternalValue;
       }
 
-      //
-      return strings;
-   }
+      /**
+       * Only check are made to the value. The only valid values accepted
+       * are string arrays with non zero length and containing non null
+       * values.
+       *
+       * @param external
+       * @return
+       * @throws NullPointerException if the value is null
+       * @throws ClassCastException if the value type is not an array of string
+       * @throws IllegalArgumentException if the array length is zero or one of the array value is null
+       */
+      protected String[] getInternal(String[] external) throws IllegalArgumentException, ClassCastException, NullPointerException
+      {
+         if (external.length == 0)
+         {
+            throw new IllegalArgumentException("Array must not be zero length");
+         }
 
+         //
+         for (int i = external.length - 1;i >= 0;i--)
+         {
+            if (external[i] == null)
+            {
+               throw new IllegalArgumentException("No null entries allowed in String[]");
+            }
+         }
 
-   protected Object getExternalValue(Object value)
-   {
-      if (cloneInternalValue)
+         //
+         if (cloneExternalValue)
+         {
+            external = external.clone();
+         }
+
+         //
+         return external;
+      }
+
+      protected String[] getExternal(String[] internal)
       {
-         return ((String[])value).clone();
+         if (cloneInternalValue)
+         {
+            internal = internal.clone();
+         }
+         return internal;
       }
-      else
+
+      protected boolean equals(String[] left, String[] right)
       {
-         return value;
+         return Arrays.equals(left, right);
       }
    }
 
-   protected boolean internalValueEquals(Object left, Object right)
-   {
-      String[] valuesL = (String[])left;
-      String[] valuesR = (String[])right;
-      return Arrays.equals(valuesL, valuesR);
-   }
-
    /**
     * Return the parameter value or null if it does not exist.
     *
@@ -171,7 +227,7 @@
     */
    public String getValue(String name) throws IllegalArgumentException
    {
-      String[] value = (String[])get(name);
+      String[] value = get(name);
       return value == null ? null : value[0];
    }
 
@@ -188,7 +244,7 @@
       {
          throw new IllegalArgumentException("No null name");
       }
-      return (String[])get(name);
+      return get(name);
    }
 
    /**

Added: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SimpleMapAccessor.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SimpleMapAccessor.java	                        (rev 0)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/SimpleMapAccessor.java	2007-11-15 15:54:55 UTC (rev 8947)
@@ -0,0 +1,50 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat                                               *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual                    *
+ * contributors as indicated by the @authors tag. See the                     *
+ * copyright.txt in the distribution for a full listing of                    *
+ * individual contributors.                                                   *
+ *                                                                            *
+ * This is free software; you can redistribute it and/or modify it            *
+ * under the terms of the GNU Lesser General Public License as                *
+ * published by the Free Software Foundation; either version 2.1 of           *
+ * the License, or (at your option) any later version.                        *
+ *                                                                            *
+ * This software is distributed in the hope that it will be useful,           *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU           *
+ * Lesser General Public License for more details.                            *
+ *                                                                            *
+ * You should have received a copy of the GNU Lesser General Public           *
+ * License along with this software; if not, write to the Free                *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA         *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.                   *
+ ******************************************************************************/
+package org.jboss.portal.common.util;
+
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class SimpleMapAccessor<K, V> implements MapAccessor<K, V>
+{
+
+   /** . */
+   private Map<K, V> delegate;
+
+   public SimpleMapAccessor(Map<K, V> delegate)
+   {
+      if (delegate == null)
+      {
+         throw new IllegalArgumentException();
+      }
+      this.delegate = delegate;
+   }
+
+   public Map<K, V> getMap(boolean writable)
+   {
+      return delegate;
+   }
+}

Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/TypedMap.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/TypedMap.java	2007-11-15 14:49:38 UTC (rev 8946)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/TypedMap.java	2007-11-15 15:54:55 UTC (rev 8947)
@@ -24,304 +24,222 @@
 
 import org.jboss.portal.common.NotYetImplemented;
 
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.HashMap;
 
 /**
- * A decorator that enforce the map content to be checked against a type. Null internal values are not supported.
- *
  * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
- * @version $Revision: 7304 $
+ * @version $Revision: 1.1 $
  */
-public abstract class TypedMap implements Map
+public class TypedMap<EK, EV, IK, IV> implements Map<EK, EV>
 {
 
-   /** The map accessor. */
-   protected final MapAccessor accessor;
-
-   protected TypedMap(MapAccessor accessor)
+   public abstract static class Converter<E, I>
    {
-      if (accessor == null)
-      {
-         throw new IllegalArgumentException("Need a valid accessor");
-      }
-      this.accessor = accessor;
-   }
 
-   protected TypedMap(final Map delegate)
-   {
-      accessor = new LazyMapAccessor(delegate);
-   }
+      /**
+       * Unwraps the key to the the internal key that will be stored in the map. This method calls the
+       * <code>assertKeyValidity(Object key)</code> and returns the same key. It can be overriden to provide a customized
+       * key that will be used instead of the external key.
+       *
+       * @param external the key to unwrap
+       * @return the unwrapped key
+       * @throws ClassCastException       if the class of the specified key prevents it from being stored in this map
+       * @throws IllegalArgumentException if some aspect of this key prevents it from being stored in this map
+       */
+      protected abstract I getInternal(E external) throws IllegalArgumentException, ClassCastException;
 
-   /**
-    * Return the map provided by the accessor.
-    *
-    * @param requestWriteable
-    * @return the delegate map
-    */
-   protected final Map getDelegate(boolean requestWriteable)
-   {
-      return accessor.getMap(requestWriteable);
-   }
+      /**
+       * Wrap the internal key into its external representation, by default returns the same key. It can be overriden to
+       * provide a customized key that will be used instead of the internal key.
+       */
+      protected abstract E getExternal(I internal);
 
-   /**
-    * Override to check the validity of the key, the default implementation is an empty method.
-    *
-    * @throws ClassCastException       if the class of the specified key prevents it from being stored in this map
-    * @throws IllegalArgumentException if some aspect of this key prevents it from being stored in this map
-    */
-   protected void assertKeyValidity(Object key) throws IllegalArgumentException, ClassCastException
-   {
-   }
+      public I unwrap(E external) throws IllegalArgumentException, ClassCastException, NullPointerException
+      {
+         if (external == null)
+         {
+            throw new NullPointerException("No null key accepted");
+         }
 
-   /**
-    * Unwraps the key to the the internal key that will be stored in the map. This method calls the
-    * <code>assertKeyValidity(Object key)</code> and returns the same key. It can be overriden to provide a customized
-    * key that will be used instead of the external key.
-    *
-    * @param key the key to unwrap
-    * @return the unwrapped key
-    * @throws ClassCastException       if the class of the specified key prevents it from being stored in this map
-    * @throws IllegalArgumentException if some aspect of this key prevents it from being stored in this map
-    */
-   protected Object getInternalKey(Object key) throws IllegalArgumentException, ClassCastException
-   {
-      assertKeyValidity(key);
+         //
+         I internal = getInternal(external);
 
-      //
-      return key;
-   }
+         //
+         if (internal == null)
+         {
+            throw new IllegalArgumentException("The provided key " + external + " was converted to a null key");
+         }
 
-   /**
-    * Wrap the internal key into its external representation, by default returns the same key. It can be overriden to
-    * provide a customized key that will be used instead of the internal key.
-    */
-   protected Object getExternalKey(Object key)
-   {
-      return key;
-   }
-
-   protected final Object unwrapKey(Object externalKey) throws IllegalArgumentException, ClassCastException, NullPointerException
-   {
-      if (externalKey == null)
-      {
-         throw new NullPointerException("No null key accepted");
+         //
+         return internal;
       }
 
-      //
-      Object internalKey = getInternalKey(externalKey);
-
-      //
-      if (internalKey == null)
+      public E wrap(I internal) throws IllegalStateException
       {
-         throw new IllegalArgumentException("The provided key " + externalKey + " was converted to a null key");
-      }
+         if (internal == null)
+         {
+            throw new IllegalStateException("Got an internal null key");
+         }
 
-      //
-      return internalKey;
-   }
+         //
+         E external = getExternal(internal);
 
-   protected final Object wrapKey(Object internalKey) throws IllegalStateException
-   {
-      if (internalKey == null)
-      {
-         throw new IllegalStateException("Got an internal null key");
+         //
+         if (external == null)
+         {
+            throw new IllegalStateException("Converted an internal key to a null key " + internal);
+         }
+
+         //
+         return external;
       }
 
-      //
-      Object externalKey = getExternalKey(internalKey);
-
-      //
-      if (externalKey == null)
+      public boolean safeEquals(I left, I right)
       {
-         throw new IllegalStateException("Converted an internal key to a null key " + internalKey);
+         // Check the internal value, it should not be null
+         return !(left == null || right == null) && equals(left, right);
       }
 
-      //
-      return externalKey;
+      /**
+       * Compare internal values, the passed argument are never null.
+       *
+       * @param left the left value
+       * @param right the right value
+       * @return true if the values are equals
+       */
+      protected abstract boolean equals(I left, I right);
    }
 
-   /**
-    * Unwrap the value to the the internal value that will be stored in the map.
-    *
-    * @param value the value to unwrap
-    * @return the unwrapped value
-    * @throws ClassCastException       if the class of the specified value prevents it from being stored in this map
-    * @throws IllegalArgumentException if some aspect of this value prevents it from being stored in this map
-    */
-   protected Object getInternalValue(Object value) throws IllegalArgumentException, ClassCastException
-   {
-      return value;
-   }
+   /** The map accessor. */
+   private final MapAccessor<IK, IV> accessor;
 
-   /**
-    * Wrap the internal value into its external representation. The external representation value should not be null. If
-    * it is not possible to obtain an external value representation of the internal value, then the the null value
-    * should be returned from this method.
-    *
-    * @param value the param value which will not be null
-    * @return the external value
-    */
-   protected Object getExternalValue(Object value)
-   {
-      return value;
-   }
+   /** The key converter. */
+   private final Converter<EK, IK> keyConverter;
 
-   /**
-    * Extract the internal value from an external value.
-    *
-    * @param externalValue the external value to unwrap
-    * @return the non null internal value
-    * @throws IllegalArgumentException
-    * @throws ClassCastException
-    * @throws NullPointerException
-    */
-   protected final Object unwrapValue(Object externalValue) throws IllegalArgumentException, ClassCastException, NullPointerException
+   /** The value converter. */
+   private final Converter<EV, IV> valueConverter;
+
+   public TypedMap(MapAccessor<IK, IV> accessor, Converter<EK, IK> keyConverter, Converter<EV, IV> valueConverter)
    {
-      if (externalValue == null)
+      if (accessor == null)
       {
-         throw new NullPointerException("No null value accepted");
+         throw new IllegalArgumentException();
       }
-
-      //
-      Object internalValue = getInternalValue(externalValue);
-
-      //
-      if (internalValue == null)
+      if (keyConverter == null)
       {
-         throw new IllegalArgumentException("The provided value " + externalValue + " was converted to a null value");
+         throw new IllegalArgumentException();
       }
+      if (valueConverter == null)
+      {
+         throw new IllegalArgumentException();
+      }
 
-      //
-      return internalValue;
+      
+      this.accessor = accessor;
+      this.keyConverter = keyConverter;
+      this.valueConverter = valueConverter;
    }
 
-   /**
-    * Convert an internal value into an external value.
-    *
-    * @param internalValue the internal value
-    * @return the non null external value
-    * @throws IllegalStateException if the internal value was null or some aspect prevented it to be converted to an
-    *                               external representation
-    */
-   protected final Object wrapValue(Object internalValue) throws IllegalStateException
+   public TypedMap(Map<IK, IV> delegate, Converter<EK, IK> keyConv, Converter<EV, IV> valueConv)
    {
-      if (internalValue == null)
-      {
-         throw new IllegalStateException("Received an internal null value");
-      }
+      this(new SimpleMapAccessor<IK, IV>(delegate), keyConv, valueConv);
+   }
 
-      //
-      Object externalValue = getExternalValue(internalValue);
-
-      //
-      if (externalValue == null)
-      {
-         throw new IllegalStateException("Converted an internal value to a null value " + internalValue);
-      }
-
-      //
-      return externalValue;
+   public Converter<EK, IK> getKeyConverter()
+   {
+      return keyConverter;
    }
 
-   /**
-    * Compare internal values, the default implementation delegates the operation to the equals method of the left
-    * argument.
-    *
-    * @param left
-    * @param right
-    * @return true if the values are equals
-    */
-   protected boolean internalValueEquals(Object left, Object right)
+   public Converter<EV, IV> getValueConverter()
    {
-      return left.equals(right);
+      return valueConverter;
    }
 
-   public int size()
+   public final int size()
    {
-      return getDelegate(false).size();
+      return accessor.getMap(false).size();
    }
 
-   public void clear()
+   public final void clear()
    {
       if (!isEmpty())
       {
-         getDelegate(true).clear();
+         accessor.getMap(true).clear();
       }
    }
 
-   public boolean isEmpty()
+   public final boolean isEmpty()
    {
-      return getDelegate(false).isEmpty();
+      return accessor.getMap(false).isEmpty();
    }
 
-   public boolean containsKey(Object key)
+   public final boolean containsKey(Object key)
    {
-      key = unwrapKey(key);
-      return getDelegate(false).containsKey(key);
+      EK ek = (EK)key;
+      IK ik = keyConverter.unwrap(ek);
+      return accessor.getMap(false).containsKey(ik);
    }
 
-   public boolean containsValue(Object value)
+   public final Set<EK> keySet()
    {
-      value = unwrapValue(value);
-      return getDelegate(false).containsValue(value);
+      return new KeySet();
    }
 
-   public Collection values()
+   public EV put(EK ek, EV ev)
    {
-      return new ValueCollection();
+      IK ik = keyConverter.unwrap(ek);
+      IV iv = valueConverter.unwrap(ev);
+      iv = accessor.getMap(true).put(ik, iv);
+      return iv == null ? null : valueConverter.wrap(iv);
    }
 
-   public void putAll(Map t)
+   public final EV get(Object key)
    {
-      t = convert(t);
-      getDelegate(true).putAll(t);
+      EK ek = (EK)key;
+      IK ik = keyConverter.unwrap(ek);
+      IV iv = accessor.getMap(false).get(ik);
+      return iv == null ? null : valueConverter.wrap(iv);
    }
 
-   public Set entrySet()
+   public final EV remove(Object key)
    {
-      return new TypedMap.TypedEntrySet();
+      EK ek = (EK)key;
+      IK ik = keyConverter.unwrap(ek);
+      IV iv = null;
+      if (!isEmpty())
+      {
+         iv = accessor.getMap(true).remove(ik);
+      }
+      return iv == null ? null : valueConverter.wrap(iv);
    }
 
-   public Set keySet()
+   public final boolean containsValue(Object value)
    {
-      return new KeySet();
+      EV ev = (EV)value;
+      IV iv = valueConverter.unwrap(ev);
+      return accessor.getMap(false).containsValue(iv);
    }
 
-   public Object get(Object key)
+   public final Set<Entry<EK, EV>> entrySet()
    {
-      key = unwrapKey(key);
-      Object value = getDelegate(false).get(key);
-      return value == null ? null : wrapValue(value);
+      return new TypedEntrySet();
    }
 
-   public Object remove(Object key)
+   public void putAll(Map<? extends EK, ? extends EV> em)
    {
-      key = unwrapKey(key);
-
-      Object value = null;
-      if (!isEmpty())
-      {
-         value = getDelegate(true).remove(key);
-      }
-
-      return value == null ? null : wrapValue(value);
+      Map<IK, IV> im = convert(em);
+      accessor.getMap(true).putAll(im);
    }
 
-   public Object put(Object key, Object value)
+   public Collection<EV> values()
    {
-      key = unwrapKey(key);
-      value = unwrapValue(value);
-      value = getDelegate(true).put(key, value);
-      return value == null ? null : wrapValue(value);
+      return new ValueCollection();
    }
 
-   // Overriden methods
-
    /** Compare to parameters objects. */
    public boolean equals(Object o)
    {
@@ -331,8 +249,8 @@
       }
       if (o instanceof Map)
       {
-         Map that = (Map)o;
-         Map delegate = getDelegate(false);
+         Map<EK, EV> that = (Map<EK,EV>)o;
+         Map<IK, IV> delegate = this.accessor.getMap(false);
 
          // Must have same sizes
          if (that.size() != delegate.size())
@@ -341,32 +259,26 @@
          }
 
          //
-         for (Iterator i = that.entrySet().iterator(); i.hasNext();)
+         for (Entry<EK, EV> thatEntry : that.entrySet())
          {
-            Map.Entry thatEntry = (Map.Entry)i.next();
-            Object thatKey = thatEntry.getKey();
-            Object thatValue = thatEntry.getValue();
 
+            EK thatKey = thatEntry.getKey();
+            EV thatValue = thatEntry.getValue();
+
             //
             try
             {
-               // Unwrap key
-               thatKey = unwrapKey(thatKey);
+               // Unwrap key, mostly for checking its type is correct
+               keyConverter.unwrap(thatKey);
 
                // Unwrap value
-               thatValue = unwrapValue(thatValue);
+               IV iv = valueConverter.unwrap(thatValue);
 
                // Get the internal value
-               Object internalValue = delegate.get(thatKey);
+               IV internalValue = delegate.get(thatKey);
 
-               // Check the internal value, it should not be null
-               if (internalValue == null)
-               {
-                  return false;
-               }
-
                // Perform value comparison
-               if (internalValueEquals(internalValue, thatValue) == false)
+               if (!valueConverter.safeEquals(internalValue, iv))
                {
                   return false;
                }
@@ -395,29 +307,52 @@
 
    public String toString()
    {
-      return getDelegate(false).toString();
+      return accessor.getMap(false).toString();
    }
 
-   // API extension
+   /**
+    * Validates and unwraps the map.
+    *
+    * @param t
+    * @return
+    * @throws IllegalArgumentException
+    * @throws NullPointerException
+    * @throws ClassCastException
+    */
+   protected final Map<IK, IV> convert(Map<? extends EK, ? extends EV> t) throws IllegalArgumentException, NullPointerException, ClassCastException
+   {
+      if (t == null)
+      {
+         throw new NullPointerException("No null map can be accepted");
+      }
+      Map<IK, IV> u = new HashMap<IK, IV>(t.size());
+      for (Entry<? extends EK, ? extends EV> entry : t.entrySet())
+      {
+         IK ik = keyConverter.unwrap(entry.getKey());
+         IV iv = valueConverter.unwrap(entry.getValue());
+         u.put(ik, iv);
+      }
+      return u;
+   }
 
    /**
     * Replace the content with the new map which is validated before replacement.
     *
-    * @param t the replacement map
+    * @param map the replacement map
     * @throws ClassCastException
     * @throws NullPointerException
     * @throws IllegalArgumentException
     */
-   public void replace(Map t) throws ClassCastException, NullPointerException, IllegalArgumentException
+   public void replace(Map<EK, EV> map) throws ClassCastException, NullPointerException, IllegalArgumentException
    {
-      if (!t.isEmpty())
+      if (!map.isEmpty())
       {
-         t = convert(t);
+         Map<IK, IV> tmp = convert(map);
 
          //
-         Map delegate = getDelegate(true);
+         Map<IK, IV> delegate = accessor.getMap(true);
          delegate.clear();
-         delegate.putAll(t);
+         delegate.putAll(tmp);
       }
    }
 
@@ -430,41 +365,22 @@
     */
    public void validate() throws ClassCastException, NullPointerException, IllegalArgumentException
    {
-      for (Iterator i = getDelegate(false).entrySet().iterator(); i.hasNext();)
+      for (Entry<IK, IV> entry : accessor.getMap(false).entrySet())
       {
-         Entry entry = (Entry)i.next();
-         wrapKey(entry.getKey());
-         wrapValue(entry.getValue());
+         keyConverter.wrap(entry.getKey());
+         valueConverter.wrap(entry.getValue());
       }
    }
 
-   /** Validate the map and unwrap it if necessary. */
-   protected final Map convert(Map t) throws IllegalArgumentException, NullPointerException, ClassCastException
+   public class KeySet implements Set<EK>
    {
-      if (t == null)
-      {
-         throw new NullPointerException("No null map can be accepted");
-      }
-      Map u = new HashMap(t.size());
-      for (Iterator i = t.entrySet().iterator(); i.hasNext();)
-      {
-         Entry entry = (Entry)i.next();
-         Object key = unwrapKey(entry.getKey());
-         Object value = unwrapValue(entry.getValue());
-         u.put(key, value);
-      }
-      return u;
-   }
 
-   public class ValueCollection implements Collection
-   {
-
       /** . */
-      private final Collection delegate;
+      private final Set<IK> delegate;
 
-      public ValueCollection()
+      public KeySet()
       {
-         this.delegate = getDelegate(false).values();
+         this.delegate = TypedMap.this.accessor.getMap(false).keySet();
       }
 
       public int size()
@@ -474,7 +390,10 @@
 
       public void clear()
       {
-         delegate.clear();
+         if (!isEmpty())
+         {
+            delegate.clear();
+         }
       }
 
       public boolean isEmpty()
@@ -482,65 +401,106 @@
          return delegate.isEmpty();
       }
 
+      public boolean contains(Object o)
+      {
+         EK ek;
+         try
+         {
+            ek = (EK)o;
+         }
+         catch (ClassCastException e)
+         {
+            return false;
+         }
+         try
+         {
+            IK ik = keyConverter.getInternal(ek);
+            return TypedMap.this.accessor.getMap(false).containsKey(ik);
+         }
+         catch (IllegalArgumentException e)
+         {
+            return false;
+         }
+         catch (ClassCastException e)
+         {
+            return false;
+         }
+         catch (NullPointerException e)
+         {
+            return false;
+         }
+      }
+
       public Object[] toArray()
       {
          throw new NotYetImplemented("TypedEntrySet.toArray()");
       }
 
-      public boolean add(Object o)
+      public boolean add(EK ek)
       {
          throw new NotYetImplemented("TypedEntrySet.add(Object o)");
       }
 
-      public boolean contains(Object o)
-      {
-         throw new NotYetImplemented("TypedEntrySet.contains(Object o)");
-      }
-
       public boolean remove(Object o)
       {
          throw new NotYetImplemented("TypedEntrySet.remove(Object o)");
       }
 
-      public boolean addAll(Collection c)
+      public boolean containsAll(Collection<?> objects)
       {
          throw new NotYetImplemented("TypedEntrySet.addAll(Collection c)");
       }
 
-      public boolean containsAll(Collection c)
+      public boolean addAll(Collection<? extends EK> eks)
       {
          throw new NotYetImplemented("TypedEntrySet.containsAll(Collection c)");
       }
 
-      public boolean removeAll(Collection c)
+      public boolean removeAll(Collection<?> objects)
       {
          throw new NotYetImplemented("TypedEntrySet.removeAll(Collection c)");
       }
 
-      public boolean retainAll(Collection c)
+      public boolean retainAll(Collection<?> c)
       {
-         throw new NotYetImplemented("TypedEntrySet.retainAll(Collection c)");
+         if (c == null)
+         {
+            throw new NullPointerException();
+         }
+
+         //
+         boolean changed = false;
+         for (Iterator i = iterator(); i.hasNext();)
+         {
+            Object key = i.next();
+            if (!c.contains(key))
+            {
+               i.remove();
+               changed = true;
+            }
+         }
+         return changed;
       }
 
-      public Iterator iterator()
+      public Iterator<EK> iterator()
       {
-         return new ValueIterator();
+         return new KeyIterator();
       }
 
-      public Object[] toArray(Object a[])
+      public <EK> EK[] toArray(EK a[])
       {
          throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])");
       }
 
-      public class ValueIterator implements Iterator
+      public class KeyIterator implements Iterator<EK>
       {
 
          /** . */
-         private final Iterator delegate;
+         private final Iterator<IK> delegate;
 
-         public ValueIterator()
+         public KeyIterator()
          {
-            this.delegate = ValueCollection.this.delegate.iterator();
+            this.delegate = KeySet.this.delegate.iterator();
          }
 
          public void remove()
@@ -553,24 +513,23 @@
             return delegate.hasNext();
          }
 
-         public Object next()
+         public EK next()
          {
-            Object value = delegate.next();
-            value = wrapValue(value);
-            return value;
+            IK ik = delegate.next();
+            return keyConverter.wrap(ik);
          }
       }
    }
 
-   public class KeySet implements Set
+   public class ValueCollection implements Collection<EV>
    {
 
       /** . */
-      private final Set delegate;
+      private final Collection<IV> delegate;
 
-      public KeySet()
+      public ValueCollection()
       {
-         this.delegate = getDelegate(false).keySet();
+         this.delegate = TypedMap.this.accessor.getMap(false).values();
       }
 
       public int size()
@@ -580,10 +539,7 @@
 
       public void clear()
       {
-         if (!isEmpty())
-         {
-            delegate.clear();
-         }
+         delegate.clear();
       }
 
       public boolean isEmpty()
@@ -591,97 +547,65 @@
          return delegate.isEmpty();
       }
 
-      public boolean contains(Object o)
-      {
-         try
-         {
-            Object key = unwrapKey(o);
-            return getDelegate(false).containsKey(key);
-         }
-         catch (IllegalArgumentException e)
-         {
-            return false;
-         }
-         catch (ClassCastException e)
-         {
-            return false;
-         }
-         catch (NullPointerException e)
-         {
-            return false;
-         }
-      }
-
       public Object[] toArray()
       {
          throw new NotYetImplemented("TypedEntrySet.toArray()");
       }
 
-      public boolean add(Object o)
+      public boolean add(EV ev)
       {
          throw new NotYetImplemented("TypedEntrySet.add(Object o)");
       }
 
+      public boolean contains(Object o)
+      {
+         throw new NotYetImplemented("TypedEntrySet.contains(Object o)");
+      }
+
       public boolean remove(Object o)
       {
          throw new NotYetImplemented("TypedEntrySet.remove(Object o)");
       }
 
-      public boolean addAll(Collection c)
+      public boolean addAll(Collection<? extends EV> evs)
       {
          throw new NotYetImplemented("TypedEntrySet.addAll(Collection c)");
       }
 
-      public boolean containsAll(Collection c)
+      public boolean containsAll(Collection<?> objects)
       {
          throw new NotYetImplemented("TypedEntrySet.containsAll(Collection c)");
       }
 
-      public boolean removeAll(Collection c)
+      public boolean removeAll(Collection<?> objects)
       {
          throw new NotYetImplemented("TypedEntrySet.removeAll(Collection c)");
       }
 
-      public boolean retainAll(Collection c)
+      public boolean retainAll(Collection<?> objects)
       {
-         if (c == null)
-         {
-            throw new NullPointerException();
-         }
-
-         //
-         boolean changed = false;
-         for (Iterator i = iterator(); i.hasNext();)
-         {
-            Object key = i.next();
-            if (!c.contains(key))
-            {
-               i.remove();
-               changed = true;
-            }
-         }
-         return changed;
+         throw new NotYetImplemented("TypedEntrySet.retainAll(Collection c)");
       }
 
-      public Iterator iterator()
+      public <T> T[] toArray(T[] ts)
       {
-         return new KeyIterator();
+         throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])");
       }
 
-      public Object[] toArray(Object a[])
+      public Iterator<EV> iterator()
       {
-         throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])");
+         return new ValueIterator();
       }
 
-      public class KeyIterator implements Iterator
+      public class ValueIterator implements Iterator<EV>
       {
 
          /** . */
-         private final Iterator delegate;
+         private final Iterator<IV> delegate;
 
-         public KeyIterator()
+         public ValueIterator()
          {
-            this.delegate = KeySet.this.delegate.iterator();
+            this.delegate = ValueCollection.this.delegate.iterator();
          }
 
          public void remove()
@@ -694,24 +618,23 @@
             return delegate.hasNext();
          }
 
-         public Object next()
+         public EV next()
          {
-            Object key = delegate.next();
-            key = wrapKey(key);
-            return key;
+            IV iv = delegate.next();
+            return valueConverter.wrap(iv);
          }
       }
    }
 
-   public class TypedEntrySet implements Set
+   public class TypedEntrySet implements Set<Entry<EK, EV>>
    {
 
       /** . */
-      private final Set delegate;
+      private final Set<Entry<IK, IV>> delegate;
 
       public TypedEntrySet()
       {
-         this.delegate = getDelegate(false).entrySet();
+         this.delegate = TypedMap.this.accessor.getMap(false).entrySet();
       }
 
       public int size()
@@ -737,7 +660,7 @@
          throw new NotYetImplemented("TypedEntrySet.toArray()");
       }
 
-      public boolean add(Object o)
+      public boolean add(Entry<EK, EV> ekevEntry)
       {
          throw new NotYetImplemented("TypedEntrySet.add(Object o)");
       }
@@ -752,41 +675,41 @@
          throw new NotYetImplemented("TypedEntrySet.remove(Object o)");
       }
 
-      public boolean addAll(Collection c)
+      public boolean addAll(Collection<? extends Entry<EK, EV>> entries)
       {
          throw new NotYetImplemented("TypedEntrySet.addAll(Collection c)");
       }
 
-      public boolean containsAll(Collection c)
+      public boolean containsAll(Collection<?> objects)
       {
          throw new NotYetImplemented("TypedEntrySet.containsAll(Collection c)");
       }
 
-      public boolean removeAll(Collection c)
+      public boolean removeAll(Collection<?> objects)
       {
          throw new NotYetImplemented("TypedEntrySet.removeAll(Collection c)");
       }
 
-      public boolean retainAll(Collection c)
+      public boolean retainAll(Collection<?> objects)
       {
          throw new NotYetImplemented("TypedEntrySet.retainAll(Collection c)");
       }
 
-      public Iterator iterator()
+      public <T> T[] toArray(T[] ts)
       {
-         return new TypedEntryIterator();
+         throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])");
       }
 
-      public Object[] toArray(Object a[])
+      public Iterator<Entry<EK, EV>> iterator()
       {
-         throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])");
+         return new TypedEntryIterator();
       }
 
-      public class TypedEntryIterator implements Iterator
+      public class TypedEntryIterator implements Iterator<Entry<EK, EV>>
       {
 
          /** . */
-         private final Iterator delegate;
+         private final Iterator<Entry<IK, IV>> delegate;
 
          public TypedEntryIterator()
          {
@@ -803,20 +726,20 @@
             return delegate.hasNext();
          }
 
-         public Object next()
+         public Entry<EK, EV> next()
          {
-            Entry entry = (Entry)delegate.next();
+            Entry<IK, IV> entry = delegate.next();
             return new TypedEntry(entry);
          }
       }
 
-      public class TypedEntry implements Entry
+      public class TypedEntry implements Entry<EK, EV>
       {
 
          /** . */
-         private final Entry delegate;
+         private final Entry<IK, IV> delegate;
 
-         public TypedEntry(Entry delegate)
+         public TypedEntry(Entry<IK, IV> delegate)
          {
             this.delegate = delegate;
          }
@@ -836,58 +759,25 @@
             return delegate.toString();
          }
 
-         public Object getKey()
+         public EK getKey()
          {
-            Object key = delegate.getKey();
-            key = wrapKey(key);
-            return key;
+            IK ik = delegate.getKey();
+            return keyConverter.wrap(ik);
          }
 
-         public Object getValue()
+         public EV getValue()
          {
-            Object value = delegate.getValue();
-            value = wrapValue(value);
-            return value;
+            IV iv = delegate.getValue();
+            return valueConverter.wrap(iv);
          }
 
-         public Object setValue(Object value)
+         public EV setValue(Object value)
          {
-            value = unwrapValue(value);
-            return delegate.setValue(value);
+            EV ev = (EV)value;
+            IV iv = valueConverter.unwrap(ev);
+            iv = delegate.setValue(iv);
+            return valueConverter.wrap(iv);
          }
       }
    }
-
-   private static class LazyMapAccessor implements MapAccessor
-   {
-      private Map delegate;
-
-      public LazyMapAccessor(Map delegate)
-      {
-         this.delegate = delegate;
-      }
-
-      public Map getMap(boolean requestWriteable)
-      {
-         // if the delegate is null, wait until we need to write to it to initialize it
-         if (delegate == null)
-         {
-            if (requestWriteable)
-            {
-               delegate = new HashMap();
-            }
-            else
-            {
-               return Collections.EMPTY_MAP;
-            }
-         }
-
-         return delegate;
-      }
-
-      public String toString()
-      {
-         return getMap(false).toString();
-      }
-   }
 }

Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/value/Value.java
===================================================================
--- modules/common/trunk/common/src/main/java/org/jboss/portal/common/value/Value.java	2007-11-15 14:49:38 UTC (rev 8946)
+++ modules/common/trunk/common/src/main/java/org/jboss/portal/common/value/Value.java	2007-11-15 15:54:55 UTC (rev 8947)
@@ -25,9 +25,6 @@
 import java.io.Serializable;
 import java.util.Arrays;
 import java.util.List;
-import java.util.Iterator;
-import java.util.Collection;
-import java.util.ListIterator;
 import java.util.AbstractList;
 import java.util.RandomAccess;
 
@@ -158,11 +155,11 @@
     *
     * @return a clone of this object
     */
-   public final Object clone()
+   public final Value clone()
    {
       try
       {
-         return super.clone();
+         return (Value)super.clone();
       }
       catch (CloneNotSupportedException e)
       {

Modified: modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ParameterMapTestCase.java
===================================================================
--- modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ParameterMapTestCase.java	2007-11-15 14:49:38 UTC (rev 8946)
+++ modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ParameterMapTestCase.java	2007-11-15 15:54:55 UTC (rev 8947)
@@ -64,7 +64,7 @@
       ParameterMap pm = new ParameterMap(new HashMap());
       try
       {
-         pm.put(new Object(), new String[]{"bar"});
+         ((Map)pm).put(new Object(), new String[]{"bar"});
          fail();
       }
       catch (ClassCastException expected)
@@ -72,7 +72,7 @@
       }
       try
       {
-         pm.put("foo", new Object[]{});
+         ((Map)pm).put("foo", new Object[]{});
          fail();
       }
       catch (ClassCastException expected)

Deleted: modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/TypedMapTestCase.java
===================================================================
--- modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/TypedMapTestCase.java	2007-11-15 14:49:38 UTC (rev 8946)
+++ modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/TypedMapTestCase.java	2007-11-15 15:54:55 UTC (rev 8947)
@@ -1,454 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat                                               *
- * Copyright 2006, Red Hat Middleware, LLC, and individual                    *
- * contributors as indicated by the @authors tag. See the                     *
- * copyright.txt in the distribution for a full listing of                    *
- * individual contributors.                                                   *
- *                                                                            *
- * This is free software; you can redistribute it and/or modify it            *
- * under the terms of the GNU Lesser General Public License as                *
- * published by the Free Software Foundation; either version 2.1 of           *
- * the License, or (at your option) any later version.                        *
- *                                                                            *
- * This software is distributed in the hope that it will be useful,           *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU           *
- * Lesser General Public License for more details.                            *
- *                                                                            *
- * You should have received a copy of the GNU Lesser General Public           *
- * License along with this software; if not, write to the Free                *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA         *
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.                   *
- ******************************************************************************/
-package org.jboss.portal.test.common;
-
-import junit.framework.TestCase;
-import org.jboss.portal.common.util.CollectionBuilder;
-import org.jboss.portal.common.util.TypedMap;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class TypedMapTestCase extends TestCase
-{
-
-   public TypedMapTestCase(String name)
-   {
-      super(name);
-   }
-
-   public void testGetWithBrokenGetInternalValue()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      map.internalValueReturnsNull = true;
-      map.get("abc");
-      map.delegate.put("abc", new Integer(0));
-      map.get("abc");
-   }
-
-   public void testPutWithBrokenGetInternalValue()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      map.internalValueReturnsNull = true;
-      try
-      {
-         map.put("abc", "0");
-         fail();
-      }
-      catch (IllegalArgumentException e)
-      {
-      }
-   }
-
-   public void testRemoveWithBrokenGetInternalValue()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      map.internalValueReturnsNull = true;
-      map.remove("abc");
-   }
-
-   public void testRemoveWithBrokenGetExternalValue()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      map.externalValueReturnsNull = true;
-      map.remove("abc");
-      map.delegate.put("abc", new Integer(0));
-      try
-      {
-         map.remove("abc");
-         fail();
-      }
-      catch (IllegalStateException e)
-      {
-      }
-   }
-
-   public void testGetWithBrokenGetExternalValue()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      map.externalValueReturnsNull = true;
-      map.get("abc");
-      map.delegate.put("abc", new Integer(0));
-      try
-      {
-         map.get("abc");
-         fail();
-      }
-      catch (IllegalStateException e)
-      {
-      }
-   }
-
-   public void testPutWithBrokenGetExternalValue()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      map.externalValueReturnsNull = true;
-      map.put("abc", "0");
-      try
-      {
-         map.put("abc", "0");
-         fail();
-      }
-      catch (IllegalStateException e)
-      {
-      }
-   }
-
-   public void testGetWithInvalidInternalValue()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      map.delegate.put("abc", "0");
-      try
-      {
-         map.get("abc");
-         fail();
-      }
-      catch (ClassCastException expected)
-      {
-      }
-   }
-
-   public void testPutWithInvalidInternalValue()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      map.delegate.put("abc", "0");
-      try
-      {
-         map.put("abc", "0");
-         fail();
-      }
-      catch (ClassCastException expected)
-      {
-      }
-   }
-
-   public void testRemoveWithInvalidInternalValue()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      map.delegate.put("abc", "0");
-      try
-      {
-         map.remove("abc");
-         fail();
-      }
-      catch (ClassCastException expected)
-      {
-      }
-   }
-
-   public void testRemove()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      assertNull(map.remove("abc"));
-      map.delegate.put("abc", new Integer(0));
-      assertEquals("0", map.remove("abc"));
-      assertTrue(map.delegate.isEmpty());
-   }
-
-   public void testPut()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      map.put("abc", "0");
-      assertEquals(Collections.singletonMap("abc", new Integer(0)), map.delegate);
-   }
-
-   public void testGet()
-   {
-      StringToIntegerMap map = new StringToIntegerMap();
-      assertNull(map.get("abc"));
-      map.delegate.put("abc", new Integer(0));
-      assertEquals(Collections.singletonMap("abc", "0"), map);
-   }
-
-   public void testContainsKeyWithInvalidKey()
-   {
-      try
-      {
-         new StringToIntegerMap().containsKey(null);
-         fail();
-      }
-      catch (NullPointerException expected)
-      {
-      }
-      try
-      {
-         new StringToIntegerMap().containsKey(new Object());
-         fail();
-      }
-      catch (ClassCastException expected)
-      {
-      }
-   }
-
-   public void testContainsValueWithInvalidValue()
-   {
-      try
-      {
-         new StringToIntegerMap().containsValue(null);
-         fail();
-      }
-      catch (NullPointerException expected)
-      {
-      }
-      try
-      {
-         new StringToIntegerMap().containsValue(new Object());
-         fail();
-      }
-      catch (ClassCastException expected)
-      {
-      }
-   }
-
-   public void testRemoveWithInvalidKey()
-   {
-      try
-      {
-         new StringToIntegerMap().remove(null);
-         fail();
-      }
-      catch (NullPointerException expected)
-      {
-      }
-      try
-      {
-         new StringToIntegerMap().remove(new Object());
-         fail();
-      }
-      catch (ClassCastException expected)
-      {
-      }
-   }
-
-   public void testGetWithInvalidKey()
-   {
-      try
-      {
-         new StringToIntegerMap().get(null);
-         fail();
-      }
-      catch (NullPointerException expected)
-      {
-      }
-      try
-      {
-         new StringToIntegerMap().get(new Object());
-         fail();
-      }
-      catch (ClassCastException expected)
-      {
-      }
-   }
-
-   public void testPutWithInvalidKey()
-   {
-      try
-      {
-         new StringToIntegerMap().put(null, "0");
-         fail();
-      }
-      catch (NullPointerException expected)
-      {
-      }
-      try
-      {
-         new StringToIntegerMap().put(new Object(), "0");
-         fail();
-      }
-      catch (ClassCastException expected)
-      {
-      }
-   }
-
-   public void testWithPutInvalidValue()
-   {
-      try
-      {
-         new StringToIntegerMap().put("", null);
-         fail();
-      }
-      catch (NullPointerException expected)
-      {
-      }
-      try
-      {
-         new StringToIntegerMap().put("", new Object());
-         fail();
-      }
-      catch (ClassCastException expected)
-      {
-      }
-   }
-
-   public void testEquals()
-   {
-      StringToIntegerMap left = new StringToIntegerMap();
-      left.delegate.put("abc", new Integer(0));
-      Map right = new HashMap();
-
-      //
-      assertFalse(left.equals(right));
-      assertFalse(right.equals(left));
-
-      //
-      right.put("abc", new Object());
-      assertFalse(left.equals(right));
-      assertFalse(right.equals(left));
-
-      //
-      right.put("abc", "abc");
-      assertFalse(left.equals(right));
-      assertFalse(right.equals(left));
-
-      //
-      right.put("abc", "0");
-      assertTrue(left.equals(right));
-      assertTrue(right.equals(left));
-
-      //
-      right.put("def", "1");
-      assertFalse(left.equals(right));
-      assertFalse(right.equals(left));
-
-      //
-      right.remove("def");
-      right.put(null, "0");
-      assertFalse(left.equals(right));
-      assertFalse(right.equals(left));
-
-      //
-      right.remove(null);
-      right.put("def", null);
-      assertFalse(left.equals(right));
-      assertFalse(right.equals(left));
-   }
-
-   public void testEntrySetRetainAll()
-   {
-      Map right = new HashMap();
-      right.put("abc", new Integer(0));
-      right.put("def", new Integer(1));
-      right.put("ghi", new Integer(2));
-
-      //
-      StringToIntegerMap left = new StringToIntegerMap();
-      left.delegate.putAll(right);
-
-      try
-      {
-         left.keySet().retainAll(null);
-         fail("Was expecting NPE");
-      }
-      catch (NullPointerException expected)
-      {
-      }
-
-      //
-      boolean changed = left.keySet().retainAll(CollectionBuilder.hashSet().add("abc").add("def").add("ghi").get());
-      assertFalse(changed);
-      assertEquals(right, left.delegate);
-
-      //
-      changed = left.keySet().retainAll(CollectionBuilder.hashSet().add("def").get());
-      assertTrue(changed);
-      right.remove("abc");
-      right.remove("ghi");
-      assertEquals(right, left.delegate);
-   }
-
-   public static class StringToIntegerMap extends TypedMap
-   {
-
-      /** . */
-      final Map delegate;
-
-      /** . */
-      boolean internalValueReturnsNull = false;
-
-      /** . */
-      boolean externalValueReturnsNull = false;
-
-      public StringToIntegerMap()
-      {
-         super(new HashMap());
-
-         //
-         delegate = getDelegate(false);
-      }
-
-      protected void assertKeyValidity(Object key) throws ClassCastException
-      {
-         assertNotNull(key);
-         if (key instanceof String == false)
-         {
-            throw new ClassCastException();
-         }
-      }
-
-      protected Object getInternalValue(Object value) throws IllegalArgumentException, ClassCastException
-      {
-         assertNotNull(value);
-         if (internalValueReturnsNull)
-         {
-            return null;
-         }
-         String s = (String)value;
-         try
-         {
-            return new Integer(s);
-         }
-         catch (NumberFormatException e)
-         {
-            IllegalArgumentException iae = new IllegalArgumentException();
-            iae.initCause(e);
-            throw iae;
-         }
-      }
-
-      protected Object getExternalValue(Object value)
-      {
-         assertNotNull(value);
-         if (externalValueReturnsNull)
-         {
-            return null;
-         }
-         Integer i = (Integer)value;
-         return i.toString();
-      }
-
-      protected boolean internalValueEquals(Object left, Object right)
-      {
-         assertNotNull(left);
-         assertNotNull(right);
-         Integer li = (Integer)left;
-         Integer ri = (Integer)right;
-         return li.intValue() == ri.intValue();
-      }
-   }
-
-}

Added: modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/TypedMapTestCase.java
===================================================================
--- modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/TypedMapTestCase.java	                        (rev 0)
+++ modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/TypedMapTestCase.java	2007-11-15 15:54:55 UTC (rev 8947)
@@ -0,0 +1,462 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat                                               *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual                    *
+ * contributors as indicated by the @authors tag. See the                     *
+ * copyright.txt in the distribution for a full listing of                    *
+ * individual contributors.                                                   *
+ *                                                                            *
+ * This is free software; you can redistribute it and/or modify it            *
+ * under the terms of the GNU Lesser General Public License as                *
+ * published by the Free Software Foundation; either version 2.1 of           *
+ * the License, or (at your option) any later version.                        *
+ *                                                                            *
+ * This software is distributed in the hope that it will be useful,           *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU           *
+ * Lesser General Public License for more details.                            *
+ *                                                                            *
+ * You should have received a copy of the GNU Lesser General Public           *
+ * License along with this software; if not, write to the Free                *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA         *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.                   *
+ ******************************************************************************/
+package org.jboss.portal.test.common.util;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.util.CollectionBuilder;
+import org.jboss.portal.common.util.TypedMap;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class TypedMapTestCase extends TestCase
+{
+
+   private StringToInteger sti;
+
+   private Map<String, Integer> delegate;
+
+   private StringToIntegerMap map;
+
+   public TypedMapTestCase(String name)
+   {
+      super(name);
+   }
+
+   protected void setUp() throws Exception
+   {
+      delegate = new HashMap<String, Integer>();
+      sti = new StringToInteger();
+      map = new StringToIntegerMap(delegate, sti);
+   }
+
+   public void testGetWithBrokenGetInternalValue()
+   {
+      sti.internalValueReturnsNull = true;
+      map.get("abc");
+      delegate.put("abc", new Integer(0));
+      map.get("abc");
+   }
+
+   public void testPutWithBrokenGetInternalValue()
+   {
+      sti.internalValueReturnsNull = true;
+      try
+      {
+         map.put("abc", "0");
+         fail();
+      }
+      catch (IllegalArgumentException e)
+      {
+      }
+   }
+
+   public void testRemoveWithBrokenGetInternalValue()
+   {
+      sti.internalValueReturnsNull = true;
+      map.remove("abc");
+   }
+
+   public void testRemoveWithBrokenGetExternalValue()
+   {
+      sti.externalValueReturnsNull = true;
+      map.remove("abc");
+      delegate.put("abc", new Integer(0));
+      try
+      {
+         map.remove("abc");
+         fail();
+      }
+      catch (IllegalStateException e)
+      {
+      }
+   }
+
+   public void testGetWithBrokenGetExternalValue()
+   {
+      sti.externalValueReturnsNull = true;
+      map.get("abc");
+      delegate.put("abc", new Integer(0));
+      try
+      {
+         map.get("abc");
+         fail();
+      }
+      catch (IllegalStateException e)
+      {
+      }
+   }
+
+   public void testPutWithBrokenGetExternalValue()
+   {
+      sti.externalValueReturnsNull = true;
+      map.put("abc", "0");
+      try
+      {
+         map.put("abc", "0");
+         fail();
+      }
+      catch (IllegalStateException e)
+      {
+      }
+   }
+
+   public void testGetWithInvalidInternalValue()
+   {
+      ((Map)delegate).put("abc", "0");
+      try
+      {
+         map.get("abc");
+         fail();
+      }
+      catch (ClassCastException expected)
+      {
+      }
+   }
+
+   public void testPutWithInvalidInternalValue()
+   {
+      ((Map)delegate).put("abc", "0");
+      try
+      {
+         map.put("abc", "0");
+         fail();
+      }
+      catch (ClassCastException expected)
+      {
+      }
+   }
+
+   public void testRemoveWithInvalidInternalValue()
+   {
+      ((Map)delegate).put("abc", "0");
+      try
+      {
+         map.remove("abc");
+         fail();
+      }
+      catch (ClassCastException expected)
+      {
+      }
+   }
+
+   public void testRemove()
+   {
+      assertNull(map.remove("abc"));
+      delegate.put("abc", new Integer(0));
+      assertEquals("0", map.remove("abc"));
+      assertTrue(delegate.isEmpty());
+   }
+
+   public void testPut()
+   {
+      map.put("abc", "0");
+      assertEquals(Collections.singletonMap("abc", new Integer(0)), delegate);
+   }
+
+   public void testGet()
+   {
+      assertNull(map.get("abc"));
+      delegate.put("abc", new Integer(0));
+      assertEquals(Collections.singletonMap("abc", "0"), map);
+   }
+
+   public void testContainsKeyWithInvalidKey()
+   {
+      try
+      {
+         map.containsKey(null);
+         fail();
+      }
+      catch (NullPointerException expected)
+      {
+      }
+      try
+      {
+         map.containsKey(new Object());
+         fail();
+      }
+      catch (ClassCastException expected)
+      {
+      }
+   }
+
+   public void testContainsValueWithInvalidValue()
+   {
+      try
+      {
+         map.containsValue(null);
+         fail();
+      }
+      catch (NullPointerException expected)
+      {
+      }
+      try
+      {
+         map.containsValue(new Object());
+         fail();
+      }
+      catch (ClassCastException expected)
+      {
+      }
+   }
+
+   public void testRemoveWithInvalidKey()
+   {
+      try
+      {
+         map.remove(null);
+         fail();
+      }
+      catch (NullPointerException expected)
+      {
+      }
+      try
+      {
+         map.remove(new Object());
+         fail();
+      }
+      catch (ClassCastException expected)
+      {
+      }
+   }
+
+   public void testGetWithInvalidKey()
+   {
+      try
+      {
+         map.get(null);
+         fail();
+      }
+      catch (NullPointerException expected)
+      {
+      }
+      try
+      {
+         map.get(new Object());
+         fail();
+      }
+      catch (ClassCastException expected)
+      {
+      }
+   }
+
+   public void testPutWithInvalidKey()
+   {
+      try
+      {
+         map.put(null, "0");
+         fail();
+      }
+      catch (NullPointerException expected)
+      {
+      }
+      try
+      {
+         ((Map)map).put(new Object(), "0");
+         fail();
+      }
+      catch (ClassCastException expected)
+      {
+      }
+   }
+
+   public void testWithPutInvalidValue()
+   {
+      try
+      {
+         map.put("", null);
+         fail();
+      }
+      catch (NullPointerException expected)
+      {
+      }
+      try
+      {
+         ((Map)map).put("", new Object());
+         fail();
+      }
+      catch (ClassCastException expected)
+      {
+      }
+   }
+
+   public void testEquals()
+   {
+      Map<String, Integer> leftDelegate = new HashMap<String, Integer>();
+      StringToIntegerMap left = new StringToIntegerMap(leftDelegate, sti);
+      leftDelegate.put("abc", new Integer(0));
+      Map right = new HashMap();
+
+      //
+      assertFalse(left.equals(right));
+      assertFalse(right.equals(left));
+
+      //
+      right.put("abc", new Object());
+      assertFalse(left.equals(right));
+      assertFalse(right.equals(left));
+
+      //
+      right.put("abc", "abc");
+      assertFalse(left.equals(right));
+      assertFalse(right.equals(left));
+
+      //
+      right.put("abc", "0");
+      assertTrue(left.equals(right));
+      assertTrue(right.equals(left));
+
+      //
+      right.put("def", "1");
+      assertFalse(left.equals(right));
+      assertFalse(right.equals(left));
+
+      //
+      right.remove("def");
+      right.put(null, "0");
+      assertFalse(left.equals(right));
+      assertFalse(right.equals(left));
+
+      //
+      right.remove(null);
+      right.put("def", null);
+      assertFalse(left.equals(right));
+      assertFalse(right.equals(left));
+   }
+
+   public void testEntrySetRetainAll()
+   {
+      Map right = new HashMap();
+      right.put("abc", new Integer(0));
+      right.put("def", new Integer(1));
+      right.put("ghi", new Integer(2));
+
+      //
+      Map<String, Integer> leftDelegate = new HashMap<String, Integer>();
+      StringToIntegerMap left = new StringToIntegerMap(leftDelegate, sti);
+      leftDelegate.putAll(right);
+
+      try
+      {
+         left.keySet().retainAll(null);
+         fail("Was expecting NPE");
+      }
+      catch (NullPointerException expected)
+      {
+      }
+
+      //
+      boolean changed = left.keySet().retainAll(CollectionBuilder.hashSet().add("abc").add("def").add("ghi").get());
+      assertFalse(changed);
+      assertEquals(right, leftDelegate);
+
+      //
+      changed = left.keySet().retainAll(CollectionBuilder.hashSet().add("def").get());
+      assertTrue(changed);
+      right.remove("abc");
+      right.remove("ghi");
+      assertEquals(right, leftDelegate);
+   }
+
+   public static class StringToIntegerMap extends TypedMap<String, String, String, Integer>
+   {
+      public StringToIntegerMap(Map<String, Integer> map, StringToInteger sti)
+      {
+         super(map, new StringToString(), sti);
+      }
+   }
+
+   private static class StringToString extends TypedMap.Converter<String, String>
+   {
+      protected String getInternal(String external) throws IllegalArgumentException, ClassCastException
+      {
+         return external;
+      }
+      protected String getExternal(String internal)
+      {
+         return internal;
+      }
+      protected boolean equals(String left, String right)
+      {
+         return left.equals(right);
+      }
+   }
+
+   private static class StringToInteger extends TypedMap.Converter<String, Integer>
+   {
+
+      /** . */
+      boolean internalValueReturnsNull = false;
+
+      /** . */
+      boolean externalValueReturnsNull = false;
+
+      protected Integer getInternal(String external) throws IllegalArgumentException, ClassCastException
+      {
+         assertNotNull(external);
+
+         //
+         if (internalValueReturnsNull)
+         {
+            return null;
+         }
+         try
+         {
+            return new Integer(external);
+         }
+         catch (NumberFormatException e)
+         {
+            IllegalArgumentException iae = new IllegalArgumentException();
+            iae.initCause(e);
+            throw iae;
+         }
+      }
+
+      protected String getExternal(Integer internal)
+      {
+         assertNotNull(internal);
+
+         //
+         if (externalValueReturnsNull)
+         {
+            return null;
+         }
+
+         //
+         return internal.toString();
+      }
+
+      protected boolean equals(Integer left, Integer right)
+      {
+         assertNotNull(left);
+         assertNotNull(right);
+         return left.intValue() == right.intValue();
+      }
+   }
+}
\ No newline at end of file




More information about the portal-commits mailing list