diff --git a/commons/src/main/java/org/archive/util/Transform.java b/commons/src/main/java/org/archive/util/Transform.java deleted file mode 100644 index c066dbf50..000000000 --- a/commons/src/main/java/org/archive/util/Transform.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * This file is part of the Heritrix web crawler (crawler.archive.org). - * - * Licensed to the Internet Archive (IA) by one or more individual - * contributors. - * - * The IA 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.archive.util; - -import java.util.AbstractCollection; -import java.util.Collection; -import java.util.Iterator; - - -/** - * A transformation of a collection. The elements in the transform are based - * on the elements of some other collection; the original collection's - * elements are transformed using a specified transformer. Changes to the - * original collection are automatically reflected in the transform and - * vice-versa. - * - *

If the transformer returns null for a given original object, then that - * object will not be included in the transform. Thus the transform might - * be smaller than the original collection. Note that Transform instances - * can never contain the null element. - * - *

This collection implementation does not support the optional add - * operation. - * - * @author pjack - * - * @param the type of the original elements in the collection - * @param the type of the tranformed elements - */ -public class Transform -extends AbstractCollection { - - /** The original collection. */ - final private Collection delegate; - - /** Transforms the original objects. */ - final private Transformer transformer; - - /** - * Constructor. - * - * @param delegate The collection whose elements to transform. - * @param transformer Transforms the elements - */ - public Transform(Collection delegate, - Transformer transformer) { - this.delegate = delegate; - this.transformer = transformer; - } - - public int size() { - int count = 0; - Iterator iter = iterator(); - while (iter.hasNext()) { - iter.next(); - count++; - } - return count; - } - - public Iterator iterator() { - return new TransformIterator( - delegate.iterator(), transformer); - } - - - /** - * Returns a transform containing only objects of a given class. - * - * @param the target class - * @param c the collection to transform - * @param cls the class of objects to return - * @return a collection containing only objects of class cls - */ - public static Collection subclasses( - Collection c, - final Class cls) { - Transformer t = new Transformer() { - public Target transform(Object s) { - if (cls.isInstance(s)) { - return cls.cast(s); - } else { - return null; - } - } - }; - return new Transform(c, t); - } -} - - -class TransformIterator implements Iterator { - - final private Iterator iterator; - final private Transformer transformer; - private Transformed next; - - public TransformIterator(Iterator iterator, - Transformer transformer) { - this.iterator = iterator; - this.transformer = transformer; - } - - public boolean hasNext() { - if (next != null) { - return true; - } - while (iterator.hasNext()) { - Original o = iterator.next(); - next = transformer.transform(o); - if (next != null) { - return true; - } - } - return false; - } - - public Transformed next() { - if (!hasNext()) { - throw new IllegalStateException(); - } - Transformed r = next; - next = null; - return r; - } - - // FIXME: this can break standard Iterator contract, for example - // transformIterator.next(); - // if(transformIterator.hasNext()) { - // transformIterator.remove(); - // } - // usual iterator contract is to remove the last object returned - // by next; in this case the subsequent - public void remove() { - iterator.remove(); - } - -} diff --git a/commons/src/main/java/org/archive/util/Transformer.java b/commons/src/main/java/org/archive/util/Transformer.java deleted file mode 100644 index 4c36b649d..000000000 --- a/commons/src/main/java/org/archive/util/Transformer.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * This file is part of the Heritrix web crawler (crawler.archive.org). - * - * Licensed to the Internet Archive (IA) by one or more individual - * contributors. - * - * The IA 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.archive.util; - - -/** - * Transforms objects from one thing into another. - * - * @author pjack - * - * @param the type of the original objects - * @param the type of the transformed objects - */ -public interface Transformer { - - /** - * Transforms the given object. - * - * @param o the object to transform - * @return the transformed object - */ - Transformed transform(Original o); - -} diff --git a/commons/src/test/java/org/archive/util/TransformTest.java b/commons/src/test/java/org/archive/util/TransformTest.java deleted file mode 100644 index 51641fcf8..000000000 --- a/commons/src/test/java/org/archive/util/TransformTest.java +++ /dev/null @@ -1,126 +0,0 @@ -package org.archive.util; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Random; -import java.util.Set; - -import junit.framework.TestCase; - -/** - * Tests the {@link dex.misc.Transform} class. - */ -public class TransformTest extends TestCase { - - // Convert integers to strings, eliminating negative numbers - private static class PositiveToString - implements Transformer { - public String transform(Integer i) { - if (i < 0) { - return null; - } - return i.toString(); - } - } - - /** - * Tests using a simple Transformer. The Transformer changes - * positive integers into strings. The test sets up a - * list of random integers, remembering which ones are - * positive. The Transform is created, and the Transform's - * contents are compared against the list of remembered positive - * integers. - */ - public void testTransform() { - Transformer transformer = new PositiveToString(); - - // Transform of an empty collection should be empty. - List empty = new ArrayList(); - assertTrue(new Transform(empty, transformer).isEmpty()); - - // Some simple test data. - Integer[] testData = new Integer[] { -5, 3, 2, -11, 0, 111, -161 }; - String[] expected = new String[] { "3", "2", "0", "111" }; - List list = Arrays.asList(testData); - Transform c = new Transform(list, - transformer); - List expectedList = Arrays.asList(expected); - assertEquals(new ArrayList(c), expectedList); - - // Same test as above, with random data - for (int i = 0; i < 100; i++) { - randomTest(); - } - } - - private void randomTest() { - Transformer transformer = new PositiveToString(); - Random random = new Random(); - int max = random.nextInt(1024) + 10; - List testData = new ArrayList(max); - List expected = new ArrayList(max); - for (int i = 0; i < max; i++) { - int e = random.nextInt(); - testData.add(e); - if (e >= 0) { - expected.add(Integer.toString(e)); - } - } - - Transform c = new Transform(testData, - transformer); - List results = new ArrayList(c); - assertEquals(expected, results); - } - - /** - * Tests the static subclasses method. The test sets up a list of - * Number instances that may contain random Double, Float, Integer - * or Long values. The Long values are remembered. The subclasses - * method is used to create a Transform containing only the Long - * values. The Transform is compared against the list of remembered - * Long values. - */ - public void testSubclasses() { - Random random = new Random(); - for (int i = 0; i < 100; i++) { - int max = random.nextInt(1024) + 10; - List testData = new ArrayList(max); - List expected = new ArrayList(max); - for (int j = 0; j < max; j++) { - int v = random.nextInt(4); - switch (v) { - case 0: - long l = random.nextLong(); - testData.add(l); - expected.add(l); - break; - case 1: - testData.add(random.nextInt()); - break; - case 2: - testData.add(random.nextDouble()); - break; - case 3: - testData.add(random.nextFloat()); - break; - } - } - Collection c = Transform.subclasses(testData, Long.class); - List results = new ArrayList(c); - assertEquals(expected, results); - } - } - - public void testSingleton() { - Set set = new HashSet(); - set.add(3); - Collection c = Transform.subclasses(set, Integer.class); - for (Integer i : c) { - System.out.println(i); - } - } -}