fixed line-endings property svn:eol-style for a few existing files

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1311961 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2012-04-10 19:54:57 +00:00
parent 6d8659cad5
commit 2fa3c8c5f0
9 changed files with 805 additions and 805 deletions

View File

@ -1,136 +1,136 @@
/*
* 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.Collection;
import java.util.HashMap;
import org.apache.commons.collections.collection.AbstractCollectionDecorator;
/**
* An IndexedCollection is a Map-like view onto a Collection. It accepts a
* keyTransformer to define how the keys are converted from the values.
* <p>
* Modifications made to this decorator modify the index as well as the
* decorated {@link Collection}. However, modifications to the underlying
* {@link Collection} will not updated the index and it will get out of sync.
* <p>
* If modification to the decorated {@link Collection} is unavoidable, then a
* call to {@link #reindex()} will update the index to the current contents of
* the {@link Collection}.
*
* @param <K> the type of object in the index.
* @param <C> the type of object in the collection.
* @author Stephen Kestle
*/
// TODO support MultiMap/non-unique index behavior
// TODO add support for remove and clear
public class IndexedCollection<K, C> extends AbstractCollectionDecorator<C> {
/**
* .
*/
private static final long serialVersionUID = -5512610452568370038L;
/**
* Create an {@link IndexedCollection} for a unique index.
*
* @param <K> the index object type.
* @param <C> the collection type.
* @param coll the decorated {@link Collection}.
* @param keyTransformer the {@link Transformer} for generating index keys.
* @return the created {@link IndexedCollection}.
*/
public static <K, C> IndexedCollection<K, C> uniqueIndexedCollection(final Collection<C> coll, final Transformer<C, K> keyTransformer) {
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.
*/
public IndexedCollection(Collection<C> coll, Transformer<C, K> keyTransformer, HashMap<K, C> map) {
super(coll);
this.keyTransformer = keyTransformer;
this.index = map;
reindex();
}
/**
* Clears the index and re-indexes the entire decorated {@link Collection}.
*/
public void reindex() {
index.clear();
for (C c : decorated()) {
addIndex(c);
}
}
/**
* Adds an object to the collection and index.
*/
@Override
// TODO: Add error handling for when super.add() fails
public boolean add(C object) {
addIndex(object);
return super.add(object);
}
/**
* 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);
}
return super.addAll(coll);
}
/**
* Provides checking for adding the index.
*
* @param object the object to index.
*/
private void addIndex(C object) {
final C existingObject = index.put(keyTransformer.transform(object), object);
if (existingObject != null) {
throw new IllegalArgumentException("Duplicate key in uniquely indexed collection.");
}
}
/**
* Get the element associated with the given key.
* @param key to look up
* @return element found
*/
public C get(K key) {
return index.get(key);
}
}
/*
* 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.Collection;
import java.util.HashMap;
import org.apache.commons.collections.collection.AbstractCollectionDecorator;
/**
* An IndexedCollection is a Map-like view onto a Collection. It accepts a
* keyTransformer to define how the keys are converted from the values.
* <p>
* Modifications made to this decorator modify the index as well as the
* decorated {@link Collection}. However, modifications to the underlying
* {@link Collection} will not updated the index and it will get out of sync.
* <p>
* If modification to the decorated {@link Collection} is unavoidable, then a
* call to {@link #reindex()} will update the index to the current contents of
* the {@link Collection}.
*
* @param <K> the type of object in the index.
* @param <C> the type of object in the collection.
* @author Stephen Kestle
*/
// TODO support MultiMap/non-unique index behavior
// TODO add support for remove and clear
public class IndexedCollection<K, C> extends AbstractCollectionDecorator<C> {
/**
* .
*/
private static final long serialVersionUID = -5512610452568370038L;
/**
* Create an {@link IndexedCollection} for a unique index.
*
* @param <K> the index object type.
* @param <C> the collection type.
* @param coll the decorated {@link Collection}.
* @param keyTransformer the {@link Transformer} for generating index keys.
* @return the created {@link IndexedCollection}.
*/
public static <K, C> IndexedCollection<K, C> uniqueIndexedCollection(final Collection<C> coll, final Transformer<C, K> keyTransformer) {
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.
*/
public IndexedCollection(Collection<C> coll, Transformer<C, K> keyTransformer, HashMap<K, C> map) {
super(coll);
this.keyTransformer = keyTransformer;
this.index = map;
reindex();
}
/**
* Clears the index and re-indexes the entire decorated {@link Collection}.
*/
public void reindex() {
index.clear();
for (C c : decorated()) {
addIndex(c);
}
}
/**
* Adds an object to the collection and index.
*/
@Override
// TODO: Add error handling for when super.add() fails
public boolean add(C object) {
addIndex(object);
return super.add(object);
}
/**
* 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);
}
return super.addAll(coll);
}
/**
* Provides checking for adding the index.
*
* @param object the object to index.
*/
private void addIndex(C object) {
final C existingObject = index.put(keyTransformer.transform(object), object);
if (existingObject != null) {
throw new IllegalArgumentException("Duplicate key in uniquely indexed collection.");
}
}
/**
* Get the element associated with the given key.
* @param key to look up
* @return element found
*/
public C get(K key) {
return index.get(key);
}
}

View File

@ -1,76 +1,76 @@
/*
* 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.functors;
import org.apache.commons.collections.Closure;
import org.apache.commons.collections.FunctorException;
/**
* {@link Closure} that catches any checked exception and re-throws it as a
* {@link FunctorException} runtime exception. Example usage:
*
* <pre>
* // Create a catch and re-throw closure via anonymous subclass
* CatchAndRethrowClosure&lt;String&gt; writer = new ThrowingClosure() {
* private java.io.Writer out = // some writer
*
* protected void executeAndThrow(String input) throws IOException {
* out.write(input); // throwing of IOException allowed
* }
* };
*
* // use catch and re-throw closure
* java.util.List<String> strList = // some list
* try {
* CollctionUtils.forAllDo(strList, writer);
* } catch (FunctorException ex) {
* Throwable originalError = ex.getCause();
* // handle error
* }
* </pre>
*
* @since Commons Collections 4.0
* @version $Revision$
*/
public abstract class CatchAndRethrowClosure<E> implements Closure<E> {
/**
* Execute this closure on the specified input object.
*
* @param input the input to execute on
* @throws FunctorException (runtime) if the closure execution resulted in a
* checked exception.
*/
public void execute(E input) {
try {
executeAndThrow(input);
} catch (RuntimeException ex) {
throw ex;
} catch (Throwable t) {
throw new FunctorException(t);
}
}
/**
* Execute this closure on the specified input object.
*
* @param input the input to execute on
* @throws Throwable if the closure execution resulted in a checked
* exception.
*/
protected abstract void executeAndThrow(E input) throws Throwable;
}
/*
* 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.functors;
import org.apache.commons.collections.Closure;
import org.apache.commons.collections.FunctorException;
/**
* {@link Closure} that catches any checked exception and re-throws it as a
* {@link FunctorException} runtime exception. Example usage:
*
* <pre>
* // Create a catch and re-throw closure via anonymous subclass
* CatchAndRethrowClosure&lt;String&gt; writer = new ThrowingClosure() {
* private java.io.Writer out = // some writer
*
* protected void executeAndThrow(String input) throws IOException {
* out.write(input); // throwing of IOException allowed
* }
* };
*
* // use catch and re-throw closure
* java.util.List<String> strList = // some list
* try {
* CollctionUtils.forAllDo(strList, writer);
* } catch (FunctorException ex) {
* Throwable originalError = ex.getCause();
* // handle error
* }
* </pre>
*
* @since Commons Collections 4.0
* @version $Revision$
*/
public abstract class CatchAndRethrowClosure<E> implements Closure<E> {
/**
* Execute this closure on the specified input object.
*
* @param input the input to execute on
* @throws FunctorException (runtime) if the closure execution resulted in a
* checked exception.
*/
public void execute(E input) {
try {
executeAndThrow(input);
} catch (RuntimeException ex) {
throw ex;
} catch (Throwable t) {
throw new FunctorException(t);
}
}
/**
* Execute this closure on the specified input object.
*
* @param input the input to execute on
* @throws Throwable if the closure execution resulted in a checked
* exception.
*/
protected abstract void executeAndThrow(E input) throws Throwable;
}

View File

@ -1,188 +1,188 @@
/*
* 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.functors;
import java.io.Serializable;
import java.util.Comparator;
import org.apache.commons.collections.Predicate;
/**
* Predicate that compares the input object with the one stored in the predicate using a comparator.
* In addition, the comparator result can be evaluated in accordance to a supplied criterion value.
*
* In order to demonstrate the use of the predicate, the following variables are declared:
*
* <pre>
* Integer ONE = new Integer(1);
* Integer TWO = new Integer(2);
*
* Comparator comparator = new Comparator() {
*
* public int compare(Object first, Object second) {
* return ((Integer) second) - ((Integer) first);
* }
*
* };
* </pre>
*
* Using the declared variables, the <code>ComparatorPredicate</code> can be used used in the
* following way:
*
* <pre>
* ComparatorPredicate.getInstance(ONE, comparator).evaluate(TWO);
* </pre>
*
* The input variable <code>TWO</code> in compared to the stored variable <code>ONE</code> using
* the supplied <code>comparator</code>. This is the default usage of the predicate and will return
* <code>true</code> if the underlying comparator returns <code>0</code>. In addition to the default
* usage of the predicate, it is possible to evaluate the comparator's result in several ways. The
* following {@link Criterion} enumeration values are provided by the predicate:
* </p>
*
* <ul>
* <li>EQUAL</li>
* <li>GREATER</li>
* <li>GREATER_OR_EQUAL</li>
* <li>LESS</li>
* <li>LESS_OR_EQUAL</li>
* </ul>
*
* The following examples demonstrates how these constants can be used in order to manipulate the
* evaluation of a comparator result.
*
* <pre>
* ComparatorPredicate.getInstance(ONE, comparator, <b>ComparatorPredicate.Criterion.GREATER</b>).evaluate(TWO);
* </pre>
*
* The input variable TWO is compared to the stored variable ONE using the supplied <code>comparator</code>
* using the <code>GREATER</code> evaluation criterion constant. This instructs the predicate to
* return <code>true</code> if the comparator returns a value greater than <code>0</code>.
*
* @since Commons Collections 4.0
* @version $Revision$
*
* @author Rune Peter Bjørnstad.
*/
public class ComparatorPredicate<T> implements Predicate<T>, Serializable {
private static final long serialVersionUID = -1863209236504077399L;
public enum Criterion {
EQUAL, GREATER, LESS, GREATER_OR_EQUAL, LESS_OR_EQUAL,
}
// Instance variables:
/** The internal object to compare with */
private final T object;
/** The comparator to use for comparison */
private final Comparator<T> comparator;
/** The comparison evaluation criterion to use */
private final Criterion criterion;
/**
* Factory to create the comparator predicate
*
* @param object the object to compare to
* @param comparator the comparator to use for comparison
* @return the predicate
* @throws IllegalArgumentException if comparator is null
*/
public static <T> Predicate<T> comparatorPredicate(T object, Comparator<T> comparator) {
return comparatorPredicate(object, comparator, Criterion.EQUAL);
}
/**
* Factory to create the comparator predicate
*
* @param object the object to compare to
* @param comparator the comparator to use for comparison
* @param criterion the criterion to use to evaluate comparison
* @return the predicate
* @throws IllegalArgumentException if comparator is null of criterion is invalid
*/
public static <T> Predicate<T> comparatorPredicate(T object, Comparator<T> comparator, Criterion criterion) {
if (comparator == null) {
throw new IllegalArgumentException("Comparator must not be null.");
}
if (criterion == null) {
throw new IllegalArgumentException("Criterion must not be null.");
}
return new ComparatorPredicate<T>(object, comparator, criterion);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want.
*
* @param object the object to compare to
* @param comparator the comparator to use for comparison
* @param criterion the criterion to use to evaluate comparison
*/
public ComparatorPredicate(T object, Comparator<T> comparator, Criterion criterion) {
super();
this.object = object;
this.comparator = comparator;
this.criterion = criterion;
}
/**
* Evaluates the predicate. The predicate evaluates to <code>true</code> in the following cases:
*
* <ul>
* <li><code>comparator.compare(object, input) == 0 && criterion == EQUAL</code></li>
* <li><code>comparator.compare(object, input) < 0 && criterion == LESS</code></li>
* <li><code>comparator.compare(object, input) > 0 && criterion == GREATER</code></li>
* <li><code>comparator.compare(object, input) >= 0 && criterion == GREATER_OR_EQUAL</code></li>
* <li><code>comparator.compare(object, input) <= 0 && criterion == LESS_OR_EQUAL</code></li>
* </ul>
*
* @see org.apache.commons.collections.Predicate#evaluate(java.lang.Object)
* @see java.util.Comparator#compare(java.lang.Object first, java.lang.Object second)
*
* @throws IllegalStateException if the criterion is invalid (really not possible)
*/
public boolean evaluate(T target) {
boolean result = false;
int comparison = comparator.compare(object, target);
switch (criterion) {
case EQUAL:
result = (comparison == 0);
break;
case GREATER:
result = (comparison > 0);
break;
case LESS:
result = (comparison < 0);
break;
case GREATER_OR_EQUAL:
result = (comparison >= 0);
break;
case LESS_OR_EQUAL:
result = (comparison <= 0);
break;
default:
throw new IllegalStateException("The current criterion '" + criterion + "' is invalid.");
}
return result;
}
}
/*
* 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.functors;
import java.io.Serializable;
import java.util.Comparator;
import org.apache.commons.collections.Predicate;
/**
* Predicate that compares the input object with the one stored in the predicate using a comparator.
* In addition, the comparator result can be evaluated in accordance to a supplied criterion value.
*
* In order to demonstrate the use of the predicate, the following variables are declared:
*
* <pre>
* Integer ONE = new Integer(1);
* Integer TWO = new Integer(2);
*
* Comparator comparator = new Comparator() {
*
* public int compare(Object first, Object second) {
* return ((Integer) second) - ((Integer) first);
* }
*
* };
* </pre>
*
* Using the declared variables, the <code>ComparatorPredicate</code> can be used used in the
* following way:
*
* <pre>
* ComparatorPredicate.getInstance(ONE, comparator).evaluate(TWO);
* </pre>
*
* The input variable <code>TWO</code> in compared to the stored variable <code>ONE</code> using
* the supplied <code>comparator</code>. This is the default usage of the predicate and will return
* <code>true</code> if the underlying comparator returns <code>0</code>. In addition to the default
* usage of the predicate, it is possible to evaluate the comparator's result in several ways. The
* following {@link Criterion} enumeration values are provided by the predicate:
* </p>
*
* <ul>
* <li>EQUAL</li>
* <li>GREATER</li>
* <li>GREATER_OR_EQUAL</li>
* <li>LESS</li>
* <li>LESS_OR_EQUAL</li>
* </ul>
*
* The following examples demonstrates how these constants can be used in order to manipulate the
* evaluation of a comparator result.
*
* <pre>
* ComparatorPredicate.getInstance(ONE, comparator, <b>ComparatorPredicate.Criterion.GREATER</b>).evaluate(TWO);
* </pre>
*
* The input variable TWO is compared to the stored variable ONE using the supplied <code>comparator</code>
* using the <code>GREATER</code> evaluation criterion constant. This instructs the predicate to
* return <code>true</code> if the comparator returns a value greater than <code>0</code>.
*
* @since Commons Collections 4.0
* @version $Revision$
*
* @author Rune Peter Bjørnstad.
*/
public class ComparatorPredicate<T> implements Predicate<T>, Serializable {
private static final long serialVersionUID = -1863209236504077399L;
public enum Criterion {
EQUAL, GREATER, LESS, GREATER_OR_EQUAL, LESS_OR_EQUAL,
}
// Instance variables:
/** The internal object to compare with */
private final T object;
/** The comparator to use for comparison */
private final Comparator<T> comparator;
/** The comparison evaluation criterion to use */
private final Criterion criterion;
/**
* Factory to create the comparator predicate
*
* @param object the object to compare to
* @param comparator the comparator to use for comparison
* @return the predicate
* @throws IllegalArgumentException if comparator is null
*/
public static <T> Predicate<T> comparatorPredicate(T object, Comparator<T> comparator) {
return comparatorPredicate(object, comparator, Criterion.EQUAL);
}
/**
* Factory to create the comparator predicate
*
* @param object the object to compare to
* @param comparator the comparator to use for comparison
* @param criterion the criterion to use to evaluate comparison
* @return the predicate
* @throws IllegalArgumentException if comparator is null of criterion is invalid
*/
public static <T> Predicate<T> comparatorPredicate(T object, Comparator<T> comparator, Criterion criterion) {
if (comparator == null) {
throw new IllegalArgumentException("Comparator must not be null.");
}
if (criterion == null) {
throw new IllegalArgumentException("Criterion must not be null.");
}
return new ComparatorPredicate<T>(object, comparator, criterion);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want.
*
* @param object the object to compare to
* @param comparator the comparator to use for comparison
* @param criterion the criterion to use to evaluate comparison
*/
public ComparatorPredicate(T object, Comparator<T> comparator, Criterion criterion) {
super();
this.object = object;
this.comparator = comparator;
this.criterion = criterion;
}
/**
* Evaluates the predicate. The predicate evaluates to <code>true</code> in the following cases:
*
* <ul>
* <li><code>comparator.compare(object, input) == 0 && criterion == EQUAL</code></li>
* <li><code>comparator.compare(object, input) < 0 && criterion == LESS</code></li>
* <li><code>comparator.compare(object, input) > 0 && criterion == GREATER</code></li>
* <li><code>comparator.compare(object, input) >= 0 && criterion == GREATER_OR_EQUAL</code></li>
* <li><code>comparator.compare(object, input) <= 0 && criterion == LESS_OR_EQUAL</code></li>
* </ul>
*
* @see org.apache.commons.collections.Predicate#evaluate(java.lang.Object)
* @see java.util.Comparator#compare(java.lang.Object first, java.lang.Object second)
*
* @throws IllegalStateException if the criterion is invalid (really not possible)
*/
public boolean evaluate(T target) {
boolean result = false;
int comparison = comparator.compare(object, target);
switch (criterion) {
case EQUAL:
result = (comparison == 0);
break;
case GREATER:
result = (comparison > 0);
break;
case LESS:
result = (comparison < 0);
break;
case GREATER_OR_EQUAL:
result = (comparison >= 0);
break;
case LESS_OR_EQUAL:
result = (comparison <= 0);
break;
default:
throw new IllegalStateException("The current criterion '" + criterion + "' is invalid.");
}
return result;
}
}

View File

@ -1,38 +1,38 @@
/*
* 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>();
}
}
/*
* 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>();
}
}

View File

@ -1,67 +1,67 @@
/*
* 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.List;
import java.util.ListIterator;
import org.easymock.EasyMock;
import org.easymock.IExpectationSetters;
/**
* Provides utilities for making mock-based tests. Most notable is the generic "type-safe"
* {@link #createMock(Class)} method, and {@link #replay()} and {@link #verify()} methods
* that call the respective methods on all created mock objects.
*
* @author Stephen Kestle
*/
public abstract class MockTestCase {
private List<Object> mockObjects = new ArrayList<Object>();
@SuppressWarnings("unchecked")
protected <T> T createMock(Class<?> name) {
T mock = (T) EasyMock.createMock(name);
return registerMock(mock);
}
private <T> T registerMock(T mock) {
mockObjects.add(mock);
return mock;
}
protected <T> IExpectationSetters<T> expect(T t) {
return EasyMock.expect(t);
}
protected final void replay() {
for (Object o : mockObjects) {
EasyMock.replay(o);
}
}
protected final void verify() {
for (ListIterator<Object> i = mockObjects.listIterator(); i.hasNext();) {
try {
EasyMock.verify(i.next());
} catch (AssertionError e) {
throw new AssertionError((i.previousIndex() + 1) + ""
+ e.getMessage());
}
}
}
}
/*
* 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.List;
import java.util.ListIterator;
import org.easymock.EasyMock;
import org.easymock.IExpectationSetters;
/**
* Provides utilities for making mock-based tests. Most notable is the generic "type-safe"
* {@link #createMock(Class)} method, and {@link #replay()} and {@link #verify()} methods
* that call the respective methods on all created mock objects.
*
* @author Stephen Kestle
*/
public abstract class MockTestCase {
private List<Object> mockObjects = new ArrayList<Object>();
@SuppressWarnings("unchecked")
protected <T> T createMock(Class<?> name) {
T mock = (T) EasyMock.createMock(name);
return registerMock(mock);
}
private <T> T registerMock(T mock) {
mockObjects.add(mock);
return mock;
}
protected <T> IExpectationSetters<T> expect(T t) {
return EasyMock.expect(t);
}
protected final void replay() {
for (Object o : mockObjects) {
EasyMock.replay(o);
}
}
protected final void verify() {
for (ListIterator<Object> i = mockObjects.listIterator(); i.hasNext();) {
try {
EasyMock.verify(i.next());
} catch (AssertionError e) {
throw new AssertionError((i.previousIndex() + 1) + ""
+ e.getMessage());
}
}
}
}

View File

@ -1,90 +1,90 @@
/*
* 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 static java.util.Arrays.asList;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import org.junit.Before;
import org.junit.Test;
@SuppressWarnings("boxing")
public class TestIndexedCollection extends AbstractDecoratedCollectionTest<String> {
private IndexedCollection<Integer, String> indexed;
@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;
}
@Test
public void addedObjectsCanBeRetrievedByKey() throws Exception {
decorated.add("12");
decorated.add("16");
decorated.add("1");
decorated.addAll(asList("2","3","4"));
assertEquals("12", indexed.get(12));
assertEquals("16", indexed.get(16));
assertEquals("1", indexed.get(1));
assertEquals("2", indexed.get(2));
assertEquals("3", indexed.get(3));
assertEquals("4", indexed.get(4));
}
@Test(expected=IllegalArgumentException.class)
public void ensureDuplicateObjectsCauseException() throws Exception {
decorated.add("1");
decorated.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));
}
}
/*
* 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 static java.util.Arrays.asList;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import org.junit.Before;
import org.junit.Test;
@SuppressWarnings("boxing")
public class TestIndexedCollection extends AbstractDecoratedCollectionTest<String> {
private IndexedCollection<Integer, String> indexed;
@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;
}
@Test
public void addedObjectsCanBeRetrievedByKey() throws Exception {
decorated.add("12");
decorated.add("16");
decorated.add("1");
decorated.addAll(asList("2","3","4"));
assertEquals("12", indexed.get(12));
assertEquals("16", indexed.get(16));
assertEquals("1", indexed.get(1));
assertEquals("2", indexed.get(2));
assertEquals("3", indexed.get(3));
assertEquals("4", indexed.get(4));
}
@Test(expected=IllegalArgumentException.class)
public void ensureDuplicateObjectsCauseException() throws Exception {
decorated.add("1");
decorated.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));
}
}

View File

@ -1,35 +1,35 @@
/*
* 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.functors;
import org.apache.commons.collections.Closure;
import org.junit.Assert;
import org.junit.Test;
public abstract class BasicClosureTestBase {
@Test
public void closureSanityTests() throws Exception {
Closure<?> closure = generateClosure();
Assert.assertNotNull(closure);
}
/**
* @return a closure for general sanity tests.
*/
protected abstract <T> Closure<T> generateClosure();
}
/*
* 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.functors;
import org.apache.commons.collections.Closure;
import org.junit.Assert;
import org.junit.Test;
public abstract class BasicClosureTestBase {
@Test
public void closureSanityTests() throws Exception {
Closure<?> closure = generateClosure();
Assert.assertNotNull(closure);
}
/**
* @return a closure for general sanity tests.
*/
protected abstract <T> Closure<T> generateClosure();
}

View File

@ -1,93 +1,93 @@
/*
* 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.functors;
import java.io.IOException;
import org.apache.commons.collections.Closure;
import org.apache.commons.collections.FunctorException;
import org.junit.Assert;
import org.junit.Test;
public class TestCatchAndRethrowClosure extends BasicClosureTestBase {
private static <T> Closure<T> generateIOExceptionClosure() {
return new CatchAndRethrowClosure<T>() {
@Override
protected void executeAndThrow(T input) throws IOException {
throw new IOException();
}
};
}
private static <T> Closure<T> generateNullPointerExceptionClosure() {
return new CatchAndRethrowClosure<T>() {
@Override
protected void executeAndThrow(T input) {
throw new NullPointerException();
}
};
}
private static <T> Closure<T> generateNoExceptionClosure() {
return new CatchAndRethrowClosure<T>() {
@Override
protected void executeAndThrow(T input) {
}
};
}
@Override
protected <T> Closure<T> generateClosure() {
return generateNoExceptionClosure();
}
@Test
public void testThrowingClosure() {
Closure<Integer> closure = generateNoExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
} catch (FunctorException ex) {
Assert.fail();
} catch (RuntimeException ex) {
Assert.fail();
}
closure = generateIOExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
Assert.fail();
} catch (FunctorException ex) {
Assert.assertTrue(ex.getCause() instanceof IOException);
} catch (RuntimeException ex) {
Assert.fail();
}
closure = generateNullPointerExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
Assert.fail();
} catch (FunctorException ex) {
Assert.fail();
} catch (RuntimeException ex) {
Assert.assertTrue(ex instanceof NullPointerException);
}
}
}
/*
* 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.functors;
import java.io.IOException;
import org.apache.commons.collections.Closure;
import org.apache.commons.collections.FunctorException;
import org.junit.Assert;
import org.junit.Test;
public class TestCatchAndRethrowClosure extends BasicClosureTestBase {
private static <T> Closure<T> generateIOExceptionClosure() {
return new CatchAndRethrowClosure<T>() {
@Override
protected void executeAndThrow(T input) throws IOException {
throw new IOException();
}
};
}
private static <T> Closure<T> generateNullPointerExceptionClosure() {
return new CatchAndRethrowClosure<T>() {
@Override
protected void executeAndThrow(T input) {
throw new NullPointerException();
}
};
}
private static <T> Closure<T> generateNoExceptionClosure() {
return new CatchAndRethrowClosure<T>() {
@Override
protected void executeAndThrow(T input) {
}
};
}
@Override
protected <T> Closure<T> generateClosure() {
return generateNoExceptionClosure();
}
@Test
public void testThrowingClosure() {
Closure<Integer> closure = generateNoExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
} catch (FunctorException ex) {
Assert.fail();
} catch (RuntimeException ex) {
Assert.fail();
}
closure = generateIOExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
Assert.fail();
} catch (FunctorException ex) {
Assert.assertTrue(ex.getCause() instanceof IOException);
} catch (RuntimeException ex) {
Assert.fail();
}
closure = generateNullPointerExceptionClosure();
try {
closure.execute(Integer.valueOf(0));
Assert.fail();
} catch (FunctorException ex) {
Assert.fail();
} catch (RuntimeException ex) {
Assert.assertTrue(ex instanceof NullPointerException);
}
}
}

View File

@ -1,82 +1,82 @@
/*
* 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.functors;
import static org.apache.commons.collections.functors.ComparatorPredicate.*;
import java.util.Comparator;
import org.apache.commons.collections.Predicate;
import org.junit.Test;
public class TestComparatorPredicate extends BasicPredicateTestBase {
private class TestComparator<T extends Comparable<T>> implements Comparator<T> {
public int compare(T first, T second) {
return first.compareTo(second);
}
}
@Test
public void compareEquals() {
Integer value = Integer.valueOf(10);
Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>());
assertFalse(p, Integer.valueOf(value.intValue() - 1));
assertTrue(p, Integer.valueOf(value.intValue()));
assertFalse(p, Integer.valueOf(value.intValue() + 1));
}
@Test
public void compareGreater() {
Integer value = Integer.valueOf(10);
Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.GREATER);
assertTrue(p, Integer.valueOf(value.intValue() - 1));
assertFalse(p, Integer.valueOf(value.intValue()));
assertFalse(p, Integer.valueOf(value.intValue() + 1));
}
@Test
public void compareLess() {
Integer value = Integer.valueOf(10);
Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.LESS);
assertFalse(p, Integer.valueOf(value.intValue() - 1));
assertFalse(p, Integer.valueOf(value.intValue()));
assertTrue(p, Integer.valueOf(value.intValue() + 1));
}
@Test
public void compareGreaterOrEqual() {
Integer value = Integer.valueOf(10);
Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.GREATER_OR_EQUAL);
assertTrue(p, Integer.valueOf(value.intValue() - 1));
assertTrue(p, Integer.valueOf(value.intValue()));
assertFalse(p, Integer.valueOf(value.intValue() + 1));
}
@Test
public void compareLessOrEqual() {
Integer value = Integer.valueOf(10);
Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.LESS_OR_EQUAL);
assertFalse(p, Integer.valueOf(value.intValue() - 1));
assertTrue(p, Integer.valueOf(value.intValue()));
assertTrue(p, Integer.valueOf(value.intValue() + 1));
}
@Override
protected Predicate<?> generatePredicate() {
return comparatorPredicate(Integer.valueOf(10), new TestComparator<Integer>());
}
}
/*
* 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.functors;
import static org.apache.commons.collections.functors.ComparatorPredicate.*;
import java.util.Comparator;
import org.apache.commons.collections.Predicate;
import org.junit.Test;
public class TestComparatorPredicate extends BasicPredicateTestBase {
private class TestComparator<T extends Comparable<T>> implements Comparator<T> {
public int compare(T first, T second) {
return first.compareTo(second);
}
}
@Test
public void compareEquals() {
Integer value = Integer.valueOf(10);
Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>());
assertFalse(p, Integer.valueOf(value.intValue() - 1));
assertTrue(p, Integer.valueOf(value.intValue()));
assertFalse(p, Integer.valueOf(value.intValue() + 1));
}
@Test
public void compareGreater() {
Integer value = Integer.valueOf(10);
Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.GREATER);
assertTrue(p, Integer.valueOf(value.intValue() - 1));
assertFalse(p, Integer.valueOf(value.intValue()));
assertFalse(p, Integer.valueOf(value.intValue() + 1));
}
@Test
public void compareLess() {
Integer value = Integer.valueOf(10);
Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.LESS);
assertFalse(p, Integer.valueOf(value.intValue() - 1));
assertFalse(p, Integer.valueOf(value.intValue()));
assertTrue(p, Integer.valueOf(value.intValue() + 1));
}
@Test
public void compareGreaterOrEqual() {
Integer value = Integer.valueOf(10);
Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.GREATER_OR_EQUAL);
assertTrue(p, Integer.valueOf(value.intValue() - 1));
assertTrue(p, Integer.valueOf(value.intValue()));
assertFalse(p, Integer.valueOf(value.intValue() + 1));
}
@Test
public void compareLessOrEqual() {
Integer value = Integer.valueOf(10);
Predicate<Integer> p = comparatorPredicate(value, new TestComparator<Integer>(), Criterion.LESS_OR_EQUAL);
assertFalse(p, Integer.valueOf(value.intValue() - 1));
assertTrue(p, Integer.valueOf(value.intValue()));
assertTrue(p, Integer.valueOf(value.intValue() + 1));
}
@Override
protected Predicate<?> generatePredicate() {
return comparatorPredicate(Integer.valueOf(10), new TestComparator<Integer>());
}
}