Re: Generics syntax and Comparing Comparables of type ?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Piotr Kobzda schreef:
Hendrik Maryns wrote:
You will have to make RangedKey a
top-level class for this to work.
Not necessarily. Having it nested, you can import it, or use its
qualified name (RangedMap.RangedKey).
I do not really understand why.
Because a class body defines a namespace separated from a header of a
class declaration (i.e. modifiers, name, type parameters, etc.).
Ah, thanks.
Indeed, the following works as well:
/**
* 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<RangedMap.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 static 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;
}
}
}
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
iD8DBQFGlkmge+7xMGD3itQRAv8aAJsHURJ2p+sl90LWRn/bdFPq5N3AaACfZhHw
ipnThRxo2PZRO1tA3Vt1RjY=
=l4d2
-----END PGP SIGNATURE-----