01: package prefuse.util.collections;
02:
03: import java.util.Comparator;
04: import java.util.Date;
05:
06: import prefuse.data.DataTypeException;
07:
08: /**
09: * Factory class that generates the appropriate IntSortedMap implementation
10: * given a key data type.
11: *
12: * @author <a href="http://jheer.org">jeffrey heer</a>
13: */
14: public class SortedMapFactory {
15:
16: public static IntSortedMap getMap(Class type, Comparator cmp,
17: boolean unique) throws IncompatibleComparatorException {
18: if (!comparatorCheck(type, cmp)) {
19: throw new IncompatibleComparatorException();
20: }
21:
22: if (type.equals(int.class) || type.equals(byte.class)) {
23: return new IntIntTreeMap((LiteralComparator) cmp, !unique);
24: } else if (type.equals(long.class)
25: || type.isAssignableFrom(Date.class)) {
26: return new LongIntTreeMap((LiteralComparator) cmp, !unique);
27: } else if (type.equals(float.class)) {
28: return new FloatIntTreeMap((LiteralComparator) cmp, !unique);
29: } else if (type.equals(double.class)) {
30: return new DoubleIntTreeMap((LiteralComparator) cmp,
31: !unique);
32: } else if (type.equals(boolean.class)) {
33: return new BooleanIntBitSetMap();
34: } else if (Object.class.isAssignableFrom(type)) {
35: return new ObjectIntTreeMap(cmp, !unique);
36: } else {
37: throw new DataTypeException(
38: "No map available for the provided type");
39: }
40: }
41:
42: public static boolean comparatorCheck(Class type, Comparator cmp) {
43: if (cmp == null) {
44: return true;
45: } else if (type.equals(int.class)) {
46: if (!(cmp instanceof LiteralIterator))
47: return false;
48: try {
49: ((LiteralComparator) cmp).compare(0, 0);
50: return true;
51: } catch (Exception e) {
52: return false;
53: }
54: } else if (type.equals(long.class)) {
55: if (!(cmp instanceof LiteralIterator))
56: return false;
57: try {
58: ((LiteralComparator) cmp).compare(0L, 0L);
59: return true;
60: } catch (Exception e) {
61: return false;
62: }
63: } else if (type.equals(float.class)) {
64: if (!(cmp instanceof LiteralIterator))
65: return false;
66: try {
67: ((LiteralComparator) cmp).compare(0.f, 0.f);
68: return true;
69: } catch (Exception e) {
70: return false;
71: }
72: } else if (type.equals(double.class)) {
73: if (!(cmp instanceof LiteralIterator))
74: return false;
75: try {
76: ((LiteralComparator) cmp).compare(0.0, 0.0);
77: return true;
78: } catch (Exception e) {
79: return false;
80: }
81: } else if (type.equals(boolean.class)) {
82: if (!(cmp instanceof LiteralIterator))
83: return false;
84: try {
85: ((LiteralComparator) cmp).compare(false, false);
86: return true;
87: } catch (Exception e) {
88: return false;
89: }
90: } else if (Object.class.isAssignableFrom(type)) {
91: return true;
92: } else {
93: throw new DataTypeException(
94: "No comparator available for the provided type");
95: }
96: }
97:
98: } // end of class SortedMapFactory
|