Merge pull request #86 from nlevitt/remove-cruft

remove unused Transform* classes
This commit is contained in:
Kenji Nagahashi
2014-08-25 15:30:08 -07:00
3 changed files with 0 additions and 320 deletions
@@ -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.
*
* <p>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.
*
* <p>This collection implementation does not support the optional add
* operation.
*
* @author pjack
*
* @param <Original> the type of the original elements in the collection
* @param <Transformed> the type of the tranformed elements
*/
public class Transform<Original,Transformed>
extends AbstractCollection<Transformed> {
/** The original collection. */
final private Collection<? extends Original> delegate;
/** Transforms the original objects. */
final private Transformer<Original,Transformed> transformer;
/**
* Constructor.
*
* @param delegate The collection whose elements to transform.
* @param transformer Transforms the elements
*/
public Transform(Collection<? extends Original> delegate,
Transformer<Original,Transformed> transformer) {
this.delegate = delegate;
this.transformer = transformer;
}
public int size() {
int count = 0;
Iterator<Transformed> iter = iterator();
while (iter.hasNext()) {
iter.next();
count++;
}
return count;
}
public Iterator<Transformed> iterator() {
return new TransformIterator<Original,Transformed>(
delegate.iterator(), transformer);
}
/**
* Returns a transform containing only objects of a given class.
*
* @param <Target> 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 <Target> Collection<Target> subclasses(
Collection<? extends Object> c,
final Class<Target> cls) {
Transformer<Object,Target> t = new Transformer<Object,Target>() {
public Target transform(Object s) {
if (cls.isInstance(s)) {
return cls.cast(s);
} else {
return null;
}
}
};
return new Transform<Object,Target>(c, t);
}
}
class TransformIterator<Original,Transformed> implements Iterator<Transformed> {
final private Iterator<? extends Original> iterator;
final private Transformer<Original,Transformed> transformer;
private Transformed next;
public TransformIterator(Iterator<? extends Original> iterator,
Transformer<Original,Transformed> 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();
}
}
@@ -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 <Original> the type of the original objects
* @param <Transformed> the type of the transformed objects
*/
public interface Transformer<Original,Transformed> {
/**
* Transforms the given object.
*
* @param o the object to transform
* @return the transformed object
*/
Transformed transform(Original o);
}
@@ -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<Integer, String> {
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<Integer,String> transformer = new PositiveToString();
// Transform of an empty collection should be empty.
List<Integer> empty = new ArrayList<Integer>();
assertTrue(new Transform<Integer,String>(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<Integer> list = Arrays.asList(testData);
Transform<Integer,String> c = new Transform<Integer,String>(list,
transformer);
List<String> expectedList = Arrays.asList(expected);
assertEquals(new ArrayList<String>(c), expectedList);
// Same test as above, with random data
for (int i = 0; i < 100; i++) {
randomTest();
}
}
private void randomTest() {
Transformer<Integer, String> transformer = new PositiveToString();
Random random = new Random();
int max = random.nextInt(1024) + 10;
List<Integer> testData = new ArrayList<Integer>(max);
List<String> expected = new ArrayList<String>(max);
for (int i = 0; i < max; i++) {
int e = random.nextInt();
testData.add(e);
if (e >= 0) {
expected.add(Integer.toString(e));
}
}
Transform<Integer,String> c = new Transform<Integer,String>(testData,
transformer);
List<String> results = new ArrayList<String>(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<Number> testData = new ArrayList<Number>(max);
List<Long> expected = new ArrayList<Long>(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<Long> c = Transform.subclasses(testData, Long.class);
List<Long> results = new ArrayList<Long>(c);
assertEquals(expected, results);
}
}
public void testSingleton() {
Set<Number> set = new HashSet<Number>();
set.add(3);
Collection<Integer> c = Transform.subclasses(set, Integer.class);
for (Integer i : c) {
System.out.println(i);
}
}
}