65 |
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 |
|
83 |
ilm |
16 |
import org.openconcerto.utils.CollectionMap2Itf.SetMapItf;
|
|
|
17 |
|
|
|
18 |
import java.util.Arrays;
|
65 |
ilm |
19 |
import java.util.Collection;
|
83 |
ilm |
20 |
import java.util.Collections;
|
65 |
ilm |
21 |
import java.util.HashSet;
|
|
|
22 |
import java.util.Map;
|
|
|
23 |
import java.util.Set;
|
|
|
24 |
|
83 |
ilm |
25 |
public class SetMap<K, V> extends CollectionMap2<K, Set<V>, V> implements SetMapItf<K, V> {
|
65 |
ilm |
26 |
|
83 |
ilm |
27 |
static private class Unmodifiable<K, V> extends UnmodifiableCollectionMap<K, Set<V>, V> implements SetMapItf<K, V> {
|
|
|
28 |
Unmodifiable(CollectionMap2Itf<K, Set<V>, V> delegate) {
|
|
|
29 |
super(delegate, new UnmodifiableCollectionMap.UnmodifiableMap<K, Set<V>>(delegate) {
|
|
|
30 |
@Override
|
93 |
ilm |
31 |
protected Set<V> nonNullToUnmodifiable(Set<V> coll) {
|
83 |
ilm |
32 |
return Collections.unmodifiableSet(coll);
|
|
|
33 |
}
|
|
|
34 |
});
|
|
|
35 |
}
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
static public <K, V> SetMapItf<K, V> unmodifiableMap(SetMapItf<K, V> map) {
|
|
|
39 |
return new Unmodifiable<K, V>(map);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
|
43 |
private static SetMap EMPTY = new SetMap(Collections.emptyMap(), Mode.NULL_FORBIDDEN) {
|
|
|
44 |
@Override
|
|
|
45 |
public SetMap clone() {
|
|
|
46 |
// this instance is immutable and Collections classes might be cloneable
|
|
|
47 |
return this;
|
|
|
48 |
}
|
|
|
49 |
};
|
|
|
50 |
|
|
|
51 |
@SuppressWarnings("unchecked")
|
|
|
52 |
public static <K, V> SetMap<K, V> empty() {
|
|
|
53 |
return EMPTY;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public static <K, V> SetMap<K, V> singleton(K key, V... values) {
|
|
|
57 |
return singleton(key, Arrays.asList(values));
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public static <K, V> SetMap<K, V> singleton(K key, Collection<? extends V> values) {
|
|
|
61 |
return singleton(key, new HashSet<V>(values), false);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
private static <K, V> SetMap<K, V> singleton(K key, Set<V> values, final boolean immutable) {
|
|
|
65 |
final Set<V> coll = immutable ? values : Collections.unmodifiableSet(values);
|
|
|
66 |
return new SetMap<K, V>(Collections.singletonMap(key, coll), DEFAULT_MODE) {
|
|
|
67 |
@Override
|
|
|
68 |
public SetMap<K, V> clone() {
|
|
|
69 |
// this instance is immutable and Collections classes might be cloneable
|
|
|
70 |
return this;
|
|
|
71 |
}
|
|
|
72 |
};
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// to avoid
|
|
|
76 |
// "Type safety : A generic array of Tuple2<String,Boolean> is created for a varargs parameter"
|
|
|
77 |
public static <K, V> SetMap<K, V> singleton(K key, V value) {
|
|
|
78 |
return singleton(key, Collections.singleton(value), true);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
// static method since one-argument is for copying (see CopyUtils)
|
|
|
82 |
static public <K, V> SetMap<K, V> decorate(final Map<K, Set<V>> m) {
|
|
|
83 |
return new SetMap<K, V>(m, DEFAULT_MODE);
|
|
|
84 |
}
|
|
|
85 |
|
65 |
ilm |
86 |
public SetMap() {
|
|
|
87 |
super();
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
public SetMap(int initialCapacity, Mode mode, boolean emptyCollSameAsNoColl) {
|
|
|
91 |
super(initialCapacity, mode, emptyCollSameAsNoColl);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
public SetMap(int initialCapacity) {
|
|
|
95 |
super(initialCapacity);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
public SetMap(Mode mode, boolean emptyCollSameAsNoColl) {
|
|
|
99 |
super(mode, emptyCollSameAsNoColl);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
public SetMap(Mode mode) {
|
|
|
103 |
super(mode);
|
|
|
104 |
}
|
|
|
105 |
|
83 |
ilm |
106 |
public SetMap(Map<K, Set<V>> delegate, Mode mode) {
|
|
|
107 |
super(delegate, mode);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public SetMap(final Map<K, Set<V>> delegate, final Mode mode, final Boolean emptyCollSameAsNoColl) {
|
|
|
111 |
super(delegate, mode, emptyCollSameAsNoColl);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
// ** copy constructors
|
|
|
115 |
|
|
|
116 |
public SetMap(CollectionMap2<K, Set<V>, ? extends V> m) {
|
|
|
117 |
super(m);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
public SetMap(Map<? extends K, ? extends Collection<? extends V>> m) {
|
|
|
121 |
super(m);
|
|
|
122 |
}
|
|
|
123 |
|
65 |
ilm |
124 |
@Override
|
81 |
ilm |
125 |
public Set<V> createCollection(Collection<? extends V> v) {
|
65 |
ilm |
126 |
return new HashSet<V>(v);
|
|
|
127 |
}
|
83 |
ilm |
128 |
|
|
|
129 |
@Override
|
|
|
130 |
public SetMap<K, V> clone() throws CloneNotSupportedException {
|
|
|
131 |
return (SetMap<K, V>) super.clone();
|
|
|
132 |
}
|
65 |
ilm |
133 |
}
|