Removed Typed* containers such as TypedList and TypedMap as generics now provides type safety

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/branches/collections_jdk5_branch@471166 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2006-11-04 11:33:22 +00:00
parent dacbc93ded
commit 88cbfb7236
27 changed files with 4 additions and 1308 deletions

View File

@ -35,6 +35,9 @@ Changes from commons-collections
- Removed FastTreeSet
- no direct replacement - use ConcurrentHashMap or synchronized TreeMap
- Removed Typed* containers such as TypedList and TypedMap
- use generics for type safety, or Collections.checked*()
Feedback
--------

View File

@ -24,8 +24,6 @@ import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.apache.commons.collections.bag.TransformedBag;
import org.apache.commons.collections.bag.TransformedSortedBag;
import org.apache.commons.collections.bag.TreeBag;
import org.apache.commons.collections.bag.TypedBag;
import org.apache.commons.collections.bag.TypedSortedBag;
import org.apache.commons.collections.bag.UnmodifiableBag;
import org.apache.commons.collections.bag.UnmodifiableSortedBag;
@ -121,19 +119,6 @@ public class BagUtils {
return PredicatedBag.decorate(bag, predicate);
}
/**
* Returns a typed bag backed by the given bag.
* <p>
* Only objects of the specified type can be added to the bag.
*
* @param bag the bag to limit to a specific type, must not be null
* @param type the type of objects which may be added to the bag
* @return a typed bag backed by the specified bag
*/
public static Bag typedBag(Bag bag, Class type) {
return TypedBag.decorate(bag, type);
}
/**
* Returns a transformed bag backed by the given bag.
* <p>
@ -212,19 +197,6 @@ public class BagUtils {
return PredicatedSortedBag.decorate(bag, predicate);
}
/**
* Returns a typed sorted bag backed by the given bag.
* <p>
* Only objects of the specified type can be added to the bag.
*
* @param bag the bag to limit to a specific type, must not be null
* @param type the type of objects which may be added to the bag
* @return a typed bag backed by the specified bag
*/
public static SortedBag typedSortedBag(SortedBag bag, Class type) {
return TypedSortedBag.decorate(bag, type);
}
/**
* Returns a transformed sorted bag backed by the given bag.
* <p>

View File

@ -17,12 +17,11 @@
package org.apache.commons.collections;
import org.apache.commons.collections.buffer.BlockingBuffer;
import org.apache.commons.collections.buffer.BoundedBuffer;
import org.apache.commons.collections.buffer.PredicatedBuffer;
import org.apache.commons.collections.buffer.SynchronizedBuffer;
import org.apache.commons.collections.buffer.TransformedBuffer;
import org.apache.commons.collections.buffer.TypedBuffer;
import org.apache.commons.collections.buffer.UnmodifiableBuffer;
import org.apache.commons.collections.buffer.BoundedBuffer;
/**
* Provides utility methods and decorators for {@link Buffer} instances.
@ -168,20 +167,6 @@ public class BufferUtils {
return PredicatedBuffer.decorate(buffer, predicate);
}
/**
* Returns a typed buffer backed by the given buffer.
* <p>
* Only elements of the specified type can be added to the buffer.
*
* @param buffer the buffer to predicate, must not be null
* @param type the type to allow into the buffer, must not be null
* @return a typed buffer
* @throws IllegalArgumentException if the buffer or type is null
*/
public static Buffer typedBuffer(Buffer buffer, Class type) {
return TypedBuffer.decorate(buffer, type);
}
/**
* Returns a transformed buffer backed by the given buffer.
* <p>

View File

@ -31,7 +31,6 @@ import java.util.Set;
import org.apache.commons.collections.collection.PredicatedCollection;
import org.apache.commons.collections.collection.SynchronizedCollection;
import org.apache.commons.collections.collection.TransformedCollection;
import org.apache.commons.collections.collection.TypedCollection;
import org.apache.commons.collections.collection.UnmodifiableBoundedCollection;
import org.apache.commons.collections.collection.UnmodifiableCollection;
@ -1072,19 +1071,6 @@ public class CollectionUtils {
return PredicatedCollection.decorate(collection, predicate);
}
/**
* Returns a typed collection backed by the given collection.
* <p>
* Only objects of the specified type can be added to the collection.
*
* @param collection the collection to limit to a specific type, must not be null
* @param type the type of objects which may be added to the collection
* @return a typed collection backed by the specified collection
*/
public static Collection typedCollection(Collection collection, Class type) {
return TypedCollection.decorate(collection, type);
}
/**
* Returns a transformed bag backed by the given collection.
* <p>

View File

@ -27,7 +27,6 @@ import org.apache.commons.collections.list.LazyList;
import org.apache.commons.collections.list.PredicatedList;
import org.apache.commons.collections.list.SynchronizedList;
import org.apache.commons.collections.list.TransformedList;
import org.apache.commons.collections.list.TypedList;
import org.apache.commons.collections.list.UnmodifiableList;
/**
@ -331,19 +330,6 @@ public class ListUtils {
return PredicatedList.decorate(list, predicate);
}
/**
* Returns a typed list backed by the given list.
* <p>
* Only objects of the specified type can be added to the list.
*
* @param list the list to limit to a specific type, must not be null
* @param type the type of objects which may be added to the list
* @return a typed list backed by the specified list
*/
public static List typedList(List list, Class type) {
return TypedList.decorate(list, type);
}
/**
* Returns a transformed list backed by the given list.
* <p>

View File

@ -39,8 +39,6 @@ import org.apache.commons.collections.map.PredicatedMap;
import org.apache.commons.collections.map.PredicatedSortedMap;
import org.apache.commons.collections.map.TransformedMap;
import org.apache.commons.collections.map.TransformedSortedMap;
import org.apache.commons.collections.map.TypedMap;
import org.apache.commons.collections.map.TypedSortedMap;
import org.apache.commons.collections.map.UnmodifiableMap;
import org.apache.commons.collections.map.UnmodifiableSortedMap;
@ -1273,21 +1271,6 @@ public class MapUtils {
return PredicatedMap.decorate(map, keyPred, valuePred);
}
/**
* Returns a typed map backed by the given map.
* <p>
* Only keys and values of the specified types can be added to the map.
*
* @param map the map to limit to a specific type, must not be null
* @param keyType the type of keys which may be added to the map, must not be null
* @param valueType the type of values which may be added to the map, must not be null
* @return a typed map backed by the specified map
* @throws IllegalArgumentException if the Map or Class is null
*/
public static Map typedMap(Map map, Class keyType, Class valueType) {
return TypedMap.decorate(map, keyType, valueType);
}
/**
* Returns a transformed map backed by the given map.
* <p>
@ -1517,20 +1500,6 @@ public class MapUtils {
return PredicatedSortedMap.decorate(map, keyPred, valuePred);
}
/**
* Returns a typed sorted map backed by the given map.
* <p>
* Only keys and values of the specified types can be added to the map.
*
* @param map the map to limit to a specific type, must not be null
* @param keyType the type of keys which may be added to the map, must not be null
* @param valueType the type of values which may be added to the map, must not be null
* @return a typed map backed by the specified map
*/
public static SortedMap typedSortedMap(SortedMap map, Class keyType, Class valueType) {
return TypedSortedMap.decorate(map, keyType, valueType);
}
/**
* Returns a transformed sorted map backed by the given map.
* <p>

View File

@ -30,8 +30,6 @@ import org.apache.commons.collections.set.SynchronizedSet;
import org.apache.commons.collections.set.SynchronizedSortedSet;
import org.apache.commons.collections.set.TransformedSet;
import org.apache.commons.collections.set.TransformedSortedSet;
import org.apache.commons.collections.set.TypedSet;
import org.apache.commons.collections.set.TypedSortedSet;
import org.apache.commons.collections.set.UnmodifiableSet;
import org.apache.commons.collections.set.UnmodifiableSortedSet;
@ -193,19 +191,6 @@ public class SetUtils {
return PredicatedSet.decorate(set, predicate);
}
/**
* Returns a typed set backed by the given set.
* <p>
* Only objects of the specified type can be added to the set.
*
* @param set the set to limit to a specific type, must not be null
* @param type the type of objects which may be added to the set
* @return a typed set backed by the specified set
*/
public static Set typedSet(Set set, Class type) {
return TypedSet.decorate(set, type);
}
/**
* Returns a transformed set backed by the given set.
* <p>
@ -294,19 +279,6 @@ public class SetUtils {
return PredicatedSortedSet.decorate(set, predicate);
}
/**
* Returns a typed sorted set backed by the given set.
* <p>
* Only objects of the specified type can be added to the set.
*
* @param set the set to limit to a specific type, must not be null
* @param type the type of objects which may be added to the set
* @return a typed set backed by the specified set
*/
public static SortedSet typedSortedSet(SortedSet set, Class type) {
return TypedSortedSet.decorate(set, type);
}
/**
* Returns a transformed sorted set backed by the given set.
* <p>

View File

@ -1,61 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.bag;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.functors.InstanceofPredicate;
/**
* Decorates another <code>Bag</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 added to the
* collection, an IllegalArgumentException is thrown.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
*/
public class TypedBag {
/**
* Factory method to create a typed bag.
* <p>
* If there are any elements already in the bag being decorated, they
* are validated.
*
* @param bag the bag to decorate, must not be null
* @param type the type to allow into the bag, must not be null
* @return a new typed Bag
* @throws IllegalArgumentException if bag or type is null
* @throws IllegalArgumentException if the bag contains invalid elements
*/
public static Bag decorate(Bag bag, Class type) {
return new PredicatedBag(bag, InstanceofPredicate.getInstance(type));
}
/**
* Restrictive constructor.
*/
protected TypedBag() {
super();
}
}

