48 |
import java.util.Map; |
import java.util.Map; |
49 |
import java.util.Set; |
import java.util.Set; |
50 |
|
|
51 |
|
|
52 |
/** |
/** |
|
* ActionMap |
|
53 |
* @author Andrew Selkirk |
* @author Andrew Selkirk |
54 |
* @version 1.0 |
* @author Michael Koch |
55 |
*/ |
*/ |
56 |
public class ActionMap implements Serializable |
public class ActionMap |
57 |
|
implements Serializable |
58 |
{ |
{ |
59 |
static final long serialVersionUID = -6277518704513986346L; |
private static final long serialVersionUID = -6277518704513986346L; |
|
|
|
|
//------------------------------------------------------------- |
|
|
// Variables -------------------------------------------------- |
|
|
//------------------------------------------------------------- |
|
60 |
|
|
61 |
/** |
/** |
62 |
* actionMap |
* actionMap |
66 |
/** |
/** |
67 |
* parent |
* parent |
68 |
*/ |
*/ |
69 |
private ActionMap parent = null; |
private ActionMap parent; |
|
|
|
|
|
|
|
//------------------------------------------------------------- |
|
|
// Initialization --------------------------------------------- |
|
|
//------------------------------------------------------------- |
|
70 |
|
|
71 |
/** |
/** |
72 |
* Constructor ActionMap |
* Creates a new <code>ActionMap</code> instance. |
73 |
*/ |
*/ |
74 |
public ActionMap() { |
public ActionMap() |
75 |
} // ActionMap() |
{ |
76 |
|
} |
|
|
|
|
//------------------------------------------------------------- |
|
|
// Methods ---------------------------------------------------- |
|
|
//------------------------------------------------------------- |
|
77 |
|
|
78 |
/** |
/** |
79 |
* get |
* Returns an action associated with an object. |
80 |
* @param key TODO |
* |
81 |
* @returns Action |
* @param key the key of the enty |
82 |
|
* |
83 |
|
* @return the action associated with key, may be null |
84 |
*/ |
*/ |
85 |
public Action get(Object key) { |
public Action get(Object key) |
86 |
|
{ |
87 |
|
Object result = actionMap.get(key); |
88 |
|
|
89 |
// Variables |
if (result == null) |
|
Object result; |
|
|
|
|
|
// Check Local store |
|
|
result = actionMap.get(key); |
|
|
|
|
|
// Check Parent |
|
|
if (result == null) { |
|
90 |
result = parent.get(key); |
result = parent.get(key); |
|
} // if |
|
91 |
|
|
92 |
return (Action) result; |
return (Action) result; |
93 |
|
} |
|
} // get() |
|
94 |
|
|
95 |
/** |
/** |
96 |
* put |
* Puts a new <code>Action</code> into the <code>ActionMap</code>. |
97 |
* @param key TODO |
* If action is null an existing entry will be removed. |
98 |
* @param action TODO |
* |
99 |
*/ |
* @param key the key for the entry |
100 |
public void put(Object key, Action action) { |
* @param action the action. |
101 |
if (action == null) { |
*/ |
102 |
|
public void put(Object key, Action action) |
103 |
|
{ |
104 |
|
if (action == null) |
105 |
actionMap.remove(key); |
actionMap.remove(key); |
106 |
} else { |
else |
107 |
actionMap.put(key, action); |
actionMap.put(key, action); |
108 |
} // if |
} |
|
} // put() |
|
109 |
|
|
110 |
/** |
/** |
111 |
* remove |
* Remove an entry from the <code>ActionMap</code>. |
112 |
* @param key TODO |
* |
113 |
|
* @param key the key of the entry to remove |
114 |
*/ |
*/ |
115 |
public void remove(Object key) { |
public void remove(Object key) |
116 |
|
{ |
117 |
actionMap.remove(key); |
actionMap.remove(key); |
118 |
} // remove() |
} |
119 |
|
|
120 |
/** |
/** |
121 |
* getParent |
* Returns the parent of this <code>ActionMap</code>. |
122 |
* @returns ActionMap |
* |
123 |
|
* @return the parent, may be null. |
124 |
*/ |
*/ |
125 |
public ActionMap getParent() { |
public ActionMap getParent() |
126 |
|
{ |
127 |
return parent; |
return parent; |
128 |
} // getParent() |
} |
129 |
|
|
130 |
/** |
/** |
131 |
* setParent |
* Sets a parent for this <code>ActionMap</code>. |
132 |
* @param parentMap TODO |
* |
133 |
|
* @param parentMap the new parent |
134 |
*/ |
*/ |
135 |
public void setParent(ActionMap parentMap) { |
public void setParent(ActionMap parentMap) |
136 |
|
{ |
137 |
parent = parentMap; |
parent = parentMap; |
138 |
} // setParent() |
} |
139 |
|
|
140 |
/** |
/** |
141 |
* size |
* Returns the number of entries in this <code>ActionMap</code>. |
142 |
* @returns int |
* |
143 |
|
* @return the number of entries |
144 |
*/ |
*/ |
145 |
public int size() { |
public int size() |
146 |
|
{ |
147 |
return actionMap.size(); |
return actionMap.size(); |
148 |
} // size() |
} |
149 |
|
|
150 |
/** |
/** |
151 |
* clear |
* Clears the <code>ActionMap</code>. |
152 |
*/ |
*/ |
153 |
public void clear() { |
public void clear() |
154 |
|
{ |
155 |
actionMap.clear(); |
actionMap.clear(); |
156 |
} // clear() |
} |
157 |
|
|
158 |
/** |
/** |
159 |
* keys |
* Returns all keys of entries in this <code>ActionMap</code>. |
160 |
* @returns Object[] |
* |
161 |
|
* @return an array of keys |
162 |
*/ |
*/ |
163 |
public Object[] keys() { |
public Object[] keys() |
164 |
return convertSet(actionMap.keySet()); |
{ |
165 |
} // keys() |
return actionMap.keySet().toArray(); |
166 |
|
} |
167 |
|
|
168 |
/** |
/** |
169 |
* allKeys |
* Returns all keys of entries in this <code>ActionMap</code> |
170 |
* @returns Object[] |
* and all its parents. |
171 |
|
* |
172 |
|
* @return an array of keys |
173 |
*/ |
*/ |
174 |
public Object[] allKeys() { |
public Object[] allKeys() |
175 |
|
{ |
176 |
// Variables |
Set set = new HashSet(); |
|
Set set; |
|
|
|
|
|
// Initialize |
|
|
set = new HashSet(); |
|
177 |
|
|
178 |
// Get Key Sets |
if (parent != null) |
|
if (parent != null) { |
|
179 |
set.addAll(Arrays.asList(parent.allKeys())); |
set.addAll(Arrays.asList(parent.allKeys())); |
|
} // if |
|
|
set.addAll(actionMap.keySet()); |
|
180 |
|
|
181 |
return convertSet(set); |
set.addAll(actionMap.keySet()); |
182 |
|
return set.toArray(); |
183 |
} // allKeys() |
} |
|
|
|
|
private Object[] convertSet(Set set) { |
|
|
|
|
|
// Variables |
|
|
int index; |
|
|
Iterator iterator; |
|
|
Object[] keys; |
|
|
|
|
|
// Create Final array |
|
|
keys = new Object[set.size()]; |
|
|
iterator = set.iterator(); |
|
|
index = 0; |
|
|
while (iterator.hasNext()) { |
|
|
keys[index++] = iterator.next(); |
|
|
} // while |
|
|
|
|
|
return keys; |
|
|
|
|
|
} // convertSet() |
|
|
|
|
|
|
|
|
//------------------------------------------------------------- |
|
|
// Interface: Serializable ------------------------------------ |
|
|
//------------------------------------------------------------- |
|
184 |
|
|
185 |
/** |
/** |
186 |
* writeObject |
* writeObject |
187 |
* @param stream TODO |
* |
188 |
* @exception IOException TODO |
* @param stream the stream to write to |
189 |
*/ |
* |
190 |
private void writeObject(ObjectOutputStream value0) throws IOException { |
* @exception IOException If an error occurs |
191 |
|
*/ |
192 |
|
private void writeObject(ObjectOutputStream stream) |
193 |
|
throws IOException |
194 |
|
{ |
195 |
// TODO |
// TODO |
196 |
} // writeObject() |
} |
197 |
|
|
198 |
/** |
/** |
199 |
* readObject |
* readObject |
200 |
* @param stream TODO |
* |
201 |
* @exception ClassNotFoundException TODO |
* @param stream the stream to read from |
202 |
* @exception IOException TODO |
* |
203 |
*/ |
* @exception ClassNotFoundException If the serialized class cannot be found |
204 |
private void readObject(ObjectInputStream value0) throws ClassNotFoundException, IOException { |
* @exception IOException If an error occurs |
205 |
|
*/ |
206 |
|
private void readObject(ObjectInputStream stream) |
207 |
|
throws ClassNotFoundException, IOException |
208 |
|
{ |
209 |
// TODO |
// TODO |
210 |
} // readObject() |
} |
211 |
|
} |
|
|
|
|
} // ActionMap |
|