1 2 3 package org.quilt.reg; 4 5 import java.util.*; 6 7 17 18 public class Registry { 19 20 22 private SortedMap registry = null; 24 25 private class CmpArrays implements Comparator { 26 40 public int compare (final Object o1, final Object o2) { 41 String str1[] = (String []) o1; 42 String str2[] = (String []) o2; 43 int depth1 = str1.length; int depth2 = str2.length; 45 int depth = (depth1 < depth2) ? depth1 : depth2; 46 47 int i; 48 for (i = 0; i < depth ; i++) { 49 int rel = str1[i].compareTo(str2[i]); 50 if ( rel < 0 ) { 51 return -1; 52 } else if ( rel > 0 ) { 53 return 1; 54 } 55 } 57 if (depth1 < depth2) { 59 return -1; } else if (depth1 > depth2) { 61 return 1; } 63 return 0; } 66 } 67 68 69 public Registry() { 70 registry = new TreeMap(new CmpArrays() ); 71 registry.clear(); } 73 74 76 77 final public void clear () { 78 registry.clear(); 79 } 80 81 final Comparator comparator() { 82 return new CmpArrays(); 83 } 84 85 final public boolean containsKey(String [] key) { 86 if (key == null) { 87 throw new IllegalArgumentException ("null key"); 88 } 89 return registry.containsKey(key); 90 } 91 97 final public Object get (String [] key) { 98 if (key == null) { 99 throw new IllegalArgumentException ("null key"); 100 } 101 if ( registry.containsKey (key) ) { 102 return registry.get (key); 103 } else { 104 return null; 105 } 106 } 107 108 final public boolean isEmpty() { 109 return registry.isEmpty(); 110 } 111 116 public Set keySet() { 117 return registry.keySet(); 118 } 119 126 final public Object put (String [] key, Object value) { 127 if (key == null) { 128 throw new IllegalArgumentException ("null key"); 129 } 130 String keyCopy[] = new String [ key.length ]; 132 for (int i = 0; i < key.length; i++) { 133 keyCopy[i] = key[i]; 134 } 135 return registry.put (keyCopy, value); 136 } 137 138 144 final public Object remove (String [] key) { 145 if (key == null) { 146 throw new IllegalArgumentException ("null key"); 147 } 148 return registry.remove(key); 149 } 150 151 final public int size () { 152 return registry.size(); 153 } 154 159 final public String [] splitClassName( String name ) { 160 return name.split("."); 161 } 162 } 163 | Popular Tags |