View File

@ -1,61 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.bag;
import org.apache.commons.collections.SortedBag;
import org.apache.commons.collections.functors.InstanceofPredicate;
/**
* Decorates another <code>SortedBag</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 added to the
* collection, an IllegalArgumentException is thrown.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
*/
public class TypedSortedBag {
/**
* Factory method to create a typed sorted bag.
* <p>
* If there are any elements already in the bag being decorated, they
* are validated.
*
* @param bag the bag to decorate, must not be null
* @param type the type to allow into the bag, must not be null
* @return a new transformed SortedBag
* @throws IllegalArgumentException if bag or type is null
* @throws IllegalArgumentException if the bag contains invalid elements
*/
public static SortedBag decorate(SortedBag bag, Class type) {
return new PredicatedSortedBag(bag, InstanceofPredicate.getInstance(type));
}
/**
* Restrictive constructor.
*/
protected TypedSortedBag() {
super();
}
}

View File

@ -1,61 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.buffer;
import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.functors.InstanceofPredicate;
/**
* Decorates another <code>Buffer</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 added to the
* collection, an IllegalArgumentException is thrown.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
*/
public class TypedBuffer {
/**
* Factory method to create a typed list.
* <p>
* If there are any elements already in the buffer being decorated, they
* are validated.
*
* @param buffer the buffer to decorate, must not be null
* @param type the type to allow into the buffer, must not be null
* @return a new typed Buffer
* @throws IllegalArgumentException if buffer or type is null
* @throws IllegalArgumentException if the buffer contains invalid elements
*/
public static Buffer decorate(Buffer buffer, Class type) {
return new PredicatedBuffer(buffer, InstanceofPredicate.getInstance(type));
}
/**
* Restrictive constructor.
*/
protected TypedBuffer() {
super();
}
}

