1 package prefuse.util.collections; 2 3 import java.util.Comparator ; 4 import java.util.Date ; 5 6 import prefuse.data.DataTypeException; 7 8 9 15 public class SortedMapFactory { 16 17 public static IntSortedMap getMap( 18 Class type, Comparator cmp, boolean unique) 19 throws IncompatibleComparatorException 20 { 21 if ( !comparatorCheck(type, cmp) ) { 22 throw new IncompatibleComparatorException(); 23 } 24 25 if ( type.equals(int.class) || type.equals(byte.class) ) 26 { 27 return new IntIntTreeMap((LiteralComparator)cmp, !unique); 28 } 29 else if ( type.equals(long.class) || type.isAssignableFrom(Date .class) ) 30 { 31 return new LongIntTreeMap((LiteralComparator)cmp, !unique); 32 } 33 else if ( type.equals(float.class) ) 34 { 35 return new FloatIntTreeMap((LiteralComparator)cmp, !unique); 36 } 37 else if ( type.equals(double.class) ) 38 { 39 return new DoubleIntTreeMap((LiteralComparator)cmp, !unique); 40 } 41 else if ( type.equals(boolean.class) ) 42 { 43 return new BooleanIntBitSetMap(); 44 } 45 else if ( Object .class.isAssignableFrom(type) ) 46 { 47 return new ObjectIntTreeMap(cmp, !unique); 48 } 49 else { 50 throw new DataTypeException( 51 "No map available for the provided type"); 52 } 53 } 54 55 public static boolean comparatorCheck(Class type, Comparator cmp) { 56 if ( cmp == null ) 57 { 58 return true; 59 } 60 else if ( type.equals(int.class) ) 61 { 62 if ( !(cmp instanceof LiteralIterator) ) 63 return false; 64 try { 65 ((LiteralComparator)cmp).compare(0,0); 66 return true; 67 } catch ( Exception e ) { 68 return false; 69 } 70 } 71 else if ( type.equals(long.class) ) 72 { 73 if ( !(cmp instanceof LiteralIterator) ) 74 return false; 75 try { 76 ((LiteralComparator)cmp).compare(0L,0L); 77 return true; 78 } catch ( Exception e ) { 79 return false; 80 } 81 } 82 else if ( type.equals(float.class) ) 83 { 84 if ( !(cmp instanceof LiteralIterator) ) 85 return false; 86 try { 87 ((LiteralComparator)cmp).compare(0.f,0.f); 88 return true; 89 } catch ( Exception e ) { 90 return false; 91 } 92 } 93 else if ( type.equals(double.class) ) 94 { 95 if ( !(cmp instanceof LiteralIterator) ) 96 return false; 97 try { 98 ((LiteralComparator)cmp).compare(0.0,0.0); 99 return true; 100 } catch ( Exception e ) { 101 return false; 102 } 103 } 104 else if ( type.equals(boolean.class) ) 105 { 106 if ( !(cmp instanceof LiteralIterator) ) 107 return false; 108 try { 109 ((LiteralComparator)cmp).compare(false,false); 110 return true; 111 } catch ( Exception e ) { 112 return false; 113 } 114 } 115 else if ( Object .class.isAssignableFrom(type) ) 116 { 117 return true; 118 } 119 else { 120 throw new DataTypeException( 121 "No comparator available for the provided type"); 122 } 123 } 124 125 } | Popular Tags |