Improved IndexedCollection, refactored test to comply with standard test framework.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1377059 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e1f8cea8a6
commit
6cbcb9ebc6
|
@ -18,6 +18,7 @@ package org.apache.commons.collections.collection;
|
|||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.collections.Transformer;
|
||||
|
||||
|
@ -40,12 +41,17 @@ import org.apache.commons.collections.Transformer;
|
|||
* @version $Id$
|
||||
*/
|
||||
// TODO support MultiMap/non-unique index behavior
|
||||
// TODO add support for remove and clear
|
||||
public class IndexedCollection<K, C> extends AbstractCollectionDecorator<C> {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = -5512610452568370038L;
|
||||
|
||||
/** The {@link Transformer} for generating index keys. */
|
||||
private final Transformer<C, K> keyTransformer;
|
||||
|
||||
/** The map of indexes to collected objects. */
|
||||
private final Map<K, C> index;
|
||||
|
||||
/**
|
||||
* Create an {@link IndexedCollection} for a unique index.
|
||||
*
|
||||
|
@ -60,67 +66,126 @@ public class IndexedCollection<K, C> extends AbstractCollectionDecorator<C> {
|
|||
return new IndexedCollection<K, C>(coll, keyTransformer, new HashMap<K, C>());
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@link Transformer} for generating index keys.
|
||||
*/
|
||||
private final Transformer<C, K> keyTransformer;
|
||||
|
||||
/**
|
||||
* The map of indexes to collected objects.
|
||||
*/
|
||||
private final HashMap<K, C> index;
|
||||
|
||||
/**
|
||||
* Create a {@link IndexedCollection} for a unique index.
|
||||
*
|
||||
* @param coll the decorated {@link Collection}.
|
||||
* @param keyTransformer the {@link Transformer} for generating index keys.
|
||||
* @param coll decorated {@link Collection}
|
||||
* @param keyTransformer {@link Transformer} for generating index keys
|
||||
* @param map map to use as index
|
||||
*/
|
||||
public IndexedCollection(Collection<C> coll, Transformer<C, K> keyTransformer, HashMap<K, C> map) {
|
||||
super(coll);
|
||||
this.keyTransformer = keyTransformer;
|
||||
this.index = map;
|
||||
this.index = new HashMap<K, C>();
|
||||
reindex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(C object) {
|
||||
final boolean added = super.add(object);
|
||||
if (added) {
|
||||
addToIndex(object);
|
||||
}
|
||||
return added;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends C> coll) {
|
||||
boolean changed = false;
|
||||
for (C c: coll) {
|
||||
changed |= add(c);
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
super.clear();
|
||||
index.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>
|
||||
* Note: uses the index for fast lookup
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean contains(Object object) {
|
||||
return index.containsKey(keyTransformer.transform((C) object));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>
|
||||
* Note: uses the index for fast lookup
|
||||
*/
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> coll) {
|
||||
for (Object o : coll) {
|
||||
if (!contains(o)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the element associated with the given key.
|
||||
*
|
||||
* @param key key to look up
|
||||
* @return element found
|
||||
*/
|
||||
public C get(K key) {
|
||||
return index.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the index and re-indexes the entire decorated {@link Collection}.
|
||||
*/
|
||||
public void reindex() {
|
||||
index.clear();
|
||||
for (C c : decorated()) {
|
||||
addIndex(c);
|
||||
addToIndex(c);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an object to the collection and index.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
// TODO: Add error handling for when super.add() fails
|
||||
public boolean add(C object) {
|
||||
addIndex(object);
|
||||
return super.add(object);
|
||||
public boolean remove(Object object) {
|
||||
final boolean removed = super.remove(object);
|
||||
if (removed) {
|
||||
removeFromIndex((C) object);
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an entire collection to the collection and index.
|
||||
*/
|
||||
@Override
|
||||
// TODO: Add error handling for when super.addAll() fails
|
||||
public boolean addAll(Collection<? extends C> coll) {
|
||||
for (C c : coll) {
|
||||
addIndex(c);
|
||||
public boolean removeAll(Collection<?> coll) {
|
||||
boolean changed = false;
|
||||
for (Object o : coll) {
|
||||
changed |= remove(o);
|
||||
}
|
||||
return super.addAll(coll);
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> coll) {
|
||||
final boolean changed = super.retainAll(coll);
|
||||
if (changed) {
|
||||
reindex();
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Provides checking for adding the index.
|
||||
*
|
||||
* @param object the object to index.
|
||||
* @param object the object to index
|
||||
*/
|
||||
private void addIndex(C object) {
|
||||
private void addToIndex(C object) {
|
||||
final C existingObject = index.put(keyTransformer.transform(object), object);
|
||||
if (existingObject != null) {
|
||||
throw new IllegalArgumentException("Duplicate key in uniquely indexed collection.");
|
||||
|
@ -128,11 +193,12 @@ public class IndexedCollection<K, C> extends AbstractCollectionDecorator<C> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the element associated with the given key.
|
||||
* @param key to look up
|
||||
* @return element found
|
||||
* Removes an object from the index.
|
||||
*
|
||||
* @param object the object to remove
|
||||
*/
|
||||
public C get(K key) {
|
||||
return index.get(key);
|
||||
private void removeFromIndex(C object) {
|
||||
index.remove(keyTransformer.transform(object));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,38 +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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Before;
|
||||
|
||||
public abstract class AbstractDecoratedCollectionTest<C> {
|
||||
/**
|
||||
* The {@link Collection} being decorated.
|
||||
*/
|
||||
protected Collection<C> original;
|
||||
/**
|
||||
* The Collection under test that decorates {@link #original}.
|
||||
*/
|
||||
protected Collection<C> decorated;
|
||||
|
||||
@Before
|
||||
public void setUpDecoratedCollection() throws Exception {
|
||||
original = new ArrayList<C>();
|
||||
}
|
||||
}
|
|
@ -17,35 +17,94 @@
|
|||
package org.apache.commons.collections.collection;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
|
||||
import org.apache.commons.collections.AbstractDecoratedCollectionTest;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections.Transformer;
|
||||
import org.apache.commons.collections.collection.IndexedCollection;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@SuppressWarnings("boxing")
|
||||
public class IndexedCollectionTest extends AbstractDecoratedCollectionTest<String> {
|
||||
private IndexedCollection<Integer, String> indexed;
|
||||
public class IndexedCollectionTest extends AbstractCollectionTest<String> {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
indexed = IndexedCollection.uniqueIndexedCollection(original, new Transformer<String, Integer>() {
|
||||
public Integer transform(String input) {
|
||||
return Integer.parseInt(input);
|
||||
}
|
||||
});
|
||||
decorated = indexed;
|
||||
public IndexedCollectionTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
|
||||
protected Collection<String> decorateCollection(Collection<String> collection) {
|
||||
return IndexedCollection.uniqueIndexedCollection(collection, new IntegerTransformer());
|
||||
}
|
||||
|
||||
private static final class IntegerTransformer implements Transformer<String, Integer>, Serializable {
|
||||
private static final long serialVersionUID = 809439581555072949L;
|
||||
|
||||
public Integer transform(String input) {
|
||||
return Integer.valueOf(input);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> makeObject() {
|
||||
return decorateCollection(new ArrayList<String>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> makeConfirmedCollection() {
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getFullElements() {
|
||||
return (String[]) new String[] { "1", "3", "5", "7", "2", "4", "6" };
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getOtherElements() {
|
||||
return new String[] {"9", "88", "678", "87", "98", "78", "99"};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> makeFullCollection() {
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.addAll(Arrays.asList(getFullElements()));
|
||||
return decorateCollection(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> makeConfirmedFullCollection() {
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.addAll(Arrays.asList(getFullElements()));
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean skipSerializedCanonicalTests() {
|
||||
// FIXME: support canonical tests
|
||||
return true;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
|
||||
public void testCollectionAddAll() {
|
||||
// FIXME: does not work as we do not support multi-keys yet
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addedObjectsCanBeRetrievedByKey() throws Exception {
|
||||
decorated.add("12");
|
||||
decorated.add("16");
|
||||
decorated.add("1");
|
||||
decorated.addAll(asList("2","3","4"));
|
||||
Collection<String> coll = getCollection();
|
||||
coll.add("12");
|
||||
coll.add("16");
|
||||
coll.add("1");
|
||||
coll.addAll(asList("2","3","4"));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
IndexedCollection<Integer, String> indexed = (IndexedCollection<Integer, String>) coll;
|
||||
assertEquals("12", indexed.get(12));
|
||||
assertEquals("16", indexed.get(16));
|
||||
assertEquals("1", indexed.get(1));
|
||||
|
@ -56,38 +115,38 @@ public class IndexedCollectionTest extends AbstractDecoratedCollectionTest<Strin
|
|||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void ensureDuplicateObjectsCauseException() throws Exception {
|
||||
decorated.add("1");
|
||||
decorated.add("1");
|
||||
getCollection().add("1");
|
||||
getCollection().add("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decoratedCollectionIsIndexedOnCreation() throws Exception {
|
||||
original.add("1");
|
||||
original.add("2");
|
||||
original.add("3");
|
||||
|
||||
indexed = IndexedCollection.uniqueIndexedCollection(original, new Transformer<String, Integer>() {
|
||||
public Integer transform(String input) {
|
||||
return Integer.parseInt(input);
|
||||
}
|
||||
});
|
||||
assertEquals("1", indexed.get(1));
|
||||
assertEquals("2", indexed.get(2));
|
||||
assertEquals("3", indexed.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reindexUpdatesIndexWhenTheDecoratedCollectionIsModifiedSeparately() throws Exception {
|
||||
original.add("1");
|
||||
original.add("2");
|
||||
original.add("3");
|
||||
|
||||
assertNull(indexed.get(1));
|
||||
assertNull(indexed.get(2));
|
||||
assertNull(indexed.get(3));
|
||||
indexed.reindex();
|
||||
assertEquals("1", indexed.get(1));
|
||||
assertEquals("2", indexed.get(2));
|
||||
assertEquals("3", indexed.get(3));
|
||||
}
|
||||
// @Test
|
||||
// public void decoratedCollectionIsIndexedOnCreation() throws Exception {
|
||||
// original.add("1");
|
||||
// original.add("2");
|
||||
// original.add("3");
|
||||
//
|
||||
// indexed = IndexedCollection.uniqueIndexedCollection(original, new Transformer<String, Integer>() {
|
||||
// public Integer transform(String input) {
|
||||
// return Integer.parseInt(input);
|
||||
// }
|
||||
// });
|
||||
// assertEquals("1", indexed.get(1));
|
||||
// assertEquals("2", indexed.get(2));
|
||||
// assertEquals("3", indexed.get(3));
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void reindexUpdatesIndexWhenTheDecoratedCollectionIsModifiedSeparately() throws Exception {
|
||||
// original.add("1");
|
||||
// original.add("2");
|
||||
// original.add("3");
|
||||
//
|
||||
// assertNull(indexed.get(1));
|
||||
// assertNull(indexed.get(2));
|
||||
// assertNull(indexed.get(3));
|
||||
// indexed.reindex();
|
||||
// assertEquals("1", indexed.get(1));
|
||||
// assertEquals("2", indexed.get(2));
|
||||
// assertEquals("3", indexed.get(3));
|
||||
// }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue