Re: Generics syntax and Comparing Comparables of type ?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Sideswipe schreef:
I am trying to write a RangedMap and when I "genericify" it, it
occurred to me that I am not completely sure how I can Quite express
the syntax. Admittedly I am learning my generics, but the code is
definitely correct when I remove the generics (run main for a test).
Essentially, I want to be able to apply this Map to any Comparable
object such as Date, Integer, String, whatever. Clearly though, the
comparison must be between comparables of the Same Type.
So, Comparable<?> min and Comparable<?> max could both be Integers,
but 1 can't be Integer and the other Date. Maybe the code will explain
better. Anyone have some input?
Christian
http://christian.bongiorno.org
/**
* Date: Jul 9, 2007
* Time: 4:25:18 PM
* This Map allows for a ranged mapping of values. The key can be
anything comparable such
* as Numbers and Dates or Strings
*/
import java.util.TreeMap;
import java.util.Map;
public class RangedMap<RangedKey, V>
In this statement, you declare RangedKey as a type variable. Thus the
compilers thinks all occurrences of RangedKey in the class are a
variable. You don???t need the variable at all here. However, it indeed
makes sense to make RangeKey generic as well, so then you need it again.
The correct syntax would be either
public class RangedMap<V> extends TreeMap<RangedKey, V> {
or
public class RangedMap<T extends Comparable<T>, V> extends
TreeMap<RangedKey<T>, V> {
But that is not the only problem. You will have to make RangedKey a
top-level class for this to work. I do not really understand why.
The following works:
/**
* Date: Jul 9, 2007
* Time: 4:25:18 PM
* This Map allows for a ranged mapping of values. The key can be
anything comparable such
* as Numbers and Dates or Strings
*/
import java.util.TreeMap;
import java.util.Map;
public class RangedMap<T extends Comparable<T>, V> extends
TreeMap<RangedKey<T>, V> {
public static void main(String[] args) {
Map<RangedKey<Integer>, String> testMap = new RangedMap<Integer,
String>();
testMap.put(new RangedKey<Integer>(50, 60), "Blue");
testMap.put(new RangedKey<Integer>(61, 70), "Yellow");
testMap.put(new RangedKey<Integer>(71, 80), "Red");
System.out.println(testMap.get(new RangedKey<Integer>(55)));
System.out.println(testMap.get(new RangedKey<Integer>(67)));
System.out.println(testMap.get(new RangedKey<Integer>(73)));
System.out.println(testMap.get(new RangedKey<Integer>(1)));
System.out.println(testMap.get(new RangedKey<Integer>(90)));
testMap.put(new RangedKey<Integer>(62, 80), "Fail");
}
@Override
public V put(RangedKey<T> key, V value) {
rangeCheck(key.getMin());
rangeCheck(key.getMax());
return super.put(key, value);
}
@Override
public void putAll(Map<? extends RangedKey<T>, ? extends V> map) {
for (Map.Entry<? extends RangedKey<T>, ? extends V> entry :
map.entrySet())
put(entry.getKey(), entry.getValue());
}
private void rangeCheck(T c) throws IllegalArgumentException {
V val = get(new RangedKey<T>(c));
if (val != null)
throw new IllegalArgumentException("range: " + c + "overlaps
with another range and would cause an ambiguous mapping");
}
}
/**
* A key which represents a range.
*
* @param <T> The comparable class which has ranges.
*/
public final class RangedKey<T extends Comparable<T>> implements
Comparable<RangedKey<T>> {
private T min;
private T max;
public int compareTo(RangedKey<T> range) {
if (this.max.compareTo(range.min) < 0)
return -1;
if (this.min.compareTo(range.max) > 0)
return 1;
else
return 0;
}
public RangedKey(T single) {
this.min = single;
this.max = single;
}
public RangedKey(T min, T max) {
this.min = min;
this.max = max;
}
public T getMin() {
return min;
}
public T getMax() {
return max;
}
@Override
public String toString() {
return min + "-" + max;
}
}
Output:
Blue
Yellow
Red
null
null
Exception in thread "main" java.lang.IllegalArgumentException: range:
62overlaps with another range and would cause an ambiguous mapping
at RangedMap.rangeCheck(RangedMap.java:48)
at RangedMap.put(RangedMap.java:33)
at RangedMap.put(RangedMap.java:1)
at RangedMap.main(RangedMap.java:27)
HTH, H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFGljDBe+7xMGD3itQRAt/YAJ9NomhjD6BXeVwZMZzTfTJPU4a+AACfX4Pp
QfUhCvh6OjkYMJ1fJKv2W9k=
=AEOG
-----END PGP SIGNATURE-----