Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / vividsolutions / jump / util / OrderedMap.java @ 312

History | View | Annotate | Download (3.39 KB)

1

    
2
/*
3
 * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI 
4
 * for visualizing and manipulating spatial features with geometry and attributes.
5
 *
6
 * Copyright (C) 2003 Vivid Solutions
7
 * 
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 * 
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 * 
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
 * 
22
 * For more information, contact:
23
 *
24
 * Vivid Solutions
25
 * Suite #1A
26
 * 2328 Government Street
27
 * Victoria BC  V8T 5G5
28
 * Canada
29
 *
30
 * (250)385-6040
31
 * www.vividsolutions.com
32
 */
33

    
34
package com.vividsolutions.jump.util;
35

    
36
import java.util.*;
37

    
38

    
39
/**
40
 * A Map that preserves the order of its keys.
41
 */
42
public class OrderedMap implements Map {
43
    private Map map;
44
    private List keyList;
45

    
46
    /**
47
     * Creates an OrderedMap backed by the given map.
48
     * @param map a Map that will be this OrderedMap's underlying Map
49
     */
50
    public OrderedMap(List keyList, Map map) {
51
            this.keyList = keyList;
52
        this.map = map;
53
    }
54

    
55
    /**
56
     * Creates an OrderedMap.
57
     */
58
    public OrderedMap() {
59
        this(new HashMap());
60
    }
61
    
62
    public OrderedMap(Map map) {
63
            this(new UniqueList(), map);
64
    }
65

    
66
    public int size() {
67
        return map.size();
68
    }
69

    
70
    public boolean isEmpty() {
71
        return map.isEmpty();
72
    }
73

    
74
    public boolean containsKey(Object key) {
75
        return map.containsKey(key);
76
    }
77

    
78
    public boolean containsValue(Object value) {
79
        return map.containsValue(value);
80
    }
81

    
82
    public Object get(Object key) {
83
        return map.get(key);
84
    }
85

    
86
    public Object put(Object key, Object value) {
87
        keyList.add(key);
88

    
89
        return map.put(key, value);
90
    }
91

    
92
    public Object remove(Object key) {
93
        keyList.remove(key);
94

    
95
        return map.remove(key);
96
    }
97

    
98
    public void putAll(Map t) {
99
        keyList.addAll(t.keySet());
100
        map.putAll(t);
101
    }
102

    
103
    public void clear() {
104
        keyList.clear();
105
        map.clear();
106
    }
107

    
108
    public Set keySet() {
109
        return map.keySet();
110
    }
111

    
112
    /**
113
     * Returns the keys, in order.
114
     * @return the keys in the order they were (first) added
115
     */
116
    public List keyList() {
117
        return keyList;
118
    }
119

    
120
    /**
121
     * Returns the values.
122
     * @return the values in the same order as the keys
123
     * @see #keyList()
124
     */
125
    public List valueList() {
126
        return (List) values();
127
    }
128

    
129
    /**
130
     * Returns the values.
131
     * @return the values in the same order as the keys
132
     * @see #keyList()
133
     */
134
    public Collection values() {
135
        ArrayList values = new ArrayList();
136

    
137
        for (Iterator i = keyList.iterator(); i.hasNext();) {
138
            Object key = i.next();
139
            values.add(map.get(key));
140
        }
141

    
142
        return values;
143
    }
144

    
145
    public Set entrySet() {
146
        return map.entrySet();
147
    }
148

    
149
    public boolean equals(Object o) {
150
        return map.equals(o);
151
    }
152
}