Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / UnmodifiableBasicMapToMapAdapter.java @ 1882

History | View | Annotate | Download (6.13 KB)

1
package org.gvsig.tools.util;
2

    
3
import java.util.AbstractMap;
4
import java.util.Arrays;
5
import java.util.Collection;
6
import java.util.Iterator;
7
import java.util.Map;
8
import java.util.Set;
9

    
10
/**
11
 *
12
 * @author jjdelcerro
13
 * 
14
 * @param <K> the type of keys maintained by this map
15
 * @param <V> the type of mapped values
16
 *
17
 */
18
public class UnmodifiableBasicMapToMapAdapter<K,V> implements Map<K,V> {
19

    
20
    private final UnmodifiableBasicMap<K,V> umap;
21

    
22
    public UnmodifiableBasicMapToMapAdapter(UnmodifiableBasicMap<K,V> umap ) {
23
        this.umap = umap;
24
    }
25
    
26
    @Override
27
    public int size() {
28
        return this.umap.size();
29
    }
30

    
31
    @Override
32
    public boolean isEmpty() {
33
        return this.umap.isEmpty();
34
    }
35

    
36
    @Override
37
    public boolean containsKey(Object key) {
38
        return this.umap.containsKey((K) key);
39
    }
40

    
41
    @Override
42
    public boolean containsValue(Object value) {
43
        for (V v : umap) {
44
            if( value == v ) {
45
                return true;
46
            }
47
            if( value!= null &&  v!=null ) {
48
                if( value.equals(v) ) {
49
                    return true;
50
                }
51
            }
52
        }
53
        return false;
54
    }
55

    
56
    @Override
57
    public V get(Object key) {
58
        return (V) this.umap.get((K) key);
59
    }
60
    
61
    @Override
62
    public Set<K> keySet() {    
63
        return new UnmodifiableBasicMapKeySet();
64
    }
65

    
66
    @Override
67
    public Collection<V> values() {
68
        return new UnmodifiableBasicMapValues();
69
    }
70

    
71
    @Override
72
    public Set<Entry<K, V>> entrySet() {
73
        return new UnmodifiableBasicMapEntrySet();
74
    }
75

    
76
    @Override
77
    public V put(K key, V value) {
78
        throw new UnsupportedOperationException("Not supported yet."); 
79
    }
80

    
81
    @Override
82
    public V remove(Object key) {
83
        throw new UnsupportedOperationException("Not supported yet."); 
84
    }
85

    
86
    @Override
87
    public void putAll(Map<? extends K, ? extends V> m) {
88
        throw new UnsupportedOperationException("Not supported yet."); 
89
    }
90

    
91
    @Override
92
    public void clear() {
93
        throw new UnsupportedOperationException("Not supported yet."); 
94
    }
95

    
96
    private abstract class AbstractUnmodifiableBasicMapCollection {
97

    
98
        public int size() {
99
            return umap.size();
100
        }
101

    
102
        public boolean isEmpty() {
103
            return umap.isEmpty();
104
        }
105

    
106
        public abstract boolean contains(Object value);
107

    
108
        public abstract Iterator iterator();
109
        
110
        public boolean containsAll(Collection c) {
111
            for (Object element : c) {
112
                if( !contains(element) ) {
113
                    return false;
114
                }
115
            }
116
            return true;
117
        }
118

    
119
        public Object[] toArray(Object[] a) {
120
            int sz = this.size();
121
            if( a.length<sz ) {
122
                a = Arrays.copyOf(new Object[] { null }, sz, a.getClass());
123
            }
124
            int n = 0;
125
            Iterator it = this.iterator();
126
            while( it.hasNext() ) {
127
                a[n] = it.next();
128
            }
129
            return a;
130
        }
131

    
132
        public Object[] toArray() {
133
            throw new UnsupportedOperationException("Not supported yet."); 
134
        }
135
        
136
        public boolean add(Object e) {
137
            throw new UnsupportedOperationException("Not supported yet."); 
138
        }
139

    
140
        public boolean remove(Object o) {
141
            throw new UnsupportedOperationException("Not supported yet."); 
142
        }
143

    
144
        public boolean addAll(Collection c) {
145
            throw new UnsupportedOperationException("Not supported yet."); 
146
        }
147

    
148
        public boolean retainAll(Collection c) {
149
            throw new UnsupportedOperationException("Not supported yet."); 
150
        }
151

    
152
        public boolean removeAll(Collection c) {
153
            throw new UnsupportedOperationException("Not supported yet."); 
154
        }
155

    
156
        public void clear() {
157
            throw new UnsupportedOperationException("Not supported yet."); 
158
        }
159
        
160
    }
161
    
162
    class UnmodifiableBasicMapEntrySet 
163
            extends AbstractUnmodifiableBasicMapCollection 
164
            implements Set {
165

    
166
        @Override
167
        public boolean contains(Object value) {
168
            for (K key : umap.keySet()) {
169
                Map.Entry entry = new AbstractMap.SimpleEntry<>(key, umap.get(key));
170
                if( value == entry ) {
171
                    return true;
172
                }
173
                if( value!= null ) {
174
                    if( value.equals(entry) ) {
175
                        return true;
176
                    }
177
                }
178
            }
179
            return false;
180
        }
181

    
182
        @Override
183
        public Iterator<Entry<K,V>> iterator() {
184
            final Iterator<K> it = umap.keySet().iterator();
185
            return new Iterator<Entry<K,V>>() {
186
                @Override
187
                public boolean hasNext() {
188
                    return it.hasNext();
189
                }
190

    
191
                @Override
192
                public Entry<K, V> next() {
193
                    K key = it.next();
194
                    Map.Entry entry = new AbstractMap.SimpleEntry<>(key, umap.get(key));
195
                    return entry;
196
                }
197
            };
198
        }
199
    }
200
    
201
    class UnmodifiableBasicMapKeySet 
202
            extends AbstractUnmodifiableBasicMapCollection 
203
            implements Set {
204

    
205
        @Override
206
        public boolean contains(Object value) {
207
            return umap.keySet().contains((K) value);
208
        }
209

    
210
        @Override
211
        public Iterator<K> iterator() {
212
            return umap.keySet().iterator();
213
        }
214

    
215
    }
216
    
217
    class UnmodifiableBasicMapValues 
218
            extends AbstractUnmodifiableBasicMapCollection 
219
            implements Collection {
220

    
221
        @Override
222
        public boolean contains(Object value) {
223
            for (V v : umap) {
224
                if( value == v ) {
225
                    return true;
226
                }
227
                if( value!= null &&  v!=null ) {
228
                    if( value.equals(v) ) {
229
                        return true;
230
                    }
231
                }
232
            }
233
            return false;
234
        }
235

    
236
        @Override
237
        public Iterator<V> iterator() {
238
            return umap.iterator();
239
        }
240

    
241
    }
242
    
243
}