diff --git a/src/java/org/apache/commons/collections/decorators/FixedSizeMap.java b/src/java/org/apache/commons/collections/decorators/FixedSizeMap.java new file mode 100644 index 000000000..391f46823 --- /dev/null +++ b/src/java/org/apache/commons/collections/decorators/FixedSizeMap.java @@ -0,0 +1,147 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/FixedSizeMap.java,v 1.1 2003/05/09 16:42:35 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.decorators; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * FixedSizeMap decorates another Map + * to fix the size. + *

+ * Any action that would change the size of the map is disallowed. + * The put method is allowed to change the value associated with an existing + * key however. + *

+ * If trying to remove or clear the map, an UnsupportedOperationException is + * thrown. If trying to put a new mapping into the map, an + * IllegalArgumentException is thrown. This is because the put method can + * succeed if the mapping's key already exists in the map, so the put method + * is not always unsupported. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:35 $ + * + * @author Stephen Colebourne + * @author Paul Jack + */ +public class FixedSizeMap extends AbstractMapDecorator implements Map { + + /** + * Factory method to create a fixed size map. + * + * @param map the map to decorate, must not be null + * @throws IllegalArgumentException if map is null + */ + public static Map decorate(Map map) { + return new FixedSizeMap(map); + } + + /** + * Constructor that wraps (not copies). + * + * @param map the map to decorate, must not be null + * @throws IllegalArgumentException if map is null + */ + protected FixedSizeMap(Map map) { + super(map); + } + + //----------------------------------------------------------------------- + public Object put(Object key, Object value) { + if (map.containsKey(key) == false) { + throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size"); + } + return map.put(key, value); + } + + public void putAll(Map map) { + for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { + if (map.containsKey(it.next()) == false) { + throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size"); + } + } + map.putAll(map); + } + + public void clear() { + throw new UnsupportedOperationException("Map is fixed size"); + } + + public Object remove(Object key) { + throw new UnsupportedOperationException("Map is fixed size"); + } + + public Set entrySet() { + Set set = map.entrySet(); + return new UnmodifiableSet(set); + } + + public Set keySet() { + Set set = map.keySet(); + return new UnmodifiableSet(set); + } + + public Collection values() { + Collection coll = map.values(); + return new UnmodifiableCollection(coll); + } + +} diff --git a/src/java/org/apache/commons/collections/decorators/FixedSizeSortedMap.java b/src/java/org/apache/commons/collections/decorators/FixedSizeSortedMap.java new file mode 100644 index 000000000..b5cc7c8ad --- /dev/null +++ b/src/java/org/apache/commons/collections/decorators/FixedSizeSortedMap.java @@ -0,0 +1,142 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/FixedSizeSortedMap.java,v 1.1 2003/05/09 16:42:35 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.decorators; + +import java.util.Comparator; +import java.util.SortedMap; + +/** + * FixedSizeSortedMap decorates another SortedMap + * to fix the size. + *

+ * Any action that would change the size of the map is disallowed. + * The put method is allowed to change the value associated with an existing + * key however. + *

+ * If trying to remove or clear the map, an UnsupportedOperationException is + * thrown. If trying to put a new mapping into the map, an + * IllegalArgumentException is thrown. This is because the put method can + * succeed if the mapping's key already exists in the map, so the put method + * is not always unsupported. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:35 $ + * + * @author Stephen Colebourne + * @author Paul Jack + */ +public class FixedSizeSortedMap extends FixedSizeMap implements SortedMap { + + /** + * Factory method to create a fixed size sorted map. + * + * @param map the map to decorate, must not be null + * @throws IllegalArgumentException if map is null + */ + public static SortedMap decorate(SortedMap map) { + return new FixedSizeSortedMap(map); + } + + /** + * Constructor that wraps (not copies). + * + * @param map the map to decorate, must not be null + * @throws IllegalArgumentException if map is null + */ + protected FixedSizeSortedMap(SortedMap map) { + super(map); + } + + /** + * Gets the map being decorated. + * + * @return the decorated map + */ + protected SortedMap getSortedMap() { + return (SortedMap) map; + } + + //----------------------------------------------------------------------- + public Object firstKey() { + return getSortedMap().firstKey(); + } + + public Object lastKey() { + return getSortedMap().lastKey(); + } + + public Comparator comparator() { + return getSortedMap().comparator(); + } + + public SortedMap subMap(Object fromKey, Object toKey) { + SortedMap map = getSortedMap().subMap(fromKey, toKey); + return new FixedSizeSortedMap(map); + } + + public SortedMap headMap(Object toKey) { + SortedMap map = getSortedMap().headMap(toKey); + return new FixedSizeSortedMap(map); + } + + public SortedMap tailMap(Object fromKey) { + SortedMap map = getSortedMap().tailMap(fromKey); + return new FixedSizeSortedMap(map); + } + +} diff --git a/src/java/org/apache/commons/collections/decorators/LazyMap.java b/src/java/org/apache/commons/collections/decorators/LazyMap.java new file mode 100644 index 000000000..20f32071d --- /dev/null +++ b/src/java/org/apache/commons/collections/decorators/LazyMap.java @@ -0,0 +1,170 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/LazyMap.java,v 1.1 2003/05/09 16:42:35 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.decorators; + +import java.util.Map; + +import org.apache.commons.collections.Factory; +import org.apache.commons.collections.Transformer; + +/** + * LazyMap decorates another Map + * to create objects in the map on demand. + *

+ * When the {@link #get(Object)} method is called with a key that does not + * exist in the map, the factory is used to create the object. The created + * object will be added to the map using the requested key. + *

+ * For instance: + *

+ * Factory factory = new Factory() {
+ *     public Object create() {
+ *         return new Date();
+ *     }
+ * }
+ * Map lazy = Lazy.map(new HashMap(), factory);
+ * Object obj = lazy.get("NOW");
+ * 
+ * + * After the above code is executed, obj will contain + * a new Date instance. Furthermore, that Date + * instance is mapped to the "NOW" key in the map. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:35 $ + * + * @author Stephen Colebourne + * @author Paul Jack + */ +public class LazyMap extends AbstractMapDecorator implements Map { + + /** The factory to use to construct elements */ + protected final Object factory; + + /** + * Factory method to create a lazily instantiated map. + * + * @param map the map to decorate, must not be null + * @param factory the factory to use, must not be null + * @throws IllegalArgumentException if map or factory is null + */ + public static Map decorate(Map map, Factory factory) { + return new LazyMap(map, factory); + } + + /** + * Factory method to create a lazily instantiated map. + * + * @param map the map to decorate, must not be null + * @param factory the factory to use, must not be null + * @throws IllegalArgumentException if map or factory is null + */ + public static Map decorate(Map map, Transformer factory) { + return new LazyMap(map, factory); + } + + /** + * Constructor that wraps (not copies). + * + * @param map the map to decorate, must not be null + * @param factory the factory to use, must not be null + * @throws IllegalArgumentException if map or factory is null + */ + protected LazyMap(Map map, Factory factory) { + super(map); + if (factory == null) { + throw new IllegalArgumentException("Factory must not be null"); + } + this.factory = factory; + } + + /** + * Constructor that wraps (not copies). + * + * @param map the map to decorate, must not be null + * @param factory the factory to use, must not be null + * @throws IllegalArgumentException if map or factory is null + */ + protected LazyMap(Map map, Transformer factory) { + super(map); + if (factory == null) { + throw new IllegalArgumentException("Factory must not be null"); + } + this.factory = factory; + } + + //----------------------------------------------------------------------- + public Object get(Object key) { + // create value for key if key is not currently in the map + if (map.containsKey(key) == false) { + if (factory instanceof Factory) { + Object value = ((Factory) factory).create(); + map.put(key, value); + return value; + } else { + Object value = ((Transformer) factory).transform(key); + map.put(key, value); + return value; + } + } + return map.get(key); + } + + // no need to wrap keySet, entrySet or values as they are views of + // existing map entries - you can't do a map-style get on them. +} diff --git a/src/java/org/apache/commons/collections/decorators/LazySortedMap.java b/src/java/org/apache/commons/collections/decorators/LazySortedMap.java new file mode 100644 index 000000000..4ce7319c5 --- /dev/null +++ b/src/java/org/apache/commons/collections/decorators/LazySortedMap.java @@ -0,0 +1,190 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/LazySortedMap.java,v 1.1 2003/05/09 16:42:35 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.decorators; + +import java.util.Comparator; +import java.util.SortedMap; + +import org.apache.commons.collections.Factory; +import org.apache.commons.collections.Transformer; + +/** + * LazySortedMap decorates another SortedMap + * to create objects in the map on demand. + *

+ * When the {@link #get(Object)} method is called with a key that does not + * exist in the map, the factory is used to create the object. The created + * object will be added to the map using the requested key. + *

+ * For instance: + *

+ * Factory factory = new Factory() {
+ *     public Object create() {
+ *         return new Date();
+ *     }
+ * }
+ * SortedMap lazy = Lazy.sortedMap(new HashMap(), factory);
+ * Object obj = lazy.get("NOW");
+ * 
+ * + * After the above code is executed, obj will contain + * a new Date instance. Furthermore, that Date + * instance is mapped to the "NOW" key in the map. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:35 $ + * + * @author Stephen Colebourne + * @author Paul Jack + */ +public class LazySortedMap extends LazyMap implements SortedMap { + + /** + * Factory method to create a lazily instantiated sorted map. + * + * @param map the map to decorate, must not be null + * @param factory the factory to use, must not be null + * @throws IllegalArgumentException if map or factory is null + */ + public static SortedMap decorate(SortedMap map, Factory factory) { + return new LazySortedMap(map, factory); + } + + /** + * Factory method to create a lazily instantiated sorted map. + * + * @param map the map to decorate, must not be null + * @param factory the factory to use, must not be null + * @throws IllegalArgumentException if map or factory is null + */ + public static SortedMap decorate(SortedMap map, Transformer factory) { + return new LazySortedMap(map, factory); + } + + /** + * Constructor that wraps (not copies). + * + * @param map the map to decorate, must not be null + * @param factory the factory to use, must not be null + * @throws IllegalArgumentException if map or factory is null + */ + protected LazySortedMap(SortedMap map, Factory factory) { + super(map, factory); + } + + /** + * Constructor that wraps (not copies). + * + * @param map the map to decorate, must not be null + * @param factory the factory to use, must not be null + * @throws IllegalArgumentException if map or factory is null + */ + protected LazySortedMap(SortedMap map, Transformer factory) { + super(map, factory); + } + + /** + * Gets the map being decorated. + * + * @return the decorated map + */ + protected SortedMap getSortedMap() { + return (SortedMap) map; + } + + //----------------------------------------------------------------------- + public Object firstKey() { + return getSortedMap().firstKey(); + } + + public Object lastKey() { + return getSortedMap().lastKey(); + } + + public Comparator comparator() { + return getSortedMap().comparator(); + } + + public SortedMap subMap(Object fromKey, Object toKey) { + SortedMap map = getSortedMap().subMap(fromKey, toKey); + if (factory instanceof Factory) { + return new LazySortedMap(map, (Factory) factory); + } else { + return new LazySortedMap(map, (Transformer) factory); + } + } + + public SortedMap headMap(Object toKey) { + SortedMap map = getSortedMap().headMap(toKey); + if (factory instanceof Factory) { + return new LazySortedMap(map, (Factory) factory); + } else { + return new LazySortedMap(map, (Transformer) factory); + } + } + + public SortedMap tailMap(Object fromKey) { + SortedMap map = getSortedMap().tailMap(fromKey); + if (factory instanceof Factory) { + return new LazySortedMap(map, (Factory) factory); + } else { + return new LazySortedMap(map, (Transformer) factory); + } + } + +} diff --git a/src/java/org/apache/commons/collections/decorators/PredicatedMap.java b/src/java/org/apache/commons/collections/decorators/PredicatedMap.java new file mode 100644 index 000000000..6d594bbfc --- /dev/null +++ b/src/java/org/apache/commons/collections/decorators/PredicatedMap.java @@ -0,0 +1,250 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/PredicatedMap.java,v 1.1 2003/05/09 16:42:36 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.decorators; + +import java.lang.reflect.Array; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.collections.Predicate; + +/** + * PredicatedMap decorates another Map + * to validate additions match a specified predicate. + *

+ * If an object cannot be addded to the map, an IllegalArgumentException + * is thrown. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:36 $ + * + * @author Stephen Colebourne + * @author Paul Jack + */ +public class PredicatedMap extends AbstractMapDecorator { + + /** The key predicate to use */ + protected final Predicate keyPredicate; + /** The value predicate to use */ + protected final Predicate valuePredicate; + + /** + * Factory method to create a predicated (validating) map. + *

+ * If there are any elements already in the list being decorated, they + * are validated. + * + * @param map the map to decorate, must not be null + * @param keyPredicate, the predicate to validate the keys, null means no check + * @param valuePredicate, the predicate to validate to values, null means no check + * @throws IllegalArgumentException if the map is null + */ + public static Map decorate(Map map, Predicate keyPredicate, Predicate valuePredicate) { + return new PredicatedMap(map, keyPredicate, valuePredicate); + } + + /** + * Constructor that wraps (not copies). + * + * @param map the map to decorate, must not be null + * @param keyPredicate, the predicate to validate the keys, null means no check + * @param valuePredicate, the predicate to validate to values, null means no check + * @throws IllegalArgumentException if the map is null + */ + protected PredicatedMap(Map map, Predicate keyPredicate, Predicate valuePredicate) { + super(map); + this.keyPredicate = keyPredicate; + this.valuePredicate = valuePredicate; + + Iterator it = map.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry entry = (Map.Entry) it.next(); + Object key = entry.getKey(); + Object value = entry.getValue(); + validate(key, value); + } + } + + protected void validate(Object key, Object value) { + if (keyPredicate != null && keyPredicate.evaluate(key) == false) { + throw new IllegalArgumentException("Cannot add key - Predicate rejected it"); + } + if (valuePredicate != null && valuePredicate.evaluate(value) == false) { + throw new IllegalArgumentException("Cannot add value - Predicate rejected it"); + } + } + + //----------------------------------------------------------------------- + public Object put(Object key, Object value) { + validate(key, value); + return map.put(key, value); + } + + public void putAll(Map map) { + Iterator it = map.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry entry = (Map.Entry) it.next(); + Object key = entry.getKey(); + Object value = entry.getValue(); + validate(key, value); + } + map.putAll(map); + } + + public Set entrySet() { + if (valuePredicate == null) { + return map.entrySet(); + } + return new PredicatedMapEntrySet(map.entrySet(), valuePredicate); + } + + + //----------------------------------------------------------------------- + /** + * Implementation of an entry set that checks (predicates) additions. + */ + protected static class PredicatedMapEntrySet extends AbstractCollectionDecorator implements Set { + + /** The predicate to use */ + private final Predicate valuePredicate; + + protected PredicatedMapEntrySet(Set set, Predicate valuePred) { + super(set); + this.valuePredicate = valuePred; + } + + public Iterator iterator() { + return new PredicatedMapEntrySetIterator(collection.iterator(), valuePredicate); + } + + public Object[] toArray() { + Object[] array = collection.toArray(); + for (int i = 0; i < array.length; i++) { + array[i] = new PredicatedMapEntry((Map.Entry) array[i], valuePredicate); + } + return array; + } + + public Object[] toArray(Object array[]) { + Object[] result = array; + if (array.length > 0) { + // we must create a new array to handle multi-threaded situations + // where another thread could access data before we decorate it + result = (Object[]) Array.newInstance(array.getClass().getComponentType(), 0); + } + result = collection.toArray(result); + for (int i = 0; i < result.length; i++) { + result[i] = new PredicatedMapEntry((Map.Entry) result[i], valuePredicate); + } + + // check to see if result should be returned straight + if (result.length > array.length) { + return result; + } + + // copy back into input array to fulfil the method contract + System.arraycopy(result, 0, array, 0, result.length); + if (array.length > result.length) { + array[result.length] = null; + } + return array; + } + } + + /** + * Implementation of an entry set iterator. + */ + protected static class PredicatedMapEntrySetIterator extends AbstractIteratorDecorator { + + /** The predicate to use */ + private final Predicate valuePredicate; + + protected PredicatedMapEntrySetIterator(Iterator iterator, Predicate valuePredicate) { + super(iterator); + this.valuePredicate = valuePredicate; + } + + public Object next() { + Map.Entry entry = (Map.Entry) iterator.next(); + return new PredicatedMapEntry(entry, valuePredicate); + } + } + + /** + * Implementation of a map entry that checks (predicates) additions. + */ + protected static class PredicatedMapEntry extends AbstractMapEntryDecorator { + + /** The predicate to use */ + private final Predicate predicate; + + protected PredicatedMapEntry(Map.Entry entry, Predicate valuePredicate) { + super(entry); + this.predicate = valuePredicate; + } + + public Object setValue(Object o) { + if (predicate != null && predicate.evaluate(o) == false) { + throw new IllegalArgumentException("Cannot set value - Predicate rejected it"); + } + return entry.setValue(o); + } + } + +} diff --git a/src/java/org/apache/commons/collections/decorators/PredicatedSortedMap.java b/src/java/org/apache/commons/collections/decorators/PredicatedSortedMap.java new file mode 100644 index 000000000..c2d90fbf5 --- /dev/null +++ b/src/java/org/apache/commons/collections/decorators/PredicatedSortedMap.java @@ -0,0 +1,144 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/PredicatedSortedMap.java,v 1.1 2003/05/09 16:42:35 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.decorators; + +import java.util.Comparator; +import java.util.SortedMap; + +import org.apache.commons.collections.Predicate; + +/** + * PredicatedSortedMap decorates another SortedMap + * to validate additions match a specified predicate. + *

+ * If an object cannot be addded to the map, an IllegalArgumentException + * is thrown. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:35 $ + * + * @author Stephen Colebourne + * @author Paul Jack + */ +public class PredicatedSortedMap extends PredicatedMap implements SortedMap { + + /** + * Factory method to create a predicated (validating) sorted map. + *

+ * If there are any elements already in the list being decorated, they + * are validated. + * + * @param map the map to decorate, must not be null + * @param keyPredicate, the predicate to validate the keys, null means no check + * @param valuePredicate, the predicate to validate to values, null means no check + * @throws IllegalArgumentException if the map is null + */ + public static SortedMap decorate(SortedMap map, Predicate keyPredicate, Predicate valuePredicate) { + return new PredicatedSortedMap(map, keyPredicate, valuePredicate); + } + + /** + * Constructor that wraps (not copies). + * + * @param map the map to decorate, must not be null + * @param keyPredicate, the predicate to validate the keys, null means no check + * @param valuePredicate, the predicate to validate to values, null means no check + * @throws IllegalArgumentException if the map is null + */ + protected PredicatedSortedMap(SortedMap map, Predicate keyPredicate, Predicate valuePredicate) { + super(map, keyPredicate, valuePredicate); + } + + /** + * Gets the map being decorated. + * + * @return the decorated map + */ + protected SortedMap getSortedMap() { + return (SortedMap) map; + } + + //----------------------------------------------------------------------- + public Object firstKey() { + return getSortedMap().firstKey(); + } + + public Object lastKey() { + return getSortedMap().lastKey(); + } + + public Comparator comparator() { + return getSortedMap().comparator(); + } + + public SortedMap subMap(Object fromKey, Object toKey) { + SortedMap map = getSortedMap().subMap(fromKey, toKey); + return new PredicatedSortedMap(map, keyPredicate, valuePredicate); + } + + public SortedMap headMap(Object toKey) { + SortedMap map = getSortedMap().headMap(toKey); + return new PredicatedSortedMap(map, keyPredicate, valuePredicate); + } + + public SortedMap tailMap(Object fromKey) { + SortedMap map = getSortedMap().tailMap(fromKey); + return new PredicatedSortedMap(map, keyPredicate, valuePredicate); + } + +} diff --git a/src/java/org/apache/commons/collections/decorators/TypedMap.java b/src/java/org/apache/commons/collections/decorators/TypedMap.java new file mode 100644 index 000000000..be770833d --- /dev/null +++ b/src/java/org/apache/commons/collections/decorators/TypedMap.java @@ -0,0 +1,104 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/TypedMap.java,v 1.1 2003/05/09 16:42:36 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.decorators; + +import java.util.Map; + +/** + * TypedMap decorates another Map + * to validate that elements added are of a specific type. + *

+ * The validation of additions is performed via an instanceof test against + * a specified Class. If an object cannot be addded to the + * collection, an IllegalArgumentException is thrown. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:36 $ + * + * @author Stephen Colebourne + * @author Matthew Hawthorne + */ +public class TypedMap { + + /** + * Factory method to create a typed map. + *

+ * If there are any elements already in the map being decorated, they + * are validated. + * + * @param map the map to decorate, must not be null + * @param keyType the type to allow as keys, must not be null + * @param valueType the type to allow as values, must not be null + * @throws IllegalArgumentException if list or type is null + * @throws IllegalArgumentException if the list contains invalid elements + */ + public static Map decorate(Map map, Class keyType, Class valueType) { + return new PredicatedMap( + map, + TypedCollection.getPredicate(keyType), + TypedCollection.getPredicate(valueType) + ); + } + + /** + * Restrictive constructor. + */ + protected TypedMap() { + } + +} diff --git a/src/java/org/apache/commons/collections/decorators/TypedSortedMap.java b/src/java/org/apache/commons/collections/decorators/TypedSortedMap.java new file mode 100644 index 000000000..5643d129f --- /dev/null +++ b/src/java/org/apache/commons/collections/decorators/TypedSortedMap.java @@ -0,0 +1,104 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/TypedSortedMap.java,v 1.1 2003/05/09 16:42:35 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.decorators; + +import java.util.SortedMap; + +/** + * TypedSortedMap decorates another SortedMap + * to validate that elements added are of a specific type. + *

+ * The validation of additions is performed via an instanceof test against + * a specified Class. If an object cannot be addded to the + * collection, an IllegalArgumentException is thrown. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:35 $ + * + * @author Stephen Colebourne + * @author Matthew Hawthorne + */ +public class TypedSortedMap { + + /** + * Factory method to create a typed sorted map. + *

+ * If there are any elements already in the map being decorated, they + * are validated. + * + * @param map the map to decorate, must not be null + * @param keyType the type to allow as keys, must not be null + * @param valueType the type to allow as values, must not be null + * @throws IllegalArgumentException if list or type is null + * @throws IllegalArgumentException if the list contains invalid elements + */ + public static SortedMap decorate(SortedMap map, Class keyType, Class valueType) { + return new PredicatedSortedMap( + map, + TypedCollection.getPredicate(keyType), + TypedCollection.getPredicate(valueType) + ); + } + + /** + * Restrictive constructor. + */ + protected TypedSortedMap() { + } + +} diff --git a/src/java/org/apache/commons/collections/decorators/UnmodifiableMap.java b/src/java/org/apache/commons/collections/decorators/UnmodifiableMap.java new file mode 100644 index 000000000..c24cf2855 --- /dev/null +++ b/src/java/org/apache/commons/collections/decorators/UnmodifiableMap.java @@ -0,0 +1,206 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/UnmodifiableMap.java,v 1.1 2003/05/09 16:42:36 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.decorators; + +import java.lang.reflect.Array; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * UnmodifiableMap decorates another Map + * to ensure it can't be altered. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:36 $ + * + * @author Stephen Colebourne + */ +public class UnmodifiableMap extends AbstractMapDecorator { + + /** + * Factory method to create an unmodifiable map. + * + * @param map the map to decorate, must not be null + * @throws IllegalArgumentException if map is null + */ + public static Map decorate(Map map) { + return new UnmodifiableMap(map); + } + + /** + * Constructor that wraps (not copies). + * + * @param map the map to decorate, must not be null + * @throws IllegalArgumentException if map is null + */ + protected UnmodifiableMap(Map map) { + super(map); + } + + //----------------------------------------------------------------------- + public void clear() { + throw new UnsupportedOperationException(); + } + + public Object put(Object key, Object value) { + throw new UnsupportedOperationException(); + } + + public void putAll(Map map) { + throw new UnsupportedOperationException(); + } + + public Object remove(Object key) { + throw new UnsupportedOperationException(); + } + + public Set entrySet() { + Set set = super.entrySet(); + return new UnmodifiableEntrySet(set); + } + + public Set keySet() { + Set set = super.keySet(); + return new UnmodifiableSet(set); + } + + public Collection values() { + Collection coll = super.values(); + return new UnmodifiableCollection(coll); + } + + //----------------------------------------------------------------------- + /** + * Implementation of an entry set that checks (predicates) additions. + */ + protected static class UnmodifiableEntrySet extends UnmodifiableSet { + + protected UnmodifiableEntrySet(Set set) { + super(set); + } + + public Iterator iterator() { + return new UnmodifiableEntrySetIterator(collection.iterator()); + } + + public Object[] toArray() { + Object[] array = collection.toArray(); + for (int i = 0; i < array.length; i++) { + array[i] = new UnmodifiableEntry((Map.Entry) array[i]); + } + return array; + } + + public Object[] toArray(Object array[]) { + Object[] result = array; + if (array.length > 0) { + // we must create a new array to handle multi-threaded situations + // where another thread could access data before we decorate it + result = (Object[]) Array.newInstance(array.getClass().getComponentType(), 0); + } + result = collection.toArray(result); + for (int i = 0; i < result.length; i++) { + result[i] = new UnmodifiableEntry((Map.Entry) result[i]); + } + + // check to see if result should be returned straight + if (result.length > array.length) { + return result; + } + + // copy back into input array to fulfil the method contract + System.arraycopy(result, 0, array, 0, result.length); + if (array.length > result.length) { + array[result.length] = null; + } + return array; + } + } + + /** + * Implementation of an entry set iterator. + */ + protected static class UnmodifiableEntrySetIterator extends AbstractIteratorDecorator { + + protected UnmodifiableEntrySetIterator(Iterator iterator) { + super(iterator); + } + + public Object next() { + Map.Entry entry = (Map.Entry) iterator.next(); + return new UnmodifiableEntry(entry); + } + } + + /** + * Implementation of a map entry that is unmodifiable. + */ + protected static class UnmodifiableEntry extends AbstractMapEntryDecorator { + + protected UnmodifiableEntry(Map.Entry entry) { + super(entry); + } + + public Object setValue(Object o) { + throw new UnsupportedOperationException(); + } + } + +} diff --git a/src/java/org/apache/commons/collections/decorators/UnmodifiableSortedMap.java b/src/java/org/apache/commons/collections/decorators/UnmodifiableSortedMap.java new file mode 100644 index 000000000..06bee790a --- /dev/null +++ b/src/java/org/apache/commons/collections/decorators/UnmodifiableSortedMap.java @@ -0,0 +1,132 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/UnmodifiableSortedMap.java,v 1.1 2003/05/09 16:42:36 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.decorators; + +import java.util.Comparator; +import java.util.Map; +import java.util.SortedMap; + +/** + * UnmodifiableSortedMap decorates another SortedMap + * to ensure it can't be altered. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:36 $ + * + * @author Stephen Colebourne + */ +public class UnmodifiableSortedMap extends UnmodifiableMap implements SortedMap { + + /** + * Factory method to create an unmodifiable sorted map. + * + * @param map the map to decorate, must not be null + * @throws IllegalArgumentException if map is null + */ + public static SortedMap decorate(SortedMap map) { + return new UnmodifiableSortedMap(map); + } + + /** + * Constructor that wraps (not copies). + * + * @param map the map to decorate, must not be null + * @throws IllegalArgumentException if map is null + */ + protected UnmodifiableSortedMap(Map map) { + super(map); + } + + /** + * Gets the map being decorated. + * + * @return the decorated map + */ + protected SortedMap getSortedMap() { + return (SortedMap) map; + } + + //----------------------------------------------------------------------- + public Object firstKey() { + return getSortedMap().firstKey(); + } + + public Object lastKey() { + return getSortedMap().lastKey(); + } + + public Comparator comparator() { + return getSortedMap().comparator(); + } + + public SortedMap subMap(Object fromKey, Object toKey) { + SortedMap map = getSortedMap().subMap(fromKey, toKey); + return new UnmodifiableSortedMap(getSortedMap()); + } + + public SortedMap headMap(Object toKey) { + SortedMap map = getSortedMap().headMap(toKey); + return new UnmodifiableSortedMap(getSortedMap()); + } + + public SortedMap tailMap(Object fromKey) { + SortedMap map = getSortedMap().tailMap(fromKey); + return new UnmodifiableSortedMap(getSortedMap()); + } + +}