1 // $Id: Key.java,v 1.1.1.1 2002/09/29 17:26:06 powerpete Exp $
2 package de.jface.util;
3
4 import java.util.Iterator;
5
6 /***
7 * Class <code>Key</code>
8 * of project RemotePostIt.
9 *
10 * @author Moritz Petersen
11 * @version $Revision: 1.1.1.1 $
12 */
13 public class Key implements Comparable
14 {
15 private SortedList children = new SortedList();
16 private String value;
17 private final String name;
18 private final Key parent;
19
20 Key(String name)
21 {
22 this(name, null);
23 }
24
25 /***
26 * Creates a key without parent. The instances
27 * created with this constructor are used to compare
28 * with the instances in the children list, to find
29 * an existing key.
30 *
31 * @param name The name of the new key.
32 */
33 private Key(String name, Key parent)
34 {
35 if (!isValidName(name))
36 {
37 throw new IllegalArgumentException("Illegal name: " + name);
38 }
39
40 this.name = name;
41 this.parent = parent;
42 }
43
44 private static final boolean isValidName(String name)
45 {
46 // name must not contain whitespace
47 // maybe some other xml specific stuff.
48 // TODO: add that stuff later.
49 for (int i = 0, max = name.length(); i < max; i++)
50 {
51 if (Character.isWhitespace(name.charAt(i)))
52 {
53 // break immediately if the first whitespace
54 // character is found.
55 return false;
56 }
57 }
58
59 return true;
60 }
61
62 /***
63 * Returns the child-key with the given name.
64 *
65 * @param name The name of the child key
66 * @return The child key.
67 */
68
69 // NOTE: If the key has not been created
70 // before, it will be created now. Otherwise
71 // the instance of the existing key will be returned.
72 public Key getKey(String name)
73 {
74 return (Key) children.get(new Key(name, this));
75 }
76
77 String getName()
78 {
79 return name;
80 }
81
82 SortedList getChildren()
83 {
84 return children;
85 }
86
87 /***
88 * Removes this key from its parent's list. This operation fails, if the
89 * parent is <code>null</code>.
90 *
91 * @return <code>true<code>, if the operation was successful, otherwise
92 * </code>false</code> is returned.
93 */
94 boolean remove()
95 {
96 if (parent == null)
97 {
98 return false;
99 }
100
101 parent.children.remove(this);
102
103 if ((parent.value == null) && (parent.children.size() == 0))
104 {
105 parent.remove();
106 }
107
108 return true;
109 }
110
111 void setValue(String value)
112 {
113 this.value = value;
114 }
115
116 String getValue()
117 {
118 return value;
119 }
120
121 public boolean equals(Object o)
122 {
123 Key k = (Key) o;
124
125 return name.equals(k.name);
126 }
127
128 public int hashCode()
129 {
130 return name.hashCode();
131 }
132
133 boolean contains(Key key)
134 {
135 if (name.equals(key.name))
136 {
137 return true;
138 }
139
140 if (children.contains(key))
141 {
142 return true;
143 }
144
145 for (Iterator i = children.iterator(); i.hasNext();)
146 {
147 Key root = (Key) i.next();
148
149 if (root.contains(key))
150 {
151 return true;
152 }
153 }
154
155 return false;
156 }
157
158 public int compareTo(Object o)
159 {
160 Key k = (Key) o;
161
162 return name.compareTo(k.name);
163 }
164
165 /***
166 * Returns the whole key structure, containing the names of the parents
167 * separated by a dot.
168 */
169 public String toString()
170 {
171 StringBuffer sbuf = new StringBuffer(name);
172
173 for (Key key = parent; key != null; key = key.parent)
174 {
175 sbuf.insert(0, ".");
176 sbuf.insert(0, key.name);
177 }
178
179 return sbuf.toString();
180 }
181 }
182
183 // ------1---------2---------3---------4---------5---------6---------7---------8
This page was automatically generated by Maven