001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.util;
034:
035: import java.util.*;
036:
037: /**
038: * A Map that preserves the order of its keys.
039: */
040: public class OrderedMap implements Map {
041: private Map map;
042: private List keyList;
043:
044: /**
045: * Creates an OrderedMap backed by the given map.
046: * @param map a Map that will be this OrderedMap's underlying Map
047: */
048: public OrderedMap(List keyList, Map map) {
049: this .keyList = keyList;
050: this .map = map;
051: }
052:
053: /**
054: * Creates an OrderedMap.
055: */
056: public OrderedMap() {
057: this (new HashMap());
058: }
059:
060: public OrderedMap(Map map) {
061: this (new UniqueList(), map);
062: }
063:
064: public int size() {
065: return map.size();
066: }
067:
068: public boolean isEmpty() {
069: return map.isEmpty();
070: }
071:
072: public boolean containsKey(Object key) {
073: return map.containsKey(key);
074: }
075:
076: public boolean containsValue(Object value) {
077: return map.containsValue(value);
078: }
079:
080: public Object get(Object key) {
081: return map.get(key);
082: }
083:
084: public Object put(Object key, Object value) {
085: keyList.add(key);
086:
087: return map.put(key, value);
088: }
089:
090: public Object remove(Object key) {
091: keyList.remove(key);
092:
093: return map.remove(key);
094: }
095:
096: public void putAll(Map t) {
097: keyList.addAll(t.keySet());
098: map.putAll(t);
099: }
100:
101: public void clear() {
102: keyList.clear();
103: map.clear();
104: }
105:
106: public Set keySet() {
107: return map.keySet();
108: }
109:
110: /**
111: * Returns the keys, in order.
112: * @return the keys in the order they were (first) added
113: */
114: public List keyList() {
115: return keyList;
116: }
117:
118: /**
119: * Returns the values.
120: * @return the values in the same order as the keys
121: * @see #keyList()
122: */
123: public List valueList() {
124: return (List) values();
125: }
126:
127: /**
128: * Returns the values.
129: * @return the values in the same order as the keys
130: * @see #keyList()
131: */
132: public Collection values() {
133: ArrayList values = new ArrayList();
134:
135: for (Iterator i = keyList.iterator(); i.hasNext();) {
136: Object key = i.next();
137: values.add(map.get(key));
138: }
139:
140: return values;
141: }
142:
143: public Set entrySet() {
144: return map.entrySet();
145: }
146:
147: public boolean equals(Object o) {
148: return map.equals(o);
149: }
150: }
|