001: /*
002: * Project: Gulden Utilies
003: * Class: de.gulden.util.OrderedHashMap
004: * Version: snapshot-beautyj-1.1
005: *
006: * Date: 2004-09-29
007: *
008: * This is a snapshot version of the Gulden Utilities,
009: * it is not released as a seperate version.
010: *
011: * Note: Contains auto-generated Javadoc comments created by BeautyJ.
012: *
013: * This is licensed under the GNU Lesser General Public License (LGPL)
014: * and comes with NO WARRANTY.
015: *
016: * Author: Jens Gulden
017: * Email: amoda@jensgulden.de
018: */
019:
020: package de.gulden.util;
021:
022: import de.gulden.util.OrderedHashMap.OrderedHashMapEntry;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.util.*;
027: import java.util.Collection;
028: import java.util.HashMap;
029: import java.util.Map;
030: import java.util.Map.Entry;
031: import java.util.Set;
032:
033: /**
034: * Class OrderedHashMap.
035: *
036: * @author Jens Gulden
037: * @version snapshot-beautyj-1.1
038: */
039: public class OrderedHashMap extends HashMap {
040:
041: // ------------------------------------------------------------------------
042: // --- fields ---
043: // ------------------------------------------------------------------------
044:
045: /**
046: * The key list.
047: */
048: protected ArrayListSet keyList;
049:
050: /**
051: * The value list.
052: */
053: protected ArrayListSet valueList;
054:
055: // ------------------------------------------------------------------------
056: // --- constructor ---
057: // ------------------------------------------------------------------------
058:
059: /**
060: * Creates a new instance of OrderedHashMap.
061: */
062: public OrderedHashMap() {
063: super ();
064: keyList = new ArrayListSet();
065: valueList = new ArrayListSet();
066: }
067:
068: // ------------------------------------------------------------------------
069: // --- methods ---
070: // ------------------------------------------------------------------------
071:
072: public Object clone() {
073: OrderedHashMap cloned = new OrderedHashMap();
074: cloned.putAll(this );
075: return cloned;
076: }
077:
078: public void clear() {
079: super .clear();
080: keyList.clear();
081: valueList.clear();
082: }
083:
084: public Object put(Object key, Object value) {
085: int i = keyList.indexOf(key);
086: if (i != -1) {
087: keyList.remove(i); // remove if already inside and add new at the end
088: valueList.remove(i);
089: }
090: keyList.add(key);
091: valueList.add(value);
092: return super .put(key, value);
093: }
094:
095: public void putAll(Map map) {
096: super .putAll(map);
097: // list must be handled -after- super.putAll()
098: Collection mapKeys = map.keySet();
099: Collection mapValues = map.values();
100: keyList.removeAll(mapKeys);
101: keyList.addAll(mapKeys);
102: valueList.removeAll(mapValues);
103: valueList.addAll(mapValues);
104: }
105:
106: public Object remove(Object key) {
107: int i = keyList.indexOf(key);
108: if (i != -1) {
109: keyList.remove(i);
110: valueList.remove(i);
111: }
112: return super .remove(key);
113: }
114:
115: public Collection values() {
116: return valueList;
117: }
118:
119: public Set keySet() {
120: return keyList;
121: }
122:
123: public Set entrySet() {
124: ArrayListSet set = new ArrayListSet();
125: for (int i = 0; i < keyList.size(); i++) {
126: Object key = keyList.get(i);
127: Object value = valueList.get(i);
128: set.add(new OrderedHashMapEntry(key, value));
129: }
130: return set;
131: }
132:
133: /**
134: *
135: * @throws IOException if an i/o error occurs
136: */
137: public void load(InputStream in) throws IOException {
138: java.io.BufferedReader r = new java.io.BufferedReader(
139: new java.io.InputStreamReader(in));
140: String line = r.readLine();
141: while (line != null) {
142: Properties p = new Properties(); // use Properties' parser to handle \=
143: p.load(new java.io.StringBufferInputStream(line));
144: for (Enumeration e = p.keys(); e.hasMoreElements();) { // should be 0..1
145: String key = (String) e.nextElement();
146: String value = p.getProperty(key);
147: this .put(key, value);
148: }
149: line = r.readLine();
150: }
151: }
152:
153: /**
154: *
155: * @throws IOException if an i/o error occurs
156: */
157: public void save(OutputStream out) throws IOException {
158: for (int i = 0; i < keyList.size(); i++) {
159: Properties p = new Properties();
160: p.put(keyList.get(i), valueList.get(i));
161: p.store(out, "");
162: }
163: }
164:
165: // ************************************************************************
166: // *** inner class ***
167: // ************************************************************************
168:
169: /**
170: * Class OrderedHashMapEntry.
171: *
172: * @author Jens Gulden
173: * @version snapshot-beautyj-1.1
174: */
175: public class OrderedHashMapEntry implements Map.Entry {
176:
177: // ------------------------------------------------------------------------
178: // --- fields ---
179: // ------------------------------------------------------------------------
180:
181: /**
182: * The key.
183: */
184: protected Object key;
185:
186: /**
187: * The value.
188: */
189: protected Object value;
190:
191: // ------------------------------------------------------------------------
192: // --- constructors ---
193: // ------------------------------------------------------------------------
194:
195: /**
196: * Creates a new instance of OrderedHashMapEntry.
197: */
198: public OrderedHashMapEntry() {
199: //nop
200: }
201:
202: /**
203: * Creates a new instance of OrderedHashMapEntry.
204: */
205: public OrderedHashMapEntry(Object key, Object value) {
206: this ();
207: setKey(key);
208: setValue(value);
209: }
210:
211: // ------------------------------------------------------------------------
212: // --- methods ---
213: // ------------------------------------------------------------------------
214:
215: public boolean equals(Object o) {
216: // your code here
217: return false;
218: }
219:
220: public int hashCode() {
221: // your code here
222: return 0;
223: }
224:
225: /**
226: * Returns the key.
227: */
228: public Object getKey() {
229: return key;
230: }
231:
232: /**
233: * Sets the key.
234: */
235: public void setKey(Object _key) {
236: key = _key;
237: }
238:
239: /**
240: * Returns the value.
241: */
242: public Object getValue() {
243: return value;
244: }
245:
246: /**
247: * Sets the value.
248: */
249: public Object setValue(Object _value) {
250: Object old = value;
251: value = _value;
252: return old;
253: }
254:
255: } // end OrderedHashMapEntry
256:
257: } // end OrderedHashMap
|