diff options
Diffstat (limited to 'israfil-foundation-collections')
15 files changed, 1420 insertions, 0 deletions
diff --git a/israfil-foundation-collections/pom.xml b/israfil-foundation-collections/pom.xml new file mode 100644 index 0000000..72014ea --- /dev/null +++ b/israfil-foundation-collections/pom.xml @@ -0,0 +1,29 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>net.israfil.foundation</groupId> + <artifactId>israfil-foundation-all</artifactId> + <version>5-SNAPSHOT</version> + </parent> + <artifactId>israfil-foundation-collections</artifactId> + <name>Israfil Foundation Collections Extras</name> + <version>1.0.3-SNAPSHOT</version> + <url>http://www.israfil.net/projects/foundation/${artifactId}</url> + <inceptionYear>2003</inceptionYear> + <licenses> + <license> + <name>BSD</name> + <distribution>repo</distribution> + <url>http://www.israfil.net/israfil-license-bsd.txt</url> + <comments>A simple open-source license with minimal restrictions</comments> + </license> + </licenses> + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>israfil-foundation-testing</artifactId> + <version>1.0.0</version> + <scope>test</scope> + </dependency> + </dependencies> +</project>
\ No newline at end of file diff --git a/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ArraySet.java b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ArraySet.java new file mode 100644 index 0000000..3447adc --- /dev/null +++ b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ArraySet.java @@ -0,0 +1,105 @@ +/*
+ * Copyright (c) 2003 - 2007 Israfil Consulting Services Corporation
+ * Copyright (c) 2003 - 2007 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: ArraySet.java 76 2006-08-03 15:37:05Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import java.util.AbstractSet;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+/**
+ * An ArrayList backed Set implementation, whose primary purpose is
+ * to provide a Set that maintains the order of elements based on
+ * indexes and/or the order in which items are added. Most
+ * particularly it's to guarrantee deterministic results of an
+ * iterator, where the iterator provides each element in the order
+ * of the underlying list.
+ *
+ * Unlike a LinkedHashSet, this implementation leaves a duplicate
+ * element in its original position. It's performance characteristics
+ * on input are similar, since a comparison still has to happen
+ * for de-dupliation, but obtaining the item by index is constant time.
+ *
+ * TODO: Consider making this a SortedSet, using the list order as the guarrantee. Not sure if it's a legit use of the API.
+ * @See java.util.LinkedHashSet
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ */
+public class ArraySet<E> extends AbstractSet<E> {
+
+ private transient ArrayList<E> internalList;
+
+ public ArraySet() {
+ internalList = new ArrayList<E>();
+ }
+
+ public ArraySet(int initialCapacity) {
+ internalList = new ArrayList<E>(initialCapacity);
+ }
+
+ public ArraySet(Collection<E> col) {
+ internalList = new ArrayList<E>(col);
+ Iterator<E> i = col.iterator();
+ while (i.hasNext()) {
+ internalList.add(i.next());
+ }
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ return internalList.iterator();
+ }
+
+ @Override
+ public int size() {
+ return internalList.size();
+ }
+
+ @Override
+ public boolean add(E o) {
+ if (internalList.contains(o)) return false;
+ else {
+ internalList.add(o);
+ return true;
+ }
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ return internalList.remove(o);
+ }
+
+ public E get(Integer index) {
+ return internalList.get(index);
+ }
+}
diff --git a/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ArrayUtils.java b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ArrayUtils.java new file mode 100644 index 0000000..880de92 --- /dev/null +++ b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ArrayUtils.java @@ -0,0 +1,113 @@ +/*
+ * Copyright (c) 2003, 2004, 2005, 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2003, 2004, 2005, 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: ArrayUtils.java 48 2006-02-23 06:11:53Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * An array utility class. This class is generic, so its usage is similar
+ * to the following:
+ * Foo[] foo1 = new Foo[]{ aFoo, bFoo, cFoo };
+ * Foo[] foo2 = new Foo[]{ dFoo, eFoo, fFoo };
+ * Foo[] fooResult = ArrayUtils.add(new Foo[],foo1,foo2);
+ *
+ * fooResult would be equivalent: new Foo[]{ aFoo, bFoo, cFoo, dFoo, eFoo, fFoo }
+ *
+ * This util is mostly helpful for putting together large batches of like
+ * arrays into a long single array, such as large groups of parameter lists
+ * or whatever.
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ */
+public class ArrayUtils {
+ private static final Logger logger = Logger.getLogger(ArrayUtils.class.getName());
+
+ /**
+ * Adds arrays of like type creating a new array containing
+ * all elements of the added arrays. Note that you have to
+ * pass in a typed array, because you can't create an array
+ * from a generic type in the code.
+ *
+ * i.e. you can't do this with Generics.
+ * <pre>
+ * public class Foo<T> {
+ * public T[] getArray() {
+ * T[] array = new T[a];
+ * }
+ * }
+ * </pre>
+ *
+ * However, as described in the tests, you can simply pass
+ * it an empty array of that type.
+ *
+ * @param resultBucket An array of the type you are attempting to aggregate. Can be empty. If it has sufficient space, it will be used for the aggregated result.
+ * @param arrays The arrays to be aggregated.
+ * @return An array of the provided type, containing the aggregated contents of the provided arrays.
+ */
+ public static <E> E[] aggregate(E[] resultBucket, E[] ... arrays)
+ {
+ logger.log(Level.FINER,Array.newInstance(arrays.getClass().getComponentType(),0).getClass().getCanonicalName());
+ List<E> resultList = new ArrayList<E>();
+ for (E[] t : arrays) {
+ for (E element : t) {
+ resultList.add(element);
+ }
+ }
+ logger.log(Level.FINER,resultList.toString());
+ return (E[])resultList.toArray(resultBucket);
+ }
+
+ /**
+ * A method that tests the eqivalence (content equality) of
+ * two arrays.
+ * @param a1
+ * @param a2
+ * @return
+ */
+ public static boolean equivalent(Object[] o1, Object[] o2) {
+ if (o1 == null && (o2 == null || o2.length == 0)) return true;
+ if (o2 == null && o1.length == 0) return true;
+ if (o1.length != o2.length) return false;
+ for (int i = 0; i < o1.length; i++) {
+ if (o1[i] != o2[i]) return false;
+ }
+ return true;
+ }
+
+}
+
diff --git a/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/IterableResultSet.java b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/IterableResultSet.java new file mode 100644 index 0000000..9258c6f --- /dev/null +++ b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/IterableResultSet.java @@ -0,0 +1,132 @@ +/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: IterableResultSet.java 26 2006-02-13 16:35:44Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import java.lang.ref.WeakReference;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.WeakHashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * A simple wrapper that provides an Iterable implementation back-ended
+ * by a result-set. Allows a ResultSet to be used in a JDK 5.0 extended
+ * for-loop syntax.
+ *
+ * No result set can have more than one Iterable wrapper around it at a time.
+ * However, it is impossible for this object to know implicitly when it is
+ * no longer required (to free the reference). Therefore, when this
+ * wrapper is concluded, it should be disposed of via the dispose() method.
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ */
+public class IterableResultSet implements Iterable<Map<String,Object>> {
+
+ protected static final Map<ResultSet,IterableResultSet> iterables = new WeakHashMap<ResultSet,IterableResultSet>();
+ protected final WeakReference<ResultSet> resultRef;
+
+ public IterableResultSet(ResultSet result) {
+ if (iterables.containsKey(result)) throw new RuntimeException("Cannot have two IterableResultSet wrappers around the same resultset.");
+ this.resultRef = new WeakReference<ResultSet>(result);
+ }
+
+ public Iterator<Map<String,Object>> iterator() {
+ if (resultRef.get() == null) throw new RuntimeException("ResultSet object not available to iterate.");
+ return new ResultSetIterator(resultRef.get());
+ }
+
+ public void dispose() {
+ ResultSet rs = resultRef.get();
+ if (rs == null) return;
+ if (iterables.containsKey(rs)) {
+ iterables.get(rs).dispose();
+ }
+ resultRef.clear();
+ }
+
+ public static class ResultSetIterator implements Iterator<Map<String,Object>> {
+ private static final Logger logger = Logger.getLogger(ResultSetIterator.class.getName());
+ protected ResultSet result;
+
+ boolean hasNext = true;
+
+
+ public ResultSetIterator(ResultSet result) {
+ this.result = result;
+ try {
+ hasNext = result.first();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public boolean hasNext() {
+ return hasNext;
+ }
+
+ public Map<String,Object> next() {
+ try {
+ Map<String,Object> row = new HashMap<String,Object>();
+ for(int i = 1; i <= result.getMetaData().getColumnCount(); i++) {
+ row.put(result.getMetaData().getColumnName(i),result.getObject(i));
+ }
+ hasNext = result.next();
+ return row;
+ } catch (SQLException e) {
+ logger.log(Level.FINER,"SQLException thrown while iterating across result set.",e);
+ return null;
+ }
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException("Removal of records from underlying result set not supported.");
+ }
+
+ public void dispose() {
+ if (iterables.containsKey(result)) iterables.remove(result);
+ result = null;
+ }
+
+ public void finalize() throws Throwable {
+ dispose();
+ super.finalize();
+ }
+ }
+}
+
+
diff --git a/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/RandomOrderIterable.java b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/RandomOrderIterable.java new file mode 100644 index 0000000..3b5656d --- /dev/null +++ b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/RandomOrderIterable.java @@ -0,0 +1,72 @@ +/*
+ * Copyright (c) 2007 Israfil Consulting Services Corporation
+ * Copyright (c) 2007 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: IterableResultSet.java 26 2006-02-13 16:35:44Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import java.security.SecureRandom;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Random;
+
+/**
+ * A simple wrapper that provides an Iterable implementation that wraps
+ * a collection and returns a RandomOrderIterator when asked for an iterator.
+ *
+ * No result set can have more than one Iterable wrapper around it at a time.
+ * However, it is impossible for this object to know implicitly when it is
+ * no longer required (to free the reference). Therefore, when this
+ * wrapper is concluded, it should be disposed of via the dispose() method.
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ */
+public class RandomOrderIterable<E> implements Iterable<E> {
+
+ private Collection<E> _collection;
+ private Random _random = null;
+
+ public RandomOrderIterable(Collection<E> collection) {
+ _collection = collection;
+ }
+
+ public RandomOrderIterable(Collection<E> collection, Random random) {
+ this(collection);
+ _random = random;
+ }
+
+ public Iterator<E> iterator() {
+ return new RandomOrderIterator<E>(_collection,(_random == null) ? new SecureRandom() : _random);
+ }
+
+}
+
+
diff --git a/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/RandomOrderIterator.java b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/RandomOrderIterator.java new file mode 100644 index 0000000..8b97e4a --- /dev/null +++ b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/RandomOrderIterator.java @@ -0,0 +1,94 @@ +/*
+ * Copyright (c) 2007 Israfil Consulting Services Corporation
+ * Copyright (c) 2007 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: ArraySet.java 76 2006-08-03 15:37:05Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import java.security.SecureRandom;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Random;
+
+/**
+ * An iterator that is constructed from a collection and returns the
+ * contents of the collection in random order. Note that this is
+ * an iterator over a copy of the collection, not the collection
+ * itself, so remove() is not supported, and underlying changes
+ * to the original collection will not be reflected in this iterator.
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ */
+public class RandomOrderIterator<E> implements Iterator<E> {
+
+ private transient List<E> _collection;
+
+ private transient Random _random = new SecureRandom();
+
+ private transient List<Integer> _indexes;
+
+
+ public RandomOrderIterator(Collection<E> collection) {
+ _collection = new ArrayList<E>(collection);
+ _indexes = new LinkedList<Integer>();
+ for (int i = 0; i < _collection.size();i++) {
+ _indexes.add(i); // populate index source.
+ }
+ }
+
+ public RandomOrderIterator(Collection<E> collection, Random random) {
+ this(collection);
+ this._random = random;
+ }
+
+ public boolean hasNext() {
+ return _indexes.size() > 0;
+ }
+
+ public E next() {
+ Integer index = _indexes.remove(_random.nextInt(_indexes.size()));
+ return _collection.get(index);
+ }
+
+ /**
+ * Because of the means required to track the current node,
+ * removing anything from the underlying collection is not supported.
+ */
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+
+
+}
diff --git a/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ReciprocalHashMap.java b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ReciprocalHashMap.java new file mode 100644 index 0000000..0ac0164 --- /dev/null +++ b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ReciprocalHashMap.java @@ -0,0 +1,202 @@ +/*
+ * Copyright (c) 2003, Christian Edward Gruber
+ * Copyright (c) 2003, Israfil Consulting Services Corporation
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: ReciprocalHashMap.java 129 2006-12-31 23:20:02Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * A two-way map, backed by java.util.HashMaps, where each key matches one
+ * value and is unique, and where each value maps to a key, and is unique
+ * among all values.
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber</a>
+ *
+ */
+public class ReciprocalHashMap<K,V> extends java.util.AbstractMap<K,V>
+ implements ReciprocalMap<K,V> {
+
+ Set<Map.Entry<K,V>> entrySetByKey = new HashSet<Map.Entry<K,V>>();
+ Set<Map.Entry<V,K>> entrySetByValue = new HashSet<Map.Entry<V,K>>();
+
+ @Override
+ public Set<Map.Entry<K,V>> entrySet() {
+ return entrySetByKey;
+ }
+
+ public Set<Map.Entry<V,K>> entrySetByValue() {
+ return entrySetByValue;
+ }
+
+ public boolean contains(K key, V value) {
+ V value1 = get(key);
+ if (value == value1) return true;
+ else if (value == null) return false; // assume null == null handled.
+ else return value.equals(value1);
+ }
+
+ public Entry<V,K> getEntryByValue(V value) {
+ Iterator<Entry<V,K>> i = entrySetByValue().iterator();
+ if (value==null) {
+ while (i.hasNext()) {
+ Entry<V,K> e = i.next();
+ if (e.getKey()==null) return e;
+ }
+ } else {
+ while (i.hasNext()) {
+ Entry<V,K> e = i.next();
+ if (value.equals(e.getKey())) return e;
+ }
+ }
+ return null;
+ }
+ public Entry<K,V> getEntryByKey(K value) {
+ Iterator<Entry<K,V>> i = entrySet().iterator();
+ if (value==null) {
+ while (i.hasNext()) {
+ Entry<K,V> e = i.next();
+ if (e.getKey()==null) return e;
+ }
+ } else {
+ while (i.hasNext()) {
+ Entry<K,V> e = i.next();
+ if (value.equals(e.getKey())) return e;
+ }
+ }
+ return null;
+ }
+
+ public K getKey(V value) {
+ Entry<V,K> e = getEntryByValue(value);
+ return (e == null) ? null : e.getValue();
+ }
+
+ public V getValue(K key) {
+ Entry<K,V> e = getEntryByKey(key);
+ return (e == null) ? null : e.getValue();
+ }
+
+ public K removeByValue(V value) {
+ Entry<V,K> valEntry = getEntryByValue(value);
+ Entry<K,V> keyEntry = getEntryByKey(valEntry.getValue());
+ if (keyEntry != null) entrySetByKey.remove(keyEntry);
+ if (valEntry != null) entrySetByValue.remove(valEntry);
+ return keyEntry.getKey();
+ }
+
+ public V removeByKey(K key) {
+ Entry<K,V> keyEntry = getEntryByKey(key);
+ Entry<V,K> valEntry = getEntryByValue(keyEntry.getValue());
+ if (keyEntry != null) entrySetByKey.remove(keyEntry);
+ if (valEntry != null) entrySetByValue.remove(valEntry);
+ return valEntry.getKey();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override public V remove(Object key) {
+ return this.removeByKey((K)key);
+ }
+
+
+ @Override public V put(K key, V value) {
+ Entry<K,V> keyEntry = getEntryByKey(key);
+ Entry<V,K> valueEntry = getEntryByValue(value);
+
+ V oldValue = null;
+
+ if (keyEntry != null) {
+ Entry<V,K> inverseKeyEntry = getEntryByValue(keyEntry.getValue());
+ keyEntry.getKey();
+ entrySetByKey.remove(keyEntry);
+ entrySetByValue.remove(inverseKeyEntry);
+ }
+ if (valueEntry != null) {
+ Entry<K,V> inverseValueEntry = getEntryByKey(valueEntry.getValue());
+ oldValue = valueEntry.getKey();
+ entrySetByValue.remove(valueEntry);
+ entrySetByKey.remove(inverseValueEntry);
+ }
+ entrySetByKey.add(new ReciprocalEntry<K,V>(key,value));
+ entrySetByValue.add(new ReciprocalEntry<V,K>(value,key));
+
+ return oldValue;
+ }
+
+ static class ReciprocalEntry<K, V> implements Entry<K, V> {
+ private K key;
+ private V value;
+
+ public ReciprocalEntry(K key, V value) {
+ this.key = key;
+ this.value = value;
+ }
+
+ public K getKey() { return key; }
+
+ public V getValue() { return value; }
+
+ public V setValue(V value) {
+ V oldValue = this.value;
+ this.value = value;
+ return oldValue;
+ }
+
+ public int hashCode() {
+ return ((key == null) ? 0 : key.hashCode()) ^
+ ((value == null) ? 0 : value.hashCode());
+ }
+
+ @Override public String toString() {
+ return "ReciprocalEntry[" + key + "=" + value + "]";
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof Map.Entry)) return false;
+ try {
+ Map.Entry<K,V> e = (Map.Entry<K,V>)o;
+ return safeEquals(key, e.getKey()) && safeEquals(value, e.getValue());
+ } catch (ClassCastException cce) {
+ return false;
+ }
+ }
+
+ private static boolean safeEquals(Object o1, Object o2) {
+ return (o1 == null ? o2 == null : o1.equals(o2));
+ }
+ }
+
+}
\ No newline at end of file diff --git a/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ReciprocalMap.java b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ReciprocalMap.java new file mode 100644 index 0000000..687d3ed --- /dev/null +++ b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/ReciprocalMap.java @@ -0,0 +1,49 @@ +/*
+ * Copyright (c) 2003, Christian Edward Gruber
+ * Copyright (c) 2003, Israfil Consulting Services Corporation
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: ReciprocalMap.java 129 2006-12-31 23:20:02Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import java.util.Map;
+
+/**
+ * Interface for a two-way map, where each key matches one value and is unique,
+ * and where each value maps to a key, and is unique among all values.
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber</a>
+ */
+public interface ReciprocalMap<K,V> extends Map<K,V> {
+ public boolean contains( K key, V value );
+ public K getKey( V value );
+ public K removeByValue( V value );
+ public V getValue( K key );
+ public V removeByKey( K key );
+}
diff --git a/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/SoloCollection.java b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/SoloCollection.java new file mode 100644 index 0000000..83a9f66 --- /dev/null +++ b/israfil-foundation-collections/src/main/java/net/israfil/foundation/collections/SoloCollection.java @@ -0,0 +1,66 @@ +/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: ArraySet.java 19 2006-01-30 21:52:52Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import java.util.AbstractCollection;
+import java.util.Iterator;
+
+/**
+ * A simple unmodifyable implementation of Collection that takes a single (possibly
+ * null) constructor parameter. The collection then "contains" this single object.
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ */
+//@Copyright(years={"2006"},owner="Israfil Consulting Services Corporation",license="BSD")
+
+public class SoloCollection<E> extends AbstractCollection<E> {
+ protected final E o;
+ public SoloCollection(E o) {
+ super();
+ this.o = o;
+ }
+
+ public Iterator<E> iterator() {
+ return new Iterator<E>() {
+ boolean beforeFirst = true;
+ public boolean hasNext() { return beforeFirst; }
+ public E next() { beforeFirst = false; return o; }
+ public void remove() {
+ throw new UnsupportedOperationException("Cannot remove from a singleton colelction");
+ }
+ };
+ }
+
+ public int size() { return 1; }
+}
diff --git a/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/ArraySetTest.java b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/ArraySetTest.java new file mode 100644 index 0000000..5e804e4 --- /dev/null +++ b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/ArraySetTest.java @@ -0,0 +1,125 @@ +/*
+ * Copyright (c) 2003 - 2008 Israfil Consulting Services Corporation
+ * Copyright (c) 2003 - 2008 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: SoloCollectionTest.java 130 2006-12-31 23:22:17Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ * @author Latest: $Author: cgruber $
+ * @version $Revision: 76 $
+ */
+@Test
+public class ArraySetTest {
+
+ ArraySet<String> stringSet;
+ String string1 = "String1";
+ String string2 = "String2";
+ String string3 = "String3";
+
+ @BeforeMethod
+ protected void setUp() throws Exception {
+ stringSet = new ArraySet<String>();
+ }
+
+ @AfterMethod
+ protected void tearDown() throws Exception {
+ stringSet = null;
+ }
+
+ public void testBasicArrayAndListBehaviour() {
+ Assert.assertEquals(0,stringSet.size());
+ stringSet.add(string1);
+ stringSet.add(string2);
+ stringSet.add(string3);
+ Assert.assertEquals(3,stringSet.size());
+ stringSet.add(string2);
+ Assert.assertEquals(3,stringSet.size());
+ Iterator<String> iter = stringSet.iterator();
+ Assert.assertEquals(string1,iter.next());
+ Assert.assertEquals(string2,iter.next());
+ Assert.assertEquals(string3,iter.next());
+ }
+
+ public void testAddAllFromArrayList() {
+ List<String> list = new ArrayList<String>();
+ list.add(string1);
+ list.add(string2);
+ list.add(string3);
+ list.add(string2);
+ Assert.assertEquals(4,list.size());
+ stringSet.addAll(list);
+ Assert.assertEquals(3,stringSet.size());
+ Iterator<String> iter = stringSet.iterator();
+ Assert.assertEquals(string1,iter.next());
+ Assert.assertEquals(string2,iter.next());
+ Assert.assertEquals(string3,iter.next());
+ }
+
+ public void testRemove() {
+ Assert.assertEquals(0,stringSet.size());
+ stringSet.add(string1);
+ stringSet.add(string2);
+ stringSet.add(string3);
+ Assert.assertEquals(3,stringSet.size());
+ stringSet.remove(string2);
+ Assert.assertEquals(2,stringSet.size());
+ Iterator<String> iter = stringSet.iterator();
+ Assert.assertEquals(string1,iter.next());
+ Assert.assertEquals(string3,iter.next());
+ }
+
+ public void testCreateWithCapacity() {
+ stringSet = new ArraySet<String>(3);
+ stringSet.add(string1);
+ Assert.assertEquals(string1,stringSet.iterator().next());
+ }
+
+ public void testCreateWithCollection() {
+ ArrayList<String> list = new ArrayList<String>();
+ list.add(string1);
+ list.add(string2);
+ list.add(string1);
+ stringSet = new ArraySet<String>(list);
+ Assert.assertEquals(string1,stringSet.iterator().next());
+ }
+}
diff --git a/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/ArrayUtilsTest.java b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/ArrayUtilsTest.java new file mode 100644 index 0000000..3d7edec --- /dev/null +++ b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/ArrayUtilsTest.java @@ -0,0 +1,107 @@ +/*
+ * Copyright (c) 2003 - 2008 Israfil Consulting Services Corporation
+ * Copyright (c) 2003 - 2008 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: SoloCollectionTest.java 130 2006-12-31 23:22:17Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ * @author Latest: $Author: cgruber $
+ * @version $Revision: 48 $
+ */
+@Test
+public class ArrayUtilsTest {
+
+ final String s1 = "s1",s2 = "s1",s3 = "s1",s4 = "s1",
+ s5 = "s1",s6 = "s1",s7 = "s1";
+
+ public void testArrayUtilOrder1() {
+ String[] sArray1 = new String[] { s1,s2,s3 };
+ String[] sArray2 = new String[] { s4,s5 };
+ String[] sArray3 = new String[] { s6,s7 };
+ String[] saResult = ArrayUtils.aggregate(new String[] {},sArray1,sArray2,sArray3);
+ String[] expected = new String[]{s1,s2,s3,s4,s5,s6,s7};
+ for (int i = 0; i < saResult.length ; i++ ) {
+ Assert.assertEquals(expected[i],saResult[i]);
+ }
+ Assert.assertEquals(String[].class,saResult.getClass());
+ }
+
+ public void testArrayUtilOrder2() {
+ String[] sArray2 = new String[] { s1,s2,s3 };
+ String[] sArray1 = new String[] { s4,s5 };
+ String[] sArray3 = new String[] { s6,s7 };
+ String[] saResult = ArrayUtils.aggregate(new String[] {},sArray1,sArray2,sArray3);
+ String[] expected = new String[]{s4,s5,s1,s2,s3,s6,s7};
+ for (int i = 0; i < saResult.length ; i++ ) {
+ Assert.assertEquals(expected[i],saResult[i]);
+ }
+ Assert.assertEquals(String[].class,saResult.getClass());
+ }
+
+ public void testVaryingTypeAddition() {
+ TestA tA1 = new TestA("tA1");
+ TestA[] tAa1 = new TestA[] { tA1 };
+ TestB tB1 = new TestB("tB1");
+ TestB[] tBa1 = new TestB[] { tB1 };
+ TestA[] result = ArrayUtils.aggregate(new TestA[]{},tAa1,tBa1);
+ Assert.assertEquals(2,result.length);
+ Assert.assertEquals(TestA[].class,result.getClass());
+ }
+
+ public void testStringArrayAggregation() {
+ String[] sa1 = { "s1", "s2" };
+ String[] sa2 = { "s3", "s4" };
+ String[] sa3 = ArrayUtils.aggregate(new String[]{},sa1,sa2);
+ Assert.assertEquals(sa1[0],sa3[0]);
+ Assert.assertEquals(sa1[1],sa3[1]);
+ Assert.assertEquals(sa2[0],sa3[2]);
+ Assert.assertEquals(sa2[1],sa3[3]);
+ }
+
+
+ public class TestA {
+ public final String text;
+ public TestA(String text) {
+ this.text = text;
+ }
+ }
+ public class TestB extends TestA {
+ public TestB(String ext) {
+ super(ext);
+ }
+ }
+}
diff --git a/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/IterableResultSetTest.java b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/IterableResultSetTest.java new file mode 100644 index 0000000..ae04d14 --- /dev/null +++ b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/IterableResultSetTest.java @@ -0,0 +1,97 @@ +/*
+ * Copyright (c) 2003 - 2008 Israfil Consulting Services Corporation
+ * Copyright (c) 2003 - 2008 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: SoloCollectionTest.java 130 2006-12-31 23:22:17Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import java.sql.ResultSet;
+import java.util.Map;
+
+import net.israfil.foundation.mock.sql.MockResultSet;
+
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ * @author Latest: $Author: cgruber $
+ * @version $Revision: 26 $
+ */
+@Test
+public class IterableResultSetTest {
+
+ String colA = "columnA", colB = "columnB" , colC = "columnC";
+ String[] columns = { colA, colB, colC };
+ Object[][] rows = { { new Integer(0), "cBr0", new Float(2.56f) },
+ { new Integer(1), "cBr1", new Float(4.45f) },
+ { new Integer(2), "cBr2", new Float(9.15f) },
+ { new Integer(3), "cBr3", new Float(-25.45f) } };
+
+ IterableResultSet irs;
+ ResultSet rs;
+
+ @BeforeMethod
+ protected void setUp() throws Exception {
+ rs = new MockResultSet(columns,rows);
+ irs = new IterableResultSet( rs );
+ }
+
+ @AfterMethod
+ protected void tearDown() throws Exception {
+ rs = null;
+ irs = null;
+ }
+
+ @Test(expectedExceptions={UnsupportedOperationException.class})
+ public void testRemove() {
+ irs.iterator().remove();
+ }
+
+ public void testIterableRSInAction() throws Throwable {
+ rs.last();
+ IterableResultSet irs = new IterableResultSet(rs);
+ int rowCount = 0;
+ for (Map<String,Object> row : irs) {
+ rowCount++;
+ for(int colNum = 0; colNum < columns.length; colNum++) {
+ Assert.assertEquals(rows[rowCount-1][colNum],row.get(columns[colNum]));
+ }
+ }
+ Assert.assertEquals(4,rowCount);
+ irs.dispose();
+ }
+
+
+}
diff --git a/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/RandomOrderIteratorTest.java b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/RandomOrderIteratorTest.java new file mode 100644 index 0000000..3b7de37 --- /dev/null +++ b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/RandomOrderIteratorTest.java @@ -0,0 +1,104 @@ +/*
+ * Copyright (c) 2003 - 2008 Israfil Consulting Services Corporation
+ * Copyright (c) 2003 - 2008 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: SoloCollectionTest.java 130 2006-12-31 23:22:17Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Random;
+
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ * @author Latest: $Author: cgruber $
+ * @version $Revision: 76 $
+ */
+@Test
+public class RandomOrderIteratorTest {
+
+ Collection<String> c;
+ final String string1 = "String1";
+ final String string2 = "String2";
+ final String string3 = "String3";
+
+ @BeforeMethod
+ protected void setUp() throws Exception {
+ c = new ArrayList<String>();
+ c.add(string1);
+ c.add(string2);
+ c.add(string3);
+ }
+
+ @AfterMethod
+ public void tearDown() throws Exception {
+ c = null;
+ }
+
+ public void testRandomIteratorWithSeed0() {
+ Iterator<String> i = new RandomOrderIterator<String>(c,new Random(0l));
+ Assert.assertTrue(i.hasNext());
+ Assert.assertSame(string1,i.next());
+ Assert.assertTrue(i.hasNext());
+ Assert.assertSame(string3,i.next());
+ Assert.assertTrue(i.hasNext());
+ Assert.assertSame(string2,i.next());
+ Assert.assertFalse(i.hasNext());
+ }
+ public void testRandomIteratorWithSeed5() {
+ Iterator<String> i = new RandomOrderIterator<String>(c,new Random(5l));
+ Assert.assertTrue(i.hasNext());
+ Assert.assertSame(string3,i.next());
+ Assert.assertTrue(i.hasNext());
+ Assert.assertSame(string1,i.next());
+ Assert.assertTrue(i.hasNext());
+ Assert.assertSame(string2,i.next());
+ Assert.assertFalse(i.hasNext());
+ }
+ public void testRandomIteratorWithSeed92283842() {
+ Iterator<String> i = new RandomOrderIterator<String>(c,new Random(92283842l));
+ Assert.assertTrue(i.hasNext());
+ Assert.assertSame(string2,i.next());
+ Assert.assertTrue(i.hasNext());
+ Assert.assertSame(string3,i.next());
+ Assert.assertTrue(i.hasNext());
+ Assert.assertSame(string1,i.next());
+ Assert.assertFalse(i.hasNext());
+ }
+
+}
diff --git a/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/ReciprocalHashMapTest.java b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/ReciprocalHashMapTest.java new file mode 100644 index 0000000..b49dfe8 --- /dev/null +++ b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/ReciprocalHashMapTest.java @@ -0,0 +1,70 @@ +/*
+ * Copyright (c) 2003 - 2008 Israfil Consulting Services Corporation
+ * Copyright (c) 2003 - 2008 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: SoloCollectionTest.java 130 2006-12-31 23:22:17Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ * @author Latest: $Author: cgruber $
+ * @version $Revision: 130 $
+ */
+@Test
+public class ReciprocalHashMapTest {
+
+ public void testSimpleCase() throws Throwable {
+ ReciprocalHashMap<String,Integer> map = new ReciprocalHashMap<String,Integer>();
+ Assert.assertNotNull(map);
+ map.put("Five",5);
+ Assert.assertEquals(1,map.size());
+ Assert.assertTrue(map.contains("Five",5));
+ Assert.assertEquals("Five",map.getKey(5));
+ Assert.assertEquals((Integer)5,map.getValue("Five"));
+ Assert.assertEquals((Integer)5,map.get("Five"));
+
+ map.put("Three",5);
+ Assert.assertEquals("Three",map.getKey(5));
+ Assert.assertEquals((Integer)5,map.getValue("Three"));
+ Assert.assertEquals(null,map.getValue("Five"));
+ Assert.assertEquals(1,map.size());
+
+ map.put("Five",4);
+ Assert.assertEquals("Five",map.getKey(4));
+ Assert.assertEquals((Integer)4,map.getValue("Five"));
+ Assert.assertEquals(2,map.size());
+ }
+
+}
diff --git a/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/SoloCollectionTest.java b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/SoloCollectionTest.java new file mode 100644 index 0000000..0c91598 --- /dev/null +++ b/israfil-foundation-collections/src/test/java/net/israfil/foundation/collections/SoloCollectionTest.java @@ -0,0 +1,55 @@ +/*
+ * Copyright (c) 2003 - 2008 Israfil Consulting Services Corporation
+ * Copyright (c) 2003 - 2008 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: SoloCollectionTest.java 130 2006-12-31 23:22:17Z cgruber $
+ */
+package net.israfil.foundation.collections;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ * @author Latest: $Author: cgruber $
+ * @version $Revision: 130 $
+ */
+@Test
+public class SoloCollectionTest {
+
+ public void testSingleCollection() {
+ Object o = new Object();
+ SoloCollection<Object> sc = new SoloCollection<Object>(o);
+ Assert.assertNotNull(sc.iterator());
+ Assert.assertTrue(sc.contains(o));
+ Assert.assertEquals(o,sc.iterator().next());
+ }
+}
|