View File

@ -1,61 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.collection;
import java.util.Collection;
import org.apache.commons.collections.functors.InstanceofPredicate;
/**
* Decorates a <code>Collection</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 added to the
* collection, an IllegalArgumentException is thrown.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
*/
public class TypedCollection {
/**
* Factory method to create a typed collection.
* <p>
* If there are any elements already in the collection being decorated, they
* are validated.
*
* @param coll the collection to decorate, must not be null
* @param type the type to allow into the collection, must not be null
* @return a new typed collection
* @throws IllegalArgumentException if collection or type is null
* @throws IllegalArgumentException if the collection contains invalid elements
*/
public static Collection decorate(Collection coll, Class type) {
return new PredicatedCollection(coll, InstanceofPredicate.getInstance(type));
}
/**
* Restrictive constructor.
*/
protected TypedCollection() {
super();
}
}

View File

@ -1,60 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.list;
import java.util.List;
import org.apache.commons.collections.functors.InstanceofPredicate;
/**
* Decorates another <code>List</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 added to the
* collection, an IllegalArgumentException is thrown.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
*/
public class TypedList {
/**
* Factory method to create a typed list.
* <p>
* If there are any elements already in the list being decorated, they
* are validated.
*
* @param list the list to decorate, must not be null
* @param type the type to allow into the collection, must not be null
* @throws IllegalArgumentException if list or type is null
* @throws IllegalArgumentException if the list contains invalid elements
*/
public static List decorate(List list, Class type) {
return new PredicatedList(list, InstanceofPredicate.getInstance(type));
}
/**
* Restrictive constructor.
*/
protected TypedList() {
}
}

View File

@ -1,73 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.map;
import java.util.Map;
import org.apache.commons.collections.functors.InstanceofPredicate;
/**
* 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 added to the
* collection, an IllegalArgumentException is thrown.
* <p>
* <strong>Note that TypedMap is not synchronized and is not thread-safe.</strong>
* If you wish to use this map from multiple threads concurrently, you must use
* appropriate synchronization. The simplest approach is to wrap this map
* using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw
* exceptions when accessed by concurrent threads without synchronization.
* <p>
* The returned implementation is Serializable from Commons Collections 3.1.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @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,
InstanceofPredicate.getInstance(keyType),
InstanceofPredicate.getInstance(valueType)
);
}
/**
* Restrictive constructor.
*/
protected TypedMap() {
}
}

View File

@ -1,73 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.map;
import java.util.SortedMap;
import org.apache.commons.collections.functors.InstanceofPredicate;
/**
* 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 added to the
* collection, an IllegalArgumentException is thrown.
* <p>
* <strong>Note that TypedSortedMap is not synchronized and is not thread-safe.</strong>
* If you wish to use this map from multiple threads concurrently, you must use
* appropriate synchronization. The simplest approach is to wrap this map
* using {@link java.util.Collections#synchronizedSortedMap}. This class may throw
* exceptions when accessed by concurrent threads without synchronization.
* <p>
* The returned implementation is Serializable from Commons Collections 3.1.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @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,
InstanceofPredicate.getInstance(keyType),
InstanceofPredicate.getInstance(valueType)
);
}
/**
* Restrictive constructor.
*/
protected TypedSortedMap() {
}
}

View File

@ -1,60 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.set;
import java.util.Set;
import org.apache.commons.collections.functors.InstanceofPredicate;
/**
* Decorates another <code>Set</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 added to the
* collection, an IllegalArgumentException is thrown.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
*/
public class TypedSet {
/**
* Factory method to create a typed set.
* <p>
* If there are any elements already in the set being decorated, they
* are validated.
*
* @param set the set to decorate, must not be null
* @param type the type to allow into the collection, must not be null
* @throws IllegalArgumentException if set or type is null
* @throws IllegalArgumentException if the set contains invalid elements
*/
public static Set decorate(Set set, Class type) {
return new PredicatedSet(set, InstanceofPredicate.getInstance(type));
}
/**
* Restrictive constructor.
*/
protected TypedSet() {
}
}

View File

@ -1,60 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.set;
import java.util.SortedSet;
import org.apache.commons.collections.functors.InstanceofPredicate;
/**
* Decorates another <code>SortedSet</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 added to the
* collection, an IllegalArgumentException is thrown.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matthew Hawthorne
*/
public class TypedSortedSet {
/**
* Factory method to create a typed sorted set.
* <p>
* If there are any elements already in the set being decorated, they
* are validated.
*
* @param set the set to decorate, must not be null
* @param type the type to allow into the collection, must not be null
* @throws IllegalArgumentException if set or type is null
* @throws IllegalArgumentException if the set contains invalid elements
*/
public static SortedSet decorate(SortedSet set, Class type) {
return new PredicatedSortedSet(set, InstanceofPredicate.getInstance(type));
}
/**
* Restrictive constructor.
*/
protected TypedSortedSet() {
}
}

View File

@ -97,24 +97,6 @@ public class TestBagUtils extends BulkTest {
}
}
public void testTypedBag() {
Bag bag = BagUtils.typedBag(new HashBag(), stringClass);
assertTrue("Returned object should be a TypedBag.",
bag instanceof PredicatedBag);
try {
bag = BagUtils.typedBag(null, stringClass);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
bag = BagUtils.typedBag(new HashBag(), null);
fail("Expecting IllegalArgumentException for null type.");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testTransformedBag() {
Bag bag = BagUtils.transformedBag(new HashBag(), nopTransformer);
assertTrue("Returned object should be an TransformedBag.",
@ -175,24 +157,6 @@ public class TestBagUtils extends BulkTest {
}
}
public void testTypedSortedBag() {
Bag bag = BagUtils.typedSortedBag(new TreeBag(), stringClass);
assertTrue("Returned object should be a TypedSortedBag.",
bag instanceof PredicatedSortedBag);
try {
bag = BagUtils.typedSortedBag(null, stringClass);
fail("Expecting IllegalArgumentException for null bag.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
bag = BagUtils.typedSortedBag(new TreeBag(), null);
fail("Expecting IllegalArgumentException for null type.");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testTransformedSortedBag() {
Bag bag = BagUtils.transformedSortedBag(new TreeBag(), nopTransformer);
assertTrue("Returned object should be an TransformedSortedBag",

View File

@ -36,7 +36,6 @@ import junit.framework.TestSuite;
import org.apache.commons.collections.bag.HashBag;
import org.apache.commons.collections.buffer.BoundedFifoBuffer;
import org.apache.commons.collections.collection.AbstractTestCollection;
import org.apache.commons.collections.collection.PredicatedCollection;
import org.apache.commons.collections.collection.SynchronizedCollection;
import org.apache.commons.collections.collection.TransformedCollection;
@ -998,43 +997,6 @@ public class TestCollectionUtils extends TestCase {
public BulkTest bulkTestTypedCollection() {
return new TestTypedCollection("") {
public Collection typedCollection() {
return CollectionUtils.typedCollection(
new ArrayList(),
super.getType());
}
public BulkTest bulkTestAll() {
return new AbstractTestCollection("") {
public Collection makeCollection() {
return typedCollection();
}
public Collection makeConfirmedCollection() {
return new ArrayList();
}
public Collection makeConfirmedFullCollection() {
ArrayList list = new ArrayList();
list.addAll(java.util.Arrays.asList(getFullElements()));
return list;
}
public Object[] getFullElements() {
return getFullNonNullStringElements();
}
public Object[] getOtherElements() {
return getOtherNonNullStringElements();
}
};
}
};
}
public void testIsFull() {
Set set = new HashSet();
set.add("1");

View File

@ -33,7 +33,6 @@ import org.apache.commons.collections.keyvalue.DefaultKeyValue;
import org.apache.commons.collections.keyvalue.DefaultMapEntry;
import org.apache.commons.collections.map.LazyMap;
import org.apache.commons.collections.map.PredicatedMap;
import org.apache.commons.collections.map.TestPredicatedMap;
/**
* Tests for MapUtils.
@ -78,68 +77,6 @@ public class TestMapUtils extends BulkTest {
}
}
// Since a typed map is a predicated map, I copied the tests for predicated map
public void testTypedMapIllegalPut() {
final Map map = MapUtils.typedMap(new HashMap(), String.class, String.class);
try {
map.put("Hi", new Integer(3));
fail("Illegal value should raise IllegalArgument");
} catch (IllegalArgumentException e) {
// expected
}
try {
map.put(new Integer(3), "Hi");
fail("Illegal key should raise IllegalArgument");
} catch (IllegalArgumentException e) {
// expected
}
assertTrue(!map.containsKey(new Integer(3)));
assertTrue(!map.containsValue(new Integer(3)));
Map map2 = new HashMap();
map2.put("A", "a");
map2.put("B", "b");
map2.put("C", "c");
map2.put("c", new Integer(3));
try {
map.putAll(map2);
fail("Illegal value should raise IllegalArgument");
} catch (IllegalArgumentException e) {
// expected
}
map.put("E", "e");
Iterator iterator = map.entrySet().iterator();
try {
Map.Entry entry = (Map.Entry)iterator.next();
entry.setValue(new Integer(3));
fail("Illegal value should raise IllegalArgument");
} catch (IllegalArgumentException e) {
// expected
}
}
public BulkTest bulkTestTypedMap() {
return new TestPredicatedMap("") {
public boolean isAllowNullKey() {
return false;
}
public boolean isAllowNullValue() {
return false;
}
public Map makeEmptyMap() {
return MapUtils.typedMap(new HashMap(), String.class, String.class);
}
};
}
public void testLazyMapFactory() {
Factory factory = FactoryUtils.constantFactory(new Integer(5));
Map map = MapUtils.lazyMap(new HashMap(), factory);

View File

@ -48,8 +48,6 @@ public class TestAll extends TestCase {
suite.addTest(TestTransformedBag.suite());
suite.addTest(TestTransformedSortedBag.suite());
suite.addTest(TestTreeBag.suite());
suite.addTest(TestTypedBag.suite());
suite.addTest(TestTypedSortedBag.suite());
return suite;
}

View File

@ -1,124 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.bag;
import java.util.Set;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.Bag;
/**
* Extension of {@link TestBag} for exercising the {@link TypedBag}
* implementation.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Phil Steitz
*/
public class TestTypedBag extends AbstractTestBag {
public TestTypedBag(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestTypedBag.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestTypedBag.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//--------------------------------------------------------------------------
protected Class stringClass = this.getName().getClass();
private Object obj = new Object();
protected Class objectClass = obj.getClass();
protected Bag decorateBag(HashBag bag, Class claz) {
return TypedBag.decorate(bag, claz);
}
public Bag makeBag() {
return decorateBag(new HashBag(), objectClass);
}
protected Bag makeTestBag() {
return decorateBag(new HashBag(), stringClass);
}
//--------------------------------------------------------------------------
public void testlegalAddRemove() {
Bag bag = makeTestBag();
assertEquals(0, bag.size());
Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "1"};
for (int i = 0; i < els.length; i++) {
bag.add(els[i]);
assertEquals(i + 1, bag.size());
assertEquals(true, bag.contains(els[i]));
}
Set set = ((PredicatedBag) bag).uniqueSet();
assertTrue("Unique set contains the first element",set.contains(els[0]));
assertEquals(true, bag.remove(els[0]));
set = ((PredicatedBag) bag).uniqueSet();
assertTrue("Unique set now does not contain the first element",
!set.contains(els[0]));
}
public void testIllegalAdd() {
Bag bag = makeTestBag();
Integer i = new Integer(3);
try {
bag.add(i);
fail("Integer should fail type check.");
} catch (IllegalArgumentException e) {
// expected
}
assertTrue("Collection shouldn't contain illegal element",
!bag.contains(i));
}
public void testIllegalDecorate() {
HashBag elements = new HashBag();
elements.add("one");
elements.add("two");
elements.add(new Integer(3));
elements.add("four");
try {
Bag bag = decorateBag(elements, stringClass);
fail("Bag contains an element that should fail the type test.");
} catch (IllegalArgumentException e) {
// expected
}
try {
Bag bag = decorateBag(new HashBag(), null);
fail("Expectiing IllegalArgumentException for null predicate.");
} catch (IllegalArgumentException e) {
// expected
}
}
protected boolean skipSerializedCanonicalTests() {
return true;
}
}

View File

@ -1,102 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.bag;
import java.util.Comparator;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.SortedBag;
/**
* Extension of {@link TestBag} for exercising the {@link TypedSortedBag}
* implementation.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Phil Steitz
*/
public class TestTypedSortedBag extends AbstractTestSortedBag {
public TestTypedSortedBag(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestTypedSortedBag.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestTypedSortedBag.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//--------------------------------------------------------------------------
protected Class stringClass = this.getName().getClass();
private Object obj = new Object();
protected Class objectClass = obj.getClass();
protected SortedBag nullBag = null;
protected SortedBag decorateBag(SortedBag bag, Class claz) {
return TypedSortedBag.decorate(bag, claz);
}
public Bag makeBag() {
return decorateBag(new TreeBag(), objectClass);
}
protected Bag makeTestBag() {
return decorateBag(new TreeBag(), stringClass);
}
//--------------------------------------------------------------------------
public void testDecorate() {
SortedBag bag = decorateBag(new TreeBag(), stringClass);
try {
SortedBag bag3 = decorateBag(new TreeBag(), null);
fail("Expecting IllegalArgumentException for null predicate");
} catch (IllegalArgumentException e) {}
try {
SortedBag bag4 = decorateBag(nullBag, stringClass);
fail("Expecting IllegalArgumentException for null bag");
} catch (IllegalArgumentException e) {}
}
public void testSortOrder() {
SortedBag bag = decorateBag(new TreeBag(), stringClass);
String one = "one";
String two = "two";
String three = "three";
bag.add(one);
bag.add(two);
bag.add(three);
assertEquals("first element", bag.first(), one);
assertEquals("last element", bag.last(), two);
Comparator c = bag.comparator();
assertTrue("natural order, so comparator should be null", c == null);
}
protected boolean skipSerializedCanonicalTests() {
return true;
}
}

View File

@ -52,7 +52,6 @@ public class TestAll extends TestCase {
suite.addTest(TestSetUniqueList.suite());
suite.addTest(TestSynchronizedList.suite());
suite.addTest(TestTransformedList.suite());
suite.addTest(TestTypedList.suite());
suite.addTest(TestUnmodifiableList.suite());
return suite;

View File

@ -1,66 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.list;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Extension of {@link TestList} for exercising the {@link TypedList}
* implementation.
*
* @since Commons Collections 3.1
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
*/
public class TestTypedList extends AbstractTestList {
public TestTypedList(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestTypedList.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestTypedList.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
public Collection makeConfirmedCollection() {
return new ArrayList();
}
public List makeEmptyList() {
return TypedList.decorate(new ArrayList(), Object.class);
}
public boolean isNullSupported() {
return false;
}
public boolean skipSerializedCanonicalTests() {
return true; // TypedList and PredicatedList get confused
}
}

View File

@ -53,8 +53,6 @@ public class TestAll extends TestCase {
suite.addTest(TestSynchronizedSortedSet.suite());
suite.addTest(TestTransformedSet.suite());
suite.addTest(TestTransformedSortedSet.suite());
suite.addTest(TestTypedSet.suite());
suite.addTest(TestTypedSortedSet.suite());
suite.addTest(TestUnmodifiableSet.suite());
suite.addTest(TestUnmodifiableSortedSet.suite());

View File

@ -1,63 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.set;
import java.util.HashSet;
import java.util.Set;
import junit.framework.Test;
import org.apache.commons.collections.BulkTest;
/**
* Extension of {@link AbstractTestSet} for exercising the
* {@link TypedSet} implementation.
*
* @since Commons Collections 3.1
* @version $Revision$ $Date$
*
* @author Phil Steitz
*/
public class TestTypedSet extends AbstractTestSet{
public TestTypedSet(String testName) {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(TestTypedSet.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestTypedSet.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//-------------------------------------------------------------------
public Set makeEmptySet() {
return TypedSet.decorate(new HashSet(), Object.class);
}
public boolean isNullSupported() {
return false;
}
public boolean skipSerializedCanonicalTests() {
return true; // Typed and Predicated get confused
}
}

View File

@ -1,110 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.set;
import java.util.Arrays;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import junit.framework.Test;
import org.apache.commons.collections.BulkTest;
/**
* Extension of {@link AbstractTestSortedSet} for exercising the
* {@link TypedSortedSet} implementation.
*
* @since Commons Collections 3.0
* @version $Revision$ $Date$
*
* @author Phil Steitz
*/
public class TestTypedSortedSet extends AbstractTestSortedSet{
public TestTypedSortedSet(String testName) {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(TestTypedSortedSet.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestTypedSortedSet.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//-------------------------------------------------------------------
protected Class integerType = new Integer(0).getClass();
public Set makeEmptySet() {
return TypedSortedSet.decorate(new TreeSet(), integerType);
}
public Set makeFullSet() {
TreeSet set = new TreeSet();
set.addAll(Arrays.asList(getFullElements()));
return TypedSortedSet.decorate(set, integerType);
}
//--------------------------------------------------------------------
protected Long getNextAsLong() {
SortedSet set = (SortedSet) makeFullSet();
int nextValue = ((Integer)set.last()).intValue() + 1;
return new Long(nextValue);
}
protected Integer getNextAsInt() {
SortedSet set = (SortedSet) makeFullSet();
int nextValue = ((Integer)set.last()).intValue() + 1;
return new Integer(nextValue);
}
public void testIllegalAdd() {
Set set = makeFullSet();
try {
set.add(getNextAsLong());
fail("Should fail type test.");
} catch (IllegalArgumentException e) {
// expected
}
assertTrue("Collection shouldn't convert long to int",
!set.contains(getNextAsInt()));
}
public void testIllegalAddAll() {
Set set = makeFullSet();
Set elements = new TreeSet();
elements.add(getNextAsLong());
try {
set.addAll(elements);
fail("Should fail type test.");
} catch (IllegalArgumentException e) {
// expected
}
assertTrue("Collection shouldn't convert long to int",
!set.contains(getNextAsInt()));
}
public boolean skipSerializedCanonicalTests() {
return true; // Typed and Predicated get confused
}
}