MATH-261 - start adding some generics to Frequency

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@768084 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2009-04-23 22:52:10 +00:00
parent e1452ca6ea
commit 2dac0fc426
1 changed files with 14 additions and 13 deletions

View File

@ -49,13 +49,13 @@ public class Frequency implements Serializable {
private static final long serialVersionUID = -3845586908418844111L; private static final long serialVersionUID = -3845586908418844111L;
/** underlying collection */ /** underlying collection */
private final TreeMap freqTable; private final TreeMap<Comparable<?>, Long> freqTable;
/** /**
* Default constructor. * Default constructor.
*/ */
public Frequency() { public Frequency() {
freqTable = new TreeMap(); freqTable = new TreeMap<Comparable<?>, Long>();
} }
/** /**
@ -64,7 +64,7 @@ public class Frequency implements Serializable {
* @param comparator Comparator used to order values * @param comparator Comparator used to order values
*/ */
public Frequency(Comparator comparator) { public Frequency(Comparator comparator) {
freqTable = new TreeMap(comparator); freqTable = new TreeMap<Comparable<?>, Long>(comparator);
} }
/** /**
@ -78,7 +78,7 @@ public class Frequency implements Serializable {
NumberFormat nf = NumberFormat.getPercentInstance(); NumberFormat nf = NumberFormat.getPercentInstance();
StringBuffer outBuffer = new StringBuffer(); StringBuffer outBuffer = new StringBuffer();
outBuffer.append("Value \t Freq. \t Pct. \t Cum Pct. \n"); outBuffer.append("Value \t Freq. \t Pct. \t Cum Pct. \n");
Iterator iter = freqTable.keySet().iterator(); Iterator<?> iter = freqTable.keySet().iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
Object value = iter.next(); Object value = iter.next();
outBuffer.append(value); outBuffer.append(value);
@ -124,13 +124,13 @@ public class Frequency implements Serializable {
* @param v the value to add. * @param v the value to add.
* @throws IllegalArgumentException if <code>v</code> is not comparable with previous entries * @throws IllegalArgumentException if <code>v</code> is not comparable with previous entries
*/ */
public void addValue(Comparable<?>v){ public void addValue(Comparable<?> v){
Object obj = v; Comparable<?> obj = v;
if (v instanceof Integer) { if (v instanceof Integer) {
obj = Long.valueOf(((Integer) v).longValue()); obj = Long.valueOf(((Integer) v).longValue());
} }
try { try {
Long count = (Long) freqTable.get(obj); Long count = freqTable.get(obj);
if (count == null) { if (count == null) {
freqTable.put(obj, Long.valueOf(1)); freqTable.put(obj, Long.valueOf(1));
} else { } else {
@ -192,7 +192,7 @@ public class Frequency implements Serializable {
* *
* @return values Iterator * @return values Iterator
*/ */
public Iterator valuesIterator() { public Iterator<Comparable<?>> valuesIterator() {
return freqTable.keySet().iterator(); return freqTable.keySet().iterator();
} }
@ -205,7 +205,7 @@ public class Frequency implements Serializable {
*/ */
public long getSumFreq() { public long getSumFreq() {
long result = 0; long result = 0;
Iterator iterator = freqTable.values().iterator(); Iterator<?> iterator = freqTable.values().iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
result += ((Long) iterator.next()).longValue(); result += ((Long) iterator.next()).longValue();
} }
@ -225,7 +225,7 @@ public class Frequency implements Serializable {
} }
long result = 0; long result = 0;
try { try {
Long count = (Long) freqTable.get(v); Long count = freqTable.get(v);
if (count != null) { if (count != null) {
result = count.longValue(); result = count.longValue();
} }
@ -341,7 +341,7 @@ public class Frequency implements Serializable {
long result = 0; long result = 0;
try { try {
Long value = (Long) freqTable.get(v); Long value = freqTable.get(v);
if (value != null) { if (value != null) {
result = value.longValue(); result = value.longValue();
} }
@ -357,7 +357,7 @@ public class Frequency implements Serializable {
return getSumFreq(); // v is comparable, but greater than the last value return getSumFreq(); // v is comparable, but greater than the last value
} }
Iterator values = valuesIterator(); Iterator<?> values = valuesIterator();
while (values.hasNext()) { while (values.hasNext()) {
Object nextValue = values.next(); Object nextValue = values.next();
if (c.compare(v, nextValue) > 0) { if (c.compare(v, nextValue) > 0) {
@ -487,8 +487,9 @@ public class Frequency implements Serializable {
* @throws ClassCastException when <i>o1</i> is not a {@link Comparable Comparable}, * @throws ClassCastException when <i>o1</i> is not a {@link Comparable Comparable},
* or when <code>((Comparable)o1).compareTo(o2)</code> does * or when <code>((Comparable)o1).compareTo(o2)</code> does
*/ */
@SuppressWarnings("unchecked") // See Javadoc, ClassCast is expected
public int compare(Object o1, Object o2) { public int compare(Object o1, Object o2) {
return ((Comparable)o1).compareTo(o2); return ((Comparable<Object>)o1).compareTo(o2);
} }
} }
} }