Add Map and SortedMap collection decorators

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131054 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-05-09 16:42:36 +00:00
parent 1c77aedc23
commit ebc5684efd
10 changed files with 1589 additions and 0 deletions

View File

@ -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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.decorators;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* <code>FixedSizeMap</code> decorates another <code>Map</code>
* to fix the size.
* <p>
* 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.
* <p>
* 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);
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.decorators;
import java.util.Comparator;
import java.util.SortedMap;
/**
* <code>FixedSizeSortedMap</code> decorates another <code>SortedMap</code>
* to fix the size.
* <p>
* 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.
* <p>
* 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);
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.decorators;
import java.util.Map;
import org.apache.commons.collections.Factory;
import org.apache.commons.collections.Transformer;
/**
* <code>LazyMap</code> decorates another <code>Map</code>
* to create objects in the map on demand.
* <p>
* 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.
* <p>
* For instance:
* <pre>
* Factory factory = new Factory() {
* public Object create() {
* return new Date();
* }
* }
* Map lazy = Lazy.map(new HashMap(), factory);
* Object obj = lazy.get("NOW");
* </pre>
*
* After the above code is executed, <code>obj</code> will contain
* a new <code>Date</code> instance. Furthermore, that <code>Date</code>
* 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.
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
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;
/**
* <code>LazySortedMap</code> decorates another <code>SortedMap </code>
* to create objects in the map on demand.
* <p>
* 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.
* <p>
* For instance:
* <pre>
* Factory factory = new Factory() {
* public Object create() {
* return new Date();
* }
* }
* SortedMap lazy = Lazy.sortedMap(new HashMap(), factory);
* Object obj = lazy.get("NOW");
* </pre>
*
* After the above code is executed, <code>obj</code> will contain
* a new <code>Date</code> instance. Furthermore, that <code>Date</code>
* 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);
}
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
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;
/**
* <code>PredicatedMap</code> decorates another <code>Map</code>
* to validate additions match a specified predicate.
* <p>
* 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.
* <p>
* 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);
}
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.decorators;
import java.util.Comparator;
import java.util.SortedMap;
import org.apache.commons.collections.Predicate;
/**
* <code>PredicatedSortedMap</code> decorates another <code>SortedMap </code>
* to validate additions match a specified predicate.
* <p>
* 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.
* <p>
* 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);
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.decorators;
import java.util.Map;
/**
* <code>TypedMap</code> decorates another <code>Map</code>
* to validate that elements added are of a specific type.
* <p>
* The validation of additions is performed via an instanceof test against
* a specified <code>Class</code>. 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.
* <p>
* 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() {
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.decorators;
import java.util.SortedMap;
/**
* <code>TypedSortedMap</code> decorates another <code>SortedMap</code>
* to validate that elements added are of a specific type.
* <p>
* The validation of additions is performed via an instanceof test against
* a specified <code>Class</code>. 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.
* <p>
* 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() {
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
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;
/**
* <code>UnmodifiableMap</code> decorates another <code>Map</code>
* 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();
}
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.decorators;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;
/**
* <code>UnmodifiableSortedMap</code> decorates another <code>SortedMap</code>
* 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());
}
}