1 /* Registry.java */
2
3 package org.quilt.reg;
4
5 import java.util.*;
6
7 /***
8 * <p>A registry for storing Quilt run-time options. The registry
9 * stores key-value pairs, where the key takes the form of an array
10 * of Strings. The registry is sorted, but XXX there is currently
11 * no support for sequential access or partial-key access.</p>
12 *
13 * <p>This class is not thread-safe.</p>
14 *
15 * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a>
16 */
17
18 public class Registry {
19
20 // PRIVATE VARIABLES, CLASS, AND CONSTRUCTOR ////////////////////
21
22 private SortedMap registry = null; // key = String[], value = Object
23
24 /*** Comparator for String arrays. */
25 private class CmpArrays implements Comparator {
26 /***
27 * Compare String arrays. The arrays are compared element
28 * by element starting at [0]. If any such pair differ,
29 * then the result of the String comparison is returned.
30 * If they match up to and including the last element of the
31 * shorter array of the two, then the shorter is deemed to sort
32 * before the longer. The two arrays are equal if and only if
33 * they have the same number of elements and each pair of
34 * elements is equal.
35 *
36 * @param o1 First String array.
37 * @param o2 Second.
38 * @return -1 if o1 < o2, 0 if o1 == o2, 1 if o1 > o2
39 */
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; // number of elements in array
44 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 // otherwise equal, so continue
56 }
57 // the first n == depth strings are the same
58 if (depth1 < depth2) {
59 return -1; // shorter
60 } else if (depth1 > depth2) {
61 return 1; // longer
62 }
63 // depth1 == depth2
64 return 0; // a perfect match
65 }
66 }
67
68 /*** No-arg constructor. */
69 public Registry() {
70 registry = new TreeMap(new CmpArrays() );
71 registry.clear(); // should not be necessary, doesn't solve problem
72 }
73
74 // OTHER METHODS ////////////////////////////////////////////////
75
76 /*** Remove all elements from the registry. */
77 final public void clear () {
78 registry.clear();
79 }
80 /*** Return comparator used in sorting keys. */
81 final Comparator comparator() {
82 return new CmpArrays();
83 }
84 /*** @return True if there is an item under this key in the registry. */
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 /***
92 * Get an object from the registry.
93 *
94 * @param key Array of strings, the unique key.
95 * @return value Object stored under the key, or null if none.
96 */
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 /*** @return True if there are no elements in the registry. */
108 final public boolean isEmpty() {
109 return registry.isEmpty();
110 }
111 /***
112 * XXX An overly powerful method.
113 * @todo Replace with first/next/last access.
114 * @return The (editable) set of keys.
115 */
116 public Set keySet() {
117 return registry.keySet();
118 }
119 /***
120 * Add an object to the registry, overwriting any existing value.
121 *
122 * @param key Array of Strings, the unique key. Must not be null.
123 * @param value Object to be stored under this key.
124 * @return Old value if there was one, null otherwise.
125 */
126 final public Object put (String [] key, Object value) {
127 if (key == null) {
128 throw new IllegalArgumentException ("null key");
129 }
130 // make a copy of the key
131 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 /***
139 * Remove an item from the registry.
140 *
141 * @param key Array of Strings.
142 * @return The value associated with the key, or null if none.
143 */
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 /*** @return Number of items in the registry. */
151 final public int size () {
152 return registry.size();
153 }
154 /***
155 * Convert a class or method name into a String array.
156 *
157 * @return A string array containing the fields of the name.
158 */
159 final public String[] splitClassName( String name ) {
160 return name.split(".");
161 }
162 }
This page was automatically generated by Maven