add some useful classes

This commit is contained in:
Terence Parr 2012-02-11 17:42:02 -08:00
parent b80b22e4fa
commit 808d23e6d1
3 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package org.antlr.v4.misc;
import java.util.Hashtable;
/** Count how many of each key we have; not thread safe */
public class FrequencySet<T> extends Hashtable<T, MutableInt> {
public int count(T key) {
MutableInt value = get(key);
if (value == null) return 0;
return value.v;
}
public void add(T key) {
MutableInt value = get(key);
if (value == null) {
value = new MutableInt(1);
put(key, value);
}
else {
value.v++;
}
}
}

View File

@ -0,0 +1,17 @@
package org.antlr.v4.misc;
public class MutableInt extends Number implements Comparable<Number> {
public int v;
public MutableInt(int v) { this.v = v; }
@Override public int compareTo(Number o) { return v; }
@Override public int intValue() { return v; }
@Override public long longValue() { return v; }
@Override public float floatValue() { return v; }
@Override public double doubleValue() { return v; }
@Override
public String toString() {
return String.valueOf(v);
}
}

View File

@ -0,0 +1,11 @@
package org.antlr.v4.misc;
public class Pair<A,B> {
public A a;
public B b;
public Pair(A a, B b) {
this.a = a;
this.b = b;
}
}