001: /*
002: * This is free software, licensed under the Gnu Public License (GPL)
003: * get a copy from <http://www.gnu.org/licenses/gpl.html>
004: * @version $Id: ListMap.java,v 1.6 2005/11/27 16:20:28 hzeller Exp $
005: * @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
006: */
007: package henplus.util;
008:
009: import java.io.Serializable;
010: import java.util.ArrayList;
011: import java.util.Collection;
012: import java.util.HashSet;
013: import java.util.List;
014: import java.util.ListIterator;
015: import java.util.Map;
016: import java.util.Set;
017:
018: /**
019: * This provides the functionality of LinkedHashMap. However, that
020: * Collection became available at 1.4. So provide this for backward
021: * compatibility.
022: *
023: * @author Martin Grotzke
024: */
025: public final class ListMap implements Map, Serializable {
026: private static final long serialVersionUID = 1;
027:
028: private List keys;
029: private List values;
030:
031: public ListMap() {
032: keys = new ArrayList();
033: values = new ArrayList();
034: }
035:
036: public int size() {
037: return keys.size();
038: }
039:
040: public boolean isEmpty() {
041: return keys.isEmpty();
042: }
043:
044: public boolean containsKey(Object key) {
045: return keys.contains(key);
046: }
047:
048: public boolean containsValue(Object value) {
049: return values.contains(value);
050: }
051:
052: public Object get(Object key) {
053: int index = keys.indexOf(key);
054: return (index > -1) ? values.get(index) : null;
055: }
056:
057: public Object put(Object key, Object value) {
058: Object orgValue = get(key);
059: keys.add(key);
060: values.add(value);
061: return orgValue;
062: }
063:
064: public Object remove(Object key) {
065: Object orgValue = get(key);
066: keys.remove(key);
067: values.remove(orgValue);
068: return orgValue;
069: }
070:
071: public void putAll(Map t) {
072: /**@todo Implement this java.util.Map method*/
073: throw new java.lang.UnsupportedOperationException(
074: "Method putAll() not yet implemented.");
075: }
076:
077: public void clear() {
078: keys.clear();
079: values.clear();
080: }
081:
082: public Set keySet() {
083: return new HashSet(keys);
084: }
085:
086: /**
087: * Returns a <code>List</code> containing all keys.
088: * @return a <code>List</code> containing all keys.
089: */
090: public List keys() {
091: return keys;
092: }
093:
094: /**
095: * Returns a <code>ListIterator</code> over the keys.
096: * Use this method instead of combining the <code>keySet</code> with it's <code>iterator</code> method.
097: */
098: public ListIterator keysListIterator() {
099: return keys.listIterator();
100: }
101:
102: /**
103: * Returns the values as a <code>Collection</code>, as defined in <code>java.util.Map</code>.
104: */
105: public Collection values() {
106: return (Collection) ((ArrayList) values).clone();
107: }
108:
109: /**
110: * Returns the values as a <code>List</code>.
111: */
112: public List valuesList() {
113: return (List) ((ArrayList) values).clone();
114: }
115:
116: /**
117: * Returns a <code>ListIterator</code> over the values.
118: */
119: public ListIterator valuesListIterator() {
120: return values.listIterator();
121: }
122:
123: public Set entrySet() {
124: /**@todo Implement this java.util.Map method*/
125: throw new java.lang.UnsupportedOperationException(
126: "Method entrySet() not yet implemented.");
127: }
128:
129: public boolean equals(Object o) {
130: /**@todo Implement this java.util.Map method*/
131: throw new java.lang.UnsupportedOperationException(
132: "Method equals() not yet implemented.");
133: }
134:
135: public String toString() {
136: StringBuffer sb = new StringBuffer();
137: sb.append("ListMap [");
138: for (int i = 0; i < keys.size(); i++)
139: sb.append(keys.get(i)).append(", ");
140: sb.delete(sb.length() - 2, sb.length());
141: sb.append("]");
142: return sb.toString();
143: }
144: }
|