OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
83 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils;
15
 
16
import org.openconcerto.utils.CollectionMap2.Mode;
17
import org.openconcerto.utils.cc.AbstractMapDecorator;
18
 
19
import java.util.AbstractCollection;
20
import java.util.AbstractSet;
21
import java.util.Collection;
22
import java.util.Collections;
23
import java.util.Iterator;
24
import java.util.Map;
25
import java.util.Set;
26
 
27
class UnmodifiableCollectionMap<K, C extends Collection<V>, V> extends AbstractMapDecorator<K, C> implements CollectionMap2Itf<K, C, V> {
28
 
29
    // protect Map methods and Collection values. The mutable CollectionMap2Itf methods are
30
    // protected by the enclosing class
31
    public static abstract class UnmodifiableMap<K, C extends Collection<?>> extends AbstractMapDecorator<K, C> {
32
 
33
        private final Map<K, C> del;
34
        private transient Collection<C> values;
35
        private transient Set<Map.Entry<K, C>> entrySet;
36
 
37
        protected UnmodifiableMap(Map<K, C> delegate) {
38
            super(Collections.unmodifiableMap(delegate));
39
            this.del = delegate;
40
        }
41
 
42
        @Override
43
        public final C get(final Object key) {
44
            return toUnmodifiable(super.get(key));
45
        }
46
 
47
        @Override
48
        public final Collection<C> values() {
49
            final Collection<C> vs = this.values;
50
            return vs != null ? vs : (this.values = new AbstractCollection<C>() {
51
 
52
                @Override
53
                public boolean contains(Object o) {
54
                    return UnmodifiableMap.this.containsValue(o);
55
                }
56
 
57
                @Override
58
                public Iterator<C> iterator() {
59
                    final Iterator<Map.Entry<K, C>> iter = entrySet().iterator();
60
                    return new Iterator<C>() {
61
 
62
                        @Override
63
                        public boolean hasNext() {
64
                            return iter.hasNext();
65
                        }
66
 
67
                        @Override
68
                        public C next() {
69
                            // our entrySet() is already unmodifiable
70
                            return iter.next().getValue();
71
                        }
72
 
73
                        @Override
74
                        public void remove() {
75
                            throw new UnsupportedOperationException();
76
                        }
77
                    };
78
                }
79
 
80
                @Override
81
                public int size() {
82
                    return UnmodifiableMap.this.size();
83
                }
84
            });
85
        }
86
 
87
        @Override
88
        public final Set<Map.Entry<K, C>> entrySet() {
89
            final Set<Map.Entry<K, C>> es = this.entrySet;
90
            return es != null ? es : (this.entrySet = new AbstractSet<Map.Entry<K, C>>() {
91
                private final Set<Map.Entry<K, C>> delegateES = UnmodifiableMap.super.entrySet();
92
 
93
                @Override
94
                public boolean contains(Object o) {
95
                    return this.delegateES.contains(o);
96
                }
97
 
98
                @Override
99
                public Iterator<Map.Entry<K, C>> iterator() {
100
                    final Iterator<Map.Entry<K, C>> iter = this.delegateES.iterator();
101
                    return new Iterator<Map.Entry<K, C>>() {
102
 
103
                        @Override
104
                        public boolean hasNext() {
105
                            return iter.hasNext();
106
                        }
107
 
108
                        @Override
109
                        public Map.Entry<K, C> next() {
110
                            final Map.Entry<K, C> orig = iter.next();
111
                            return new Map.Entry<K, C>() {
112
 
113
                                @Override
114
                                public K getKey() {
115
                                    return orig.getKey();
116
                                }
117
 
118
                                @Override
119
                                public C getValue() {
120
                                    return toUnmodifiable(orig.getValue());
121
                                }
122
 
123
                                @Override
124
                                public C setValue(C value) {
125
                                    throw new UnsupportedOperationException();
126
                                }
127
                            };
128
                        }
129
 
130
                        @Override
131
                        public void remove() {
132
                            throw new UnsupportedOperationException();
133
                        }
134
                    };
135
                }
136
 
137
                @Override
138
                public int size() {
139
                    return UnmodifiableMap.this.size();
140
                }
141
 
142
                @Override
143
                public boolean equals(Object o) {
144
                    return o == this || this.delegateES.equals(o);
145
                }
146
 
147
                @Override
148
                public int hashCode() {
149
                    return this.delegateES.hashCode();
150
                }
151
            });
152
        }
153
 
93 ilm 154
        protected final C toUnmodifiable(C coll) {
155
            if (coll == null)
156
                return null;
157
            else
158
                return this.nonNullToUnmodifiable(coll);
159
        }
160
 
161
        protected abstract C nonNullToUnmodifiable(C coll);
83 ilm 162
    }
163
 
164
    private final CollectionMap2Itf<K, C, V> del;
165
    private transient Collection<V> values;
166
 
167
    UnmodifiableCollectionMap(final CollectionMap2Itf<K, C, V> delegate, final UnmodifiableMap<K, C> unmodif) {
168
        super(unmodif);
169
        if (unmodif.del != delegate)
170
            throw new IllegalArgumentException("Mismatched arguments");
171
        this.del = delegate;
172
    }
173
 
174
    private final CollectionMap2Itf<K, C, V> getDel() {
175
        return this.del;
176
    }
177
 
178
    @Override
179
    public Mode getMode() {
180
        return getDel().getMode();
181
    }
182
 
183
    @Override
184
    public boolean isEmptyCollSameAsNoColl() {
185
        return getDel().isEmptyCollSameAsNoColl();
186
    }
187
 
188
    @Override
189
    public C getNonNull(final K key) {
190
        return getDel().getNonNull(key);
191
    }
192
 
193
    @Override
194
    public C get(final Object key, final boolean nullIfMissing, final boolean nullIfPresent) {
195
        return getDel().get(key, nullIfMissing, nullIfPresent);
196
    }
197
 
198
    @Override
199
    public C getCollection(final Object key) {
200
        return getDel().getCollection(key);
201
    }
202
 
203
    @Override
177 ilm 204
    public boolean containsInCollection(K key, V val) {
205
        return getDel().containsInCollection(key, val);
206
    }
207
 
208
    @Override
83 ilm 209
    public Collection<V> allValues() {
210
        if (this.values == null) {
211
            this.values = Collections.unmodifiableCollection(getDel().allValues());
212
        }
213
        return this.values;
214
    }
215
 
216
    @Override
217
    public C putCollection(final K key, final Collection<? extends V> value) {
218
        throw new UnsupportedOperationException();
219
    }
220
 
221
    @Override
222
    public boolean add(final K k, final V v) {
223
        throw new UnsupportedOperationException();
224
    }
225
 
226
    @Override
227
    public boolean addAll(final K k, final Collection<? extends V> v) {
228
        throw new UnsupportedOperationException();
229
    }
230
 
231
    @Override
232
    public void merge(final Map<? extends K, ? extends Collection<? extends V>> mm) {
233
        throw new UnsupportedOperationException();
234
    }
235
 
236
    @Override
237
    public void mergeScalarMap(final Map<? extends K, ? extends V> scalarMap) {
238
        throw new UnsupportedOperationException();
239
    }
240
 
241
    @Override
144 ilm 242
    public boolean removeOne(final K k, final V v) {
83 ilm 243
        throw new UnsupportedOperationException();
244
    }
245
 
246
    @Override
144 ilm 247
    public boolean removeAllInstancesOfItem(final K k, final V v) {
248
        throw new UnsupportedOperationException();
249
    }
250
 
251
    @Override
83 ilm 252
    public boolean removeAll(final K k, final Collection<? extends V> v) {
253
        throw new UnsupportedOperationException();
254
    }
255
 
256
    @Override
257
    public boolean removeAll(final Map<? extends K, ? extends Collection<? extends V>> mm) {
258
        throw new UnsupportedOperationException();
259
    }
260
 
261
    @Override
262
    public boolean removeAllScalar(final Map<? extends K, ? extends V> m) {
263
        throw new UnsupportedOperationException();
264
    }
265
 
266
    @Override
267
    public Set<K> removeAllEmptyCollections() {
268
        throw new UnsupportedOperationException();
269
    }
270
 
271
    @Override
272
    public Set<K> removeAllNullCollections() {
273
        throw new UnsupportedOperationException();
274
    }
275
}