Skip to content

Commit

Permalink
introduced generics
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Wagner committed Jan 31, 2012
1 parent d53b22b commit 3c43ad9
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 80 deletions.
31 changes: 15 additions & 16 deletions src/org/arabidopsis/interval/FlankingFinder.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.arabidopsis.interval;

import java.util.List;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

Expand All @@ -16,22 +15,22 @@
*
*/

public class FlankingFinder {
private RbTree lows;
private RbTree highs;
public class FlankingFinder<T> {
private final RbTree lows;
private final RbTree highs;

private Map lowRbNodeToObj;
private Map highRbNodeToObj;
private final Map<RbNode, T> lowRbNodeToObj;
private final Map<RbNode, T> highRbNodeToObj;

public FlankingFinder() {
this.lows = new RbTree();
this.highs = new RbTree();
this.lowRbNodeToObj = new WeakHashMap();
this.highRbNodeToObj = new WeakHashMap();
this.lowRbNodeToObj = new WeakHashMap<RbNode, T>();
this.highRbNodeToObj = new WeakHashMap<RbNode, T>();
}


public void add(Object obj, int low, int high) {
public void add(T obj, int low, int high) {
RbNode lowNode = new RbNode(low);
RbNode highNode = new RbNode(high);
this.lows.insert(lowNode);
Expand All @@ -43,9 +42,9 @@ public void add(Object obj, int low, int high) {



public List flankingLeft(int pos, int n) {
public List<T> flankingLeft(int pos, int n) {
if (this.highs.root.isNull())
return new ArrayList();
return new ArrayList<T>();

RbNode node = this.highs.root;
RbNode lastNode = node;
Expand All @@ -63,7 +62,7 @@ public List flankingLeft(int pos, int n) {
lastNode = this.highs.predecessor(lastNode);
}

List results = new ArrayList();
List<T> results = new ArrayList<T>();
for (int i = 0; i < n && lastNode != RbNode.NIL; i++) {
results.add(highRbNodeToObj.get(lastNode));
lastNode = this.highs.predecessor(lastNode);
Expand All @@ -74,9 +73,9 @@ public List flankingLeft(int pos, int n) {



public List flankingRight(int pos, int n) {
public List<T> flankingRight(int pos, int n) {
if (this.lows.root.isNull())
return new ArrayList();
return new ArrayList<T>();

RbNode node = this.lows.root;
RbNode lastNode = node;
Expand All @@ -94,7 +93,7 @@ public List flankingRight(int pos, int n) {
lastNode = this.lows.successor(lastNode);
}

List results = new ArrayList();
List<T> results = new ArrayList<T>();
for (int i = 0; i < n && lastNode != RbNode.NIL; i++) {
results.add(lowRbNodeToObj.get(lastNode));
lastNode = this.lows.successor(lastNode);
Expand Down
5 changes: 2 additions & 3 deletions src/org/arabidopsis/interval/Interval.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.arabidopsis.interval;

// Quick and dirty interval class
public class Interval implements Comparable {
public class Interval implements Comparable<Interval> {
private final int low;
private final int high;

Expand Down Expand Up @@ -29,8 +29,7 @@ public int hashCode() {
}


public int compareTo(Object o) {
Interval other = (Interval) o;
public int compareTo(final Interval other) {
if (this.low < other.low)
return -1;
if (this.low > other.low)
Expand Down
39 changes: 20 additions & 19 deletions src/org/arabidopsis/interval/IntervalTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
*/


import java.util.WeakHashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.apache.log4j.Logger;

public class IntervalTree {
private StatisticUpdate updater;
private RbTree tree;
private final StatisticUpdate updater;
private final RbTree tree;

private Map intervals;
private Map max;
private Map min;
private final Map<RbNode, Interval> intervals;
private final Map<RbNode, Integer> max;
private final Map<RbNode, Integer> min;

private Logger logger;
private final Logger logger;


public IntervalTree() {
Expand All @@ -28,12 +29,12 @@ public IntervalTree() {

this.logger = Logger.getLogger(this.getClass());

this.intervals = new WeakHashMap();
this.intervals = new WeakHashMap<RbNode, Interval>();
this.intervals.put(RbNode.NIL, null);

this.max = new WeakHashMap();
this.max = new WeakHashMap<RbNode, Integer>();
this.max.put(RbNode.NIL, new Integer(Integer.MIN_VALUE));
this.min = new WeakHashMap();
this.min = new WeakHashMap<RbNode, Integer>();
this.min.put(RbNode.NIL, new Integer(Integer.MAX_VALUE));
}

Expand Down Expand Up @@ -93,22 +94,22 @@ private boolean canOverlapOnRightSide(Interval interval,


// Returns all matches as a list of Intervals
public List searchAll(Interval interval) {
public List<Interval> searchAll(Interval interval) {
logger.debug("Starting search for " + interval);

if (tree.root().isNull()) {
return new ArrayList();
return new ArrayList<Interval>();
}
return this._searchAll(interval, tree.root());
}


private List _searchAll(Interval interval, RbNode node) {
private List<Interval> _searchAll(Interval interval, RbNode node) {
assert (! node.isNull());

logger.debug("Looking at " + getInterval(node));

List results = new ArrayList();
List<Interval> results = new ArrayList<Interval>();
if (getInterval(node).overlaps(interval)) {
results.add(getInterval(node));
logger.debug("match");
Expand Down Expand Up @@ -136,15 +137,15 @@ public Interval getInterval(RbNode node) {

assert (this.intervals.containsKey(node));

return (Interval) this.intervals.get(node);
return this.intervals.get(node);
}


public int getMax(RbNode node) {
assert (node != null);
assert (this.intervals.containsKey(node));

return ((Integer) this.max.get(node)).intValue();
return (this.max.get(node)).intValue();
}


Expand All @@ -157,7 +158,7 @@ public int getMin(RbNode node) {
assert (node != null);
assert (this.intervals.containsKey(node));

return ((Integer) this.min.get(node)).intValue();
return (this.min.get(node)).intValue();
}


Expand Down
10 changes: 5 additions & 5 deletions src/org/arabidopsis/interval/OrderStatisticTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@


public class OrderStatisticTree {
private RbTree tree;
private Map sizes; // RbNode -> size
private OrderStatisticUpdate updater;
private final RbTree tree;
private final Map<RbNode, Integer> sizes; // RbNode -> size
private final OrderStatisticUpdate updater;

public OrderStatisticTree() {
this.sizes = new WeakHashMap();
this.sizes = new WeakHashMap<RbNode, Integer>();
this.sizes.put(RbNode.NIL, new Integer(0));
this.updater = new OrderStatisticUpdate();
this.tree = new RbTree(this.updater);
Expand Down Expand Up @@ -43,7 +43,7 @@ private RbNode _select(RbNode node, int i) {
// Returns the size of a node.
public int size(RbNode node) {
assert this.sizes.containsKey(node);
return ((Integer) this.sizes.get(node)).intValue();
return (this.sizes.get(node)).intValue();
}


Expand Down
33 changes: 17 additions & 16 deletions src/org/arabidopsis/interval/TestAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
* @see
*/

import junit.runner.TestCollector;
import junit.runner.LoadingTestCollector;
import junit.framework.TestSuite;
import junit.framework.TestCase;

import java.util.List;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.runner.LoadingTestCollector;
import junit.runner.TestCollector;


public class TestAll extends TestCase {
Expand All @@ -36,9 +36,9 @@ public TestAll(String name) {
public static TestSuite suite() throws Exception {
TestSuite suite = new TestSuite();

List allTestClasses = getAllTestClasses();
List<Class<? extends TestCase>> allTestClasses = getAllTestClasses();
for(int i = 0 ; i < allTestClasses.size(); i++) {
suite.addTestSuite((Class)allTestClasses.get(i));
suite.addTestSuite(allTestClasses.get(i));
}

return suite;
Expand All @@ -57,13 +57,14 @@ public static TestSuite suite() throws Exception {
* @exception
* @see
*/
private static List getAllTestClasses() throws ClassNotFoundException {
TestCollector collector = new LoadingTestCollector();
java.util.Enumeration e = collector.collectTests();
List testClasses = new ArrayList();
private static List<Class<? extends TestCase>> getAllTestClasses() throws ClassNotFoundException {
final TestCollector collector = new LoadingTestCollector();
@SuppressWarnings("unchecked")
final java.util.Enumeration<String> e = collector.collectTests();
final List<Class<? extends TestCase>> testClasses = new ArrayList<Class<? extends TestCase>>();
while (e.hasMoreElements()) {
String className = (String) e.nextElement();
Class c = Class.forName(className);
final String className = e.nextElement();
final Class<? extends TestCase> c = Class.forName(className).asSubclass(TestCase.class);
// don't add ourself
if (isSelfClass(c) || isClassNotInMyPackage(c)) {
continue;
Expand All @@ -75,14 +76,14 @@ private static List getAllTestClasses() throws ClassNotFoundException {


// Returns true if the given class is ourself.
private static boolean isSelfClass(Class c) {
private static boolean isSelfClass(Class<?> c) {
return c.equals(TestAll.class);
}


// Returns true if the class c is not within the same package
// hierarchy as ourselves.
private static boolean isClassNotInMyPackage(Class c) {
private static boolean isClassNotInMyPackage(Class<?> c) {
return (! c.getPackage().getName().startsWith
(TestAll.class.getPackage().getName()));
}
Expand Down
20 changes: 10 additions & 10 deletions src/org/arabidopsis/interval/TestFlankingFinder.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
package org.arabidopsis.interval;

import junit.framework.TestCase;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import junit.framework.TestCase;


public class TestFlankingFinder extends TestCase {
private FlankingFinder finder;
private FlankingFinder<String> finder;


public void setUp() {
this.finder = new FlankingFinder();
this.finder = new FlankingFinder<String>();
}


public void testEmptyCase() {
assertEquals(new ArrayList(),
assertEquals(new ArrayList<String>(),
this.finder.flankingLeft(42, 1));
assertEquals(new ArrayList(),
assertEquals(new ArrayList<String>(),
this.finder.flankingRight(42, 1));
}


public void testNoFinding() {
this.finder.add("hello", 10, 20);
assertEquals(new ArrayList(),
assertEquals(new ArrayList<String>(),
this.finder.flankingLeft(9, 1));
assertEquals(new ArrayList(),
assertEquals(new ArrayList<String>(),
this.finder.flankingRight(21, 1));
}


public void testSimpleFinding() {
this.finder.add("hello", 10, 20);
List expected = new ArrayList();
List<String> expected = new ArrayList<String>();
expected.add("hello");
assertEquals(expected,
this.finder.flankingLeft(21, 1));
Expand Down
Loading

0 comments on commit 3c43ad9

Please sign in to comment.