From portal-commits at lists.jboss.org Thu Nov 15 10:54:55 2007 Content-Type: multipart/mixed; boundary="===============4148238125705886174==" MIME-Version: 1.0 From: portal-commits at lists.jboss.org To: portal-commits at lists.jboss.org Subject: [portal-commits] JBoss Portal SVN: r8947 - in modules/common/trunk/common/src: main/java/org/jboss/portal/common/value and 2 other directories. Date: Thu, 15 Nov 2007 10:54:55 -0500 Message-ID: --===============4148238125705886174== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: julien(a)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/S= impleMapAccessor.java modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/u= til/TypedMapTestCase.java Removed: modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/T= ypedMapTestCase.java Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/M= apAccessor.java modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/P= arameterMap.java modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/T= ypedMap.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/P= arameterMapTestCase.java Log: generified TypedMap + improved it Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common= /util/MapAccessor.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 Julien Viet * @version $Revision: 7228 $ */ -public interface MapAccessor +public interface MapAccessor { /** * 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 cr= eate-on-write mechanism. * @return a non null map */ - Map getMap(boolean requestWriteable); + Map getMap(boolean writable); } Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common= /util/ParameterMap.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 Julien Viet * @version $Revision: 6671 $ */ -public class ParameterMap extends TypedMap +public class ParameterMap extends TypedMap { = - /** If true returned values will be cloned when returned to the client.= */ - private boolean cloneInternalValue; + /** . */ + private static final KeyConverter keyConv =3D new KeyConverter(); = - /** If true returned values will be cloned when provided by the client.= */ - private boolean cloneExternalValue; + /** . */ + private static final ValueConverter valueConv1 =3D new ValueConverter(f= alse, false); = - public ParameterMap(boolean cloneInternalValue, boolean cloneExternalVa= lue) + /** . */ + private static final ValueConverter valueConv2 =3D new ValueConverter(f= alse, true); + + /** . */ + private static final ValueConverter valueConv3 =3D new ValueConverter(t= rue, false); + + /** . */ + private static final ValueConverter valueConv4 =3D new ValueConverter(t= rue, true); + + private static ValueConverter getValueConverter(boolean cloneInternalVa= lue, 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, b= oolean cloneExternalValue) - { - super(accessor); + /** . */ + private final boolean cloneInternalValue; = - // - this.cloneInternalValue =3D cloneInternalValue; - this.cloneExternalValue =3D cloneExternalValue; - } + /** . */ + private final boolean cloneExternalValue; = - public ParameterMap(Map delegate, boolean cloneInternalValue, boolean c= loneExternalValue) + public ParameterMap(boolean cloneInternalValue, boolean cloneExternalVa= lue) { - super(delegate); - - // - this.cloneInternalValue =3D cloneInternalValue; - this.cloneExternalValue =3D cloneExternalValue; + this(new HashMap(), cloneInternalValue, cloneExter= nalValue); } = - public ParameterMap(MapAccessor accessor) + public ParameterMap(MapAccessor accessor) { this(accessor, false, false); } = - public ParameterMap(Map delegate) + public ParameterMap(Map delegate) { this(delegate, false, false); } @@ -79,6 +102,24 @@ this(false, false); } = + public ParameterMap(MapAccessor accessor, boolean clo= neInternalValue, boolean cloneExternalValue) + { + super(accessor, keyConv, getValueConverter(cloneInternalValue, clone= ExternalValue)); + + // + this.cloneInternalValue =3D cloneInternalValue; + this.cloneExternalValue =3D cloneExternalValue; + } + + public ParameterMap(Map delegate, boolean cloneIntern= alValue, boolean cloneExternalValue) + { + super(delegate, keyConv, getValueConverter(cloneInternalValue, clone= ExternalValue)); + + // + this.cloneInternalValue =3D cloneInternalValue; + this.cloneExternalValue =3D 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 =3D=3D 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 stri= ng - * @throws IllegalArgumentException if the array length is zero or one = of the array value is null - */ - protected Object getInternalValue(Object value) throws NullPointerExcep= tion, ClassCastException, IllegalArgumentException + private static class KeyConverter extends Converter { - if (value instanceof String[] =3D=3D false) + protected String getInternal(String external) throws IllegalArgument= Exception, ClassCastException { - throw new ClassCastException("Value should be a String[]"); + return external; } - String[] strings =3D (String[])value; - if (strings.length =3D=3D 0) + protected String getExternal(String internal) { - throw new IllegalArgumentException("Array must not be zero length= "); + return internal; } - - // - for (int i =3D strings.length - 1;i >=3D 0;i--) + protected boolean equals(String left, String right) { - if (strings[i] =3D=3D null) - { - throw new IllegalArgumentException("No null entries allowed in= String[]"); - } + return left.equals(right); } + } = - // - if (cloneExternalValue) + private static class ValueConverter extends Converter + { + + /** . */ + private final boolean cloneInternalValue; + + /** . */ + private final boolean cloneExternalValue; + + private ValueConverter(boolean cloneInternalValue, boolean cloneExte= rnalValue) { - strings =3D (String[])strings.clone(); + this.cloneInternalValue =3D cloneInternalValue; + this.cloneExternalValue =3D 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 s= tring + * @throws IllegalArgumentException if the array length is zero or o= ne of the array value is null + */ + protected String[] getInternal(String[] external) throws IllegalArgu= mentException, ClassCastException, NullPointerException + { + if (external.length =3D=3D 0) + { + throw new IllegalArgumentException("Array must not be zero len= gth"); + } = + // + for (int i =3D external.length - 1;i >=3D 0;i--) + { + if (external[i] =3D=3D null) + { + throw new IllegalArgumentException("No null entries allowed= in String[]"); + } + } = - protected Object getExternalValue(Object value) - { - if (cloneInternalValue) + // + if (cloneExternalValue) + { + external =3D external.clone(); + } + + // + return external; + } + + protected String[] getExternal(String[] internal) { - return ((String[])value).clone(); + if (cloneInternalValue) + { + internal =3D 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 =3D (String[])left; - String[] valuesR =3D (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 =3D (String[])get(name); + String[] value =3D get(name); return value =3D=3D 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/ut= il/SimpleMapAccessor.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 Julien Viet + * @version $Revision: 1.1 $ + */ +public class SimpleMapAccessor implements MapAccessor +{ + + /** . */ + private Map delegate; + + public SimpleMapAccessor(Map delegate) + { + if (delegate =3D=3D null) + { + throw new IllegalArgumentException(); + } + this.delegate =3D delegate; + } + + public Map getMap(boolean writable) + { + return delegate; + } +} Modified: modules/common/trunk/common/src/main/java/org/jboss/portal/common= /util/TypedMap.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 Julien Viet - * @version $Revision: 7304 $ + * @version $Revision: 1.1 $ */ -public abstract class TypedMap implements Map +public class TypedMap implements Map { = - /** The map accessor. */ - protected final MapAccessor accessor; - - protected TypedMap(MapAccessor accessor) + public abstract static class Converter { - if (accessor =3D=3D null) - { - throw new IllegalArgumentException("Need a valid accessor"); - } - this.accessor =3D accessor; - } = - protected TypedMap(final Map delegate) - { - accessor =3D new LazyMapAccessor(delegate); - } + /** + * Unwraps the key to the the internal key that will be stored in th= e map. This method calls the + * assertKeyValidity(Object key) and returns the same k= ey. 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 ke= y prevents it from being stored in this map + * @throws IllegalArgumentException if some aspect of this key preve= nts it from being stored in this map + */ + protected abstract I getInternal(E external) throws IllegalArgumentE= xception, 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 defaul= t returns the same key. It can be overriden to + * provide a customized key that will be used instead of the interna= l key. + */ + protected abstract E getExternal(I internal); = - /** - * Override to check the validity of the key, the default implementatio= n is an empty method. - * - * @throws ClassCastException if the class of the specified key p= revents 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 IllegalArgumentExce= ption, ClassCastException - { - } + public I unwrap(E external) throws IllegalArgumentException, ClassCa= stException, NullPointerException + { + if (external =3D=3D null) + { + throw new NullPointerException("No null key accepted"); + } = - /** - * Unwraps the key to the the internal key that will be stored in the m= ap. This method calls the - * assertKeyValidity(Object key) 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 p= revents 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 IllegalArgumentExcep= tion, ClassCastException - { - assertKeyValidity(key); + // + I internal =3D getInternal(external); = - // - return key; - } + // + if (internal =3D=3D null) + { + throw new IllegalArgumentException("The provided key " + exter= nal + " was converted to a null key"); + } = - /** - * Wrap the internal key into its external representation, by default r= eturns the same key. It can be overriden to - * provide a customized key that will be used instead of the internal k= ey. - */ - protected Object getExternalKey(Object key) - { - return key; - } - - protected final Object unwrapKey(Object externalKey) throws IllegalArgu= mentException, ClassCastException, NullPointerException - { - if (externalKey =3D=3D null) - { - throw new NullPointerException("No null key accepted"); + // + return internal; } = - // - Object internalKey =3D getInternalKey(externalKey); - - // - if (internalKey =3D=3D null) + public E wrap(I internal) throws IllegalStateException { - throw new IllegalArgumentException("The provided key " + external= Key + " was converted to a null key"); - } + if (internal =3D=3D null) + { + throw new IllegalStateException("Got an internal null key"); + } = - // - return internalKey; - } + // + E external =3D getExternal(internal); = - protected final Object wrapKey(Object internalKey) throws IllegalStateE= xception - { - if (internalKey =3D=3D null) - { - throw new IllegalStateException("Got an internal null key"); + // + if (external =3D=3D null) + { + throw new IllegalStateException("Converted an internal key to = a null key " + internal); + } + + // + return external; } = - // - Object externalKey =3D getExternalKey(internalKey); - - // - if (externalKey =3D=3D null) + public boolean safeEquals(I left, I right) { - throw new IllegalStateException("Converted an internal key to a n= ull key " + internalKey); + // Check the internal value, it should not be null + return !(left =3D=3D null || right =3D=3D null) && equals(left, r= ight); } = - // - 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 th= e 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 preven= ts it from being stored in this map - */ - protected Object getInternalValue(Object value) throws IllegalArgumentE= xception, ClassCastException - { - return value; - } + /** The map accessor. */ + private final MapAccessor accessor; = - /** - * Wrap the internal value into its external representation. The extern= al 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 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 Illegal= ArgumentException, ClassCastException, NullPointerException + /** The value converter. */ + private final Converter valueConverter; + + public TypedMap(MapAccessor accessor, Converter keyConv= erter, Converter valueConverter) { - if (externalValue =3D=3D null) + if (accessor =3D=3D null) { - throw new NullPointerException("No null value accepted"); + throw new IllegalArgumentException(); } - - // - Object internalValue =3D getInternalValue(externalValue); - - // - if (internalValue =3D=3D null) + if (keyConverter =3D=3D null) { - throw new IllegalArgumentException("The provided value " + extern= alValue + " was converted to a null value"); + throw new IllegalArgumentException(); } + if (valueConverter =3D=3D null) + { + throw new IllegalArgumentException(); + } = - // - return internalValue; + = + this.accessor =3D accessor; + this.keyConverter =3D keyConverter; + this.valueConverter =3D 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 IllegalSt= ateException + public TypedMap(Map delegate, Converter keyConv, Conver= ter valueConv) { - if (internalValue =3D=3D null) - { - throw new IllegalStateException("Received an internal null value"= ); - } + this(new SimpleMapAccessor(delegate), keyConv, valueConv); + } = - // - Object externalValue =3D getExternalValue(internalValue); - - // - if (externalValue =3D=3D null) - { - throw new IllegalStateException("Converted an internal value to a= null value " + internalValue); - } - - // - return externalValue; + public Converter getKeyConverter() + { + return keyConverter; } = - /** - * Compare internal values, the default implementation delegates the op= eration 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 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 =3D unwrapKey(key); - return getDelegate(false).containsKey(key); + EK ek =3D (EK)key; + IK ik =3D keyConverter.unwrap(ek); + return accessor.getMap(false).containsKey(ik); } = - public boolean containsValue(Object value) + public final Set keySet() { - value =3D 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 =3D keyConverter.unwrap(ek); + IV iv =3D valueConverter.unwrap(ev); + iv =3D accessor.getMap(true).put(ik, iv); + return iv =3D=3D null ? null : valueConverter.wrap(iv); } = - public void putAll(Map t) + public final EV get(Object key) { - t =3D convert(t); - getDelegate(true).putAll(t); + EK ek =3D (EK)key; + IK ik =3D keyConverter.unwrap(ek); + IV iv =3D accessor.getMap(false).get(ik); + return iv =3D=3D null ? null : valueConverter.wrap(iv); } = - public Set entrySet() + public final EV remove(Object key) { - return new TypedMap.TypedEntrySet(); + EK ek =3D (EK)key; + IK ik =3D keyConverter.unwrap(ek); + IV iv =3D null; + if (!isEmpty()) + { + iv =3D accessor.getMap(true).remove(ik); + } + return iv =3D=3D null ? null : valueConverter.wrap(iv); } = - public Set keySet() + public final boolean containsValue(Object value) { - return new KeySet(); + EV ev =3D (EV)value; + IV iv =3D valueConverter.unwrap(ev); + return accessor.getMap(false).containsValue(iv); } = - public Object get(Object key) + public final Set> entrySet() { - key =3D unwrapKey(key); - Object value =3D getDelegate(false).get(key); - return value =3D=3D null ? null : wrapValue(value); + return new TypedEntrySet(); } = - public Object remove(Object key) + public void putAll(Map em) { - key =3D unwrapKey(key); - - Object value =3D null; - if (!isEmpty()) - { - value =3D getDelegate(true).remove(key); - } - - return value =3D=3D null ? null : wrapValue(value); + Map im =3D convert(em); + accessor.getMap(true).putAll(im); } = - public Object put(Object key, Object value) + public Collection values() { - key =3D unwrapKey(key); - value =3D unwrapValue(value); - value =3D getDelegate(true).put(key, value); - return value =3D=3D 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 =3D (Map)o; - Map delegate =3D getDelegate(false); + Map that =3D (Map)o; + Map delegate =3D this.accessor.getMap(false); = // Must have same sizes if (that.size() !=3D delegate.size()) @@ -341,32 +259,26 @@ } = // - for (Iterator i =3D that.entrySet().iterator(); i.hasNext();) + for (Entry thatEntry : that.entrySet()) { - Map.Entry thatEntry =3D (Map.Entry)i.next(); - Object thatKey =3D thatEntry.getKey(); - Object thatValue =3D thatEntry.getValue(); = + EK thatKey =3D thatEntry.getKey(); + EV thatValue =3D thatEntry.getValue(); + // try { - // Unwrap key - thatKey =3D unwrapKey(thatKey); + // Unwrap key, mostly for checking its type is correct + keyConverter.unwrap(thatKey); = // Unwrap value - thatValue =3D unwrapValue(thatValue); + IV iv =3D valueConverter.unwrap(thatValue); = // Get the internal value - Object internalValue =3D delegate.get(thatKey); + IV internalValue =3D delegate.get(thatKey); = - // Check the internal value, it should not be null - if (internalValue =3D=3D null) - { - return false; - } - // Perform value comparison - if (internalValueEquals(internalValue, thatValue) =3D=3D fa= lse) + 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 convert(Map t) = throws IllegalArgumentException, NullPointerException, ClassCastException + { + if (t =3D=3D null) + { + throw new NullPointerException("No null map can be accepted"); + } + Map u =3D new HashMap(t.size()); + for (Entry entry : t.entrySet()) + { + IK ik =3D keyConverter.unwrap(entry.getKey()); + IV iv =3D valueConverter.unwrap(entry.getValue()); + u.put(ik, iv); + } + return u; + } = /** * Replace the content with the new map which is validated before repla= cement. * - * @param t the replacement map + * @param map the replacement map * @throws ClassCastException * @throws NullPointerException * @throws IllegalArgumentException */ - public void replace(Map t) throws ClassCastException, NullPointerExcept= ion, IllegalArgumentException + public void replace(Map map) throws ClassCastException, NullPoi= nterException, IllegalArgumentException { - if (!t.isEmpty()) + if (!map.isEmpty()) { - t =3D convert(t); + Map tmp =3D convert(map); = // - Map delegate =3D getDelegate(true); + Map delegate =3D 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 =3D getDelegate(false).entrySet().iterator(); i.hasN= ext();) + for (Entry entry : accessor.getMap(false).entrySet()) { - Entry entry =3D (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, Nul= lPointerException, ClassCastException + public class KeySet implements Set { - if (t =3D=3D null) - { - throw new NullPointerException("No null map can be accepted"); - } - Map u =3D new HashMap(t.size()); - for (Iterator i =3D t.entrySet().iterator(); i.hasNext();) - { - Entry entry =3D (Entry)i.next(); - Object key =3D unwrapKey(entry.getKey()); - Object value =3D unwrapValue(entry.getValue()); - u.put(key, value); - } - return u; - } = - public class ValueCollection implements Collection - { - /** . */ - private final Collection delegate; + private final Set delegate; = - public ValueCollection() + public KeySet() { - this.delegate =3D getDelegate(false).values(); + this.delegate =3D 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 =3D (EK)o; + } + catch (ClassCastException e) + { + return false; + } + try + { + IK ik =3D 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 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 =3D=3D null) + { + throw new NullPointerException(); + } + + // + boolean changed =3D false; + for (Iterator i =3D iterator(); i.hasNext();) + { + Object key =3D i.next(); + if (!c.contains(key)) + { + i.remove(); + changed =3D true; + } + } + return changed; } = - public Iterator iterator() + public Iterator iterator() { - return new ValueIterator(); + return new KeyIterator(); } = - public Object[] toArray(Object a[]) + public EK[] toArray(EK a[]) { throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])"); } = - public class ValueIterator implements Iterator + public class KeyIterator implements Iterator { = /** . */ - private final Iterator delegate; + private final Iterator delegate; = - public ValueIterator() + public KeyIterator() { - this.delegate =3D ValueCollection.this.delegate.iterator(); + this.delegate =3D KeySet.this.delegate.iterator(); } = public void remove() @@ -553,24 +513,23 @@ return delegate.hasNext(); } = - public Object next() + public EK next() { - Object value =3D delegate.next(); - value =3D wrapValue(value); - return value; + IK ik =3D delegate.next(); + return keyConverter.wrap(ik); } } } = - public class KeySet implements Set + public class ValueCollection implements Collection { = /** . */ - private final Set delegate; + private final Collection delegate; = - public KeySet() + public ValueCollection() { - this.delegate =3D getDelegate(false).keySet(); + this.delegate =3D 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 =3D 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 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 =3D=3D null) - { - throw new NullPointerException(); - } - - // - boolean changed =3D false; - for (Iterator i =3D iterator(); i.hasNext();) - { - Object key =3D i.next(); - if (!c.contains(key)) - { - i.remove(); - changed =3D true; - } - } - return changed; + throw new NotYetImplemented("TypedEntrySet.retainAll(Collection c= )"); } = - public Iterator iterator() + public T[] toArray(T[] ts) { - return new KeyIterator(); + throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])"); } = - public Object[] toArray(Object a[]) + public Iterator iterator() { - throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])"); + return new ValueIterator(); } = - public class KeyIterator implements Iterator + public class ValueIterator implements Iterator { = /** . */ - private final Iterator delegate; + private final Iterator delegate; = - public KeyIterator() + public ValueIterator() { - this.delegate =3D KeySet.this.delegate.iterator(); + this.delegate =3D ValueCollection.this.delegate.iterator(); } = public void remove() @@ -694,24 +618,23 @@ return delegate.hasNext(); } = - public Object next() + public EV next() { - Object key =3D delegate.next(); - key =3D wrapKey(key); - return key; + IV iv =3D delegate.next(); + return valueConverter.wrap(iv); } } } = - public class TypedEntrySet implements Set + public class TypedEntrySet implements Set> { = /** . */ - private final Set delegate; + private final Set> delegate; = public TypedEntrySet() { - this.delegate =3D getDelegate(false).entrySet(); + this.delegate =3D 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 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> 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[] toArray(T[] ts) { - return new TypedEntryIterator(); + throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])"); } = - public Object[] toArray(Object a[]) + public Iterator> iterator() { - throw new NotYetImplemented("TypedEntrySet.toArray(Object a[])"); + return new TypedEntryIterator(); } = - public class TypedEntryIterator implements Iterator + public class TypedEntryIterator implements Iterator> { = /** . */ - private final Iterator delegate; + private final Iterator> delegate; = public TypedEntryIterator() { @@ -803,20 +726,20 @@ return delegate.hasNext(); } = - public Object next() + public Entry next() { - Entry entry =3D (Entry)delegate.next(); + Entry entry =3D delegate.next(); return new TypedEntry(entry); } } = - public class TypedEntry implements Entry + public class TypedEntry implements Entry { = /** . */ - private final Entry delegate; + private final Entry delegate; = - public TypedEntry(Entry delegate) + public TypedEntry(Entry delegate) { this.delegate =3D delegate; } @@ -836,58 +759,25 @@ return delegate.toString(); } = - public Object getKey() + public EK getKey() { - Object key =3D delegate.getKey(); - key =3D wrapKey(key); - return key; + IK ik =3D delegate.getKey(); + return keyConverter.wrap(ik); } = - public Object getValue() + public EV getValue() { - Object value =3D delegate.getValue(); - value =3D wrapValue(value); - return value; + IV iv =3D delegate.getValue(); + return valueConverter.wrap(iv); } = - public Object setValue(Object value) + public EV setValue(Object value) { - value =3D unwrapValue(value); - return delegate.setValue(value); + EV ev =3D (EV)value; + IV iv =3D valueConverter.unwrap(ev); + iv =3D delegate.setValue(iv); + return valueConverter.wrap(iv); } } } - - private static class LazyMapAccessor implements MapAccessor - { - private Map delegate; - - public LazyMapAccessor(Map delegate) - { - this.delegate =3D delegate; - } - - public Map getMap(boolean requestWriteable) - { - // if the delegate is null, wait until we need to write to it to = initialize it - if (delegate =3D=3D null) - { - if (requestWriteable) - { - delegate =3D 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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/c= ommon/ParameterMapTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 =3D 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/co= mmon/TypedMapTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 Julien Viet - * @version $Revision: 1.1 $ - */ -public class TypedMapTestCase extends TestCase -{ - - public TypedMapTestCase(String name) - { - super(name); - } - - public void testGetWithBrokenGetInternalValue() - { - StringToIntegerMap map =3D new StringToIntegerMap(); - map.internalValueReturnsNull =3D true; - map.get("abc"); - map.delegate.put("abc", new Integer(0)); - map.get("abc"); - } - - public void testPutWithBrokenGetInternalValue() - { - StringToIntegerMap map =3D new StringToIntegerMap(); - map.internalValueReturnsNull =3D true; - try - { - map.put("abc", "0"); - fail(); - } - catch (IllegalArgumentException e) - { - } - } - - public void testRemoveWithBrokenGetInternalValue() - { - StringToIntegerMap map =3D new StringToIntegerMap(); - map.internalValueReturnsNull =3D true; - map.remove("abc"); - } - - public void testRemoveWithBrokenGetExternalValue() - { - StringToIntegerMap map =3D new StringToIntegerMap(); - map.externalValueReturnsNull =3D true; - map.remove("abc"); - map.delegate.put("abc", new Integer(0)); - try - { - map.remove("abc"); - fail(); - } - catch (IllegalStateException e) - { - } - } - - public void testGetWithBrokenGetExternalValue() - { - StringToIntegerMap map =3D new StringToIntegerMap(); - map.externalValueReturnsNull =3D true; - map.get("abc"); - map.delegate.put("abc", new Integer(0)); - try - { - map.get("abc"); - fail(); - } - catch (IllegalStateException e) - { - } - } - - public void testPutWithBrokenGetExternalValue() - { - StringToIntegerMap map =3D new StringToIntegerMap(); - map.externalValueReturnsNull =3D true; - map.put("abc", "0"); - try - { - map.put("abc", "0"); - fail(); - } - catch (IllegalStateException e) - { - } - } - - public void testGetWithInvalidInternalValue() - { - StringToIntegerMap map =3D new StringToIntegerMap(); - map.delegate.put("abc", "0"); - try - { - map.get("abc"); - fail(); - } - catch (ClassCastException expected) - { - } - } - - public void testPutWithInvalidInternalValue() - { - StringToIntegerMap map =3D new StringToIntegerMap(); - map.delegate.put("abc", "0"); - try - { - map.put("abc", "0"); - fail(); - } - catch (ClassCastException expected) - { - } - } - - public void testRemoveWithInvalidInternalValue() - { - StringToIntegerMap map =3D new StringToIntegerMap(); - map.delegate.put("abc", "0"); - try - { - map.remove("abc"); - fail(); - } - catch (ClassCastException expected) - { - } - } - - public void testRemove() - { - StringToIntegerMap map =3D 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 =3D new StringToIntegerMap(); - map.put("abc", "0"); - assertEquals(Collections.singletonMap("abc", new Integer(0)), map.de= legate); - } - - public void testGet() - { - StringToIntegerMap map =3D 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 =3D new StringToIntegerMap(); - left.delegate.put("abc", new Integer(0)); - Map right =3D 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 =3D new HashMap(); - right.put("abc", new Integer(0)); - right.put("def", new Integer(1)); - right.put("ghi", new Integer(2)); - - // - StringToIntegerMap left =3D new StringToIntegerMap(); - left.delegate.putAll(right); - - try - { - left.keySet().retainAll(null); - fail("Was expecting NPE"); - } - catch (NullPointerException expected) - { - } - - // - boolean changed =3D left.keySet().retainAll(CollectionBuilder.hashSe= t().add("abc").add("def").add("ghi").get()); - assertFalse(changed); - assertEquals(right, left.delegate); - - // - changed =3D 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 =3D false; - - /** . */ - boolean externalValueReturnsNull =3D false; - - public StringToIntegerMap() - { - super(new HashMap()); - - // - delegate =3D getDelegate(false); - } - - protected void assertKeyValidity(Object key) throws ClassCastExcepti= on - { - assertNotNull(key); - if (key instanceof String =3D=3D false) - { - throw new ClassCastException(); - } - } - - protected Object getInternalValue(Object value) throws IllegalArgume= ntException, ClassCastException - { - assertNotNull(value); - if (internalValueReturnsNull) - { - return null; - } - String s =3D (String)value; - try - { - return new Integer(s); - } - catch (NumberFormatException e) - { - IllegalArgumentException iae =3D new IllegalArgumentException(= ); - iae.initCause(e); - throw iae; - } - } - - protected Object getExternalValue(Object value) - { - assertNotNull(value); - if (externalValueReturnsNull) - { - return null; - } - Integer i =3D (Integer)value; - return i.toString(); - } - - protected boolean internalValueEquals(Object left, Object right) - { - assertNotNull(left); - assertNotNull(right); - Integer li =3D (Integer)left; - Integer ri =3D (Integer)right; - return li.intValue() =3D=3D ri.intValue(); - } - } - -} Added: modules/common/trunk/common/src/test/java/org/jboss/portal/test/comm= on/util/TypedMapTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- 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 Julien Viet + * @version $Revision: 1.1 $ + */ +public class TypedMapTestCase extends TestCase +{ + + private StringToInteger sti; + + private Map delegate; + + private StringToIntegerMap map; + + public TypedMapTestCase(String name) + { + super(name); + } + + protected void setUp() throws Exception + { + delegate =3D new HashMap(); + sti =3D new StringToInteger(); + map =3D new StringToIntegerMap(delegate, sti); + } + + public void testGetWithBrokenGetInternalValue() + { + sti.internalValueReturnsNull =3D true; + map.get("abc"); + delegate.put("abc", new Integer(0)); + map.get("abc"); + } + + public void testPutWithBrokenGetInternalValue() + { + sti.internalValueReturnsNull =3D true; + try + { + map.put("abc", "0"); + fail(); + } + catch (IllegalArgumentException e) + { + } + } + + public void testRemoveWithBrokenGetInternalValue() + { + sti.internalValueReturnsNull =3D true; + map.remove("abc"); + } + + public void testRemoveWithBrokenGetExternalValue() + { + sti.externalValueReturnsNull =3D true; + map.remove("abc"); + delegate.put("abc", new Integer(0)); + try + { + map.remove("abc"); + fail(); + } + catch (IllegalStateException e) + { + } + } + + public void testGetWithBrokenGetExternalValue() + { + sti.externalValueReturnsNull =3D true; + map.get("abc"); + delegate.put("abc", new Integer(0)); + try + { + map.get("abc"); + fail(); + } + catch (IllegalStateException e) + { + } + } + + public void testPutWithBrokenGetExternalValue() + { + sti.externalValueReturnsNull =3D 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)), delega= te); + } + + 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 leftDelegate =3D new HashMap(); + StringToIntegerMap left =3D new StringToIntegerMap(leftDelegate, sti= ); + leftDelegate.put("abc", new Integer(0)); + Map right =3D 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 =3D new HashMap(); + right.put("abc", new Integer(0)); + right.put("def", new Integer(1)); + right.put("ghi", new Integer(2)); + + // + Map leftDelegate =3D new HashMap(); + StringToIntegerMap left =3D new StringToIntegerMap(leftDelegate, sti= ); + leftDelegate.putAll(right); + + try + { + left.keySet().retainAll(null); + fail("Was expecting NPE"); + } + catch (NullPointerException expected) + { + } + + // + boolean changed =3D left.keySet().retainAll(CollectionBuilder.hashSe= t().add("abc").add("def").add("ghi").get()); + assertFalse(changed); + assertEquals(right, leftDelegate); + + // + changed =3D 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 + { + public StringToIntegerMap(Map map, StringToInteger = sti) + { + super(map, new StringToString(), sti); + } + } + + private static class StringToString extends TypedMap.Converter + { + protected String getInternal(String external) throws IllegalArgument= Exception, 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 + { + + /** . */ + boolean internalValueReturnsNull =3D false; + + /** . */ + boolean externalValueReturnsNull =3D false; + + protected Integer getInternal(String external) throws IllegalArgumen= tException, ClassCastException + { + assertNotNull(external); + + // + if (internalValueReturnsNull) + { + return null; + } + try + { + return new Integer(external); + } + catch (NumberFormatException e) + { + IllegalArgumentException iae =3D 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() =3D=3D right.intValue(); + } + } +} \ No newline at end of file --===============4148238125705886174==--