OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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