OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

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