OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
65 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
65 ilm 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) {
182 ilm 39
        if (map.isEmpty())
40
            return empty();
83 ilm 41
        return new Unmodifiable<K, V>(map);
42
    }
43
 
44
    @SuppressWarnings({ "unchecked", "rawtypes" })
45
    private static SetMap EMPTY = new SetMap(Collections.emptyMap(), Mode.NULL_FORBIDDEN) {
46
        @Override
47
        public SetMap clone() {
48
            // this instance is immutable and Collections classes might be cloneable
49
            return this;
50
        }
51
    };
52
 
53
    @SuppressWarnings("unchecked")
54
    public static <K, V> SetMap<K, V> empty() {
55
        return EMPTY;
56
    }
57
 
58
    public static <K, V> SetMap<K, V> singleton(K key, V... values) {
59
        return singleton(key, Arrays.asList(values));
60
    }
61
 
62
    public static <K, V> SetMap<K, V> singleton(K key, Collection<? extends V> values) {
63
        return singleton(key, new HashSet<V>(values), false);
64
    }
65
 
66
    private static <K, V> SetMap<K, V> singleton(K key, Set<V> values, final boolean immutable) {
67
        final Set<V> coll = immutable ? values : Collections.unmodifiableSet(values);
68
        return new SetMap<K, V>(Collections.singletonMap(key, coll), DEFAULT_MODE) {
69
            @Override
70
            public SetMap<K, V> clone() {
71
                // this instance is immutable and Collections classes might be cloneable
72
                return this;
73
            }
74
        };
75
    }
76
 
77
    // to avoid
78
    // "Type safety : A generic array of Tuple2<String,Boolean> is created for a varargs parameter"
79
    public static <K, V> SetMap<K, V> singleton(K key, V value) {
80
        return singleton(key, Collections.singleton(value), true);
81
    }
82
 
83
    // static method since one-argument is for copying (see CopyUtils)
84
    static public <K, V> SetMap<K, V> decorate(final Map<K, Set<V>> m) {
85
        return new SetMap<K, V>(m, DEFAULT_MODE);
86
    }
87
 
65 ilm 88
    public SetMap() {
89
        super();
90
    }
91
 
92
    public SetMap(int initialCapacity, Mode mode, boolean emptyCollSameAsNoColl) {
93
        super(initialCapacity, mode, emptyCollSameAsNoColl);
94
    }
95
 
96
    public SetMap(int initialCapacity) {
97
        super(initialCapacity);
98
    }
99
 
100
    public SetMap(Mode mode, boolean emptyCollSameAsNoColl) {
101
        super(mode, emptyCollSameAsNoColl);
102
    }
103
 
104
    public SetMap(Mode mode) {
105
        super(mode);
106
    }
107
 
83 ilm 108
    public SetMap(Map<K, Set<V>> delegate, Mode mode) {
109
        super(delegate, mode);
110
    }
111
 
112
    public SetMap(final Map<K, Set<V>> delegate, final Mode mode, final Boolean emptyCollSameAsNoColl) {
113
        super(delegate, mode, emptyCollSameAsNoColl);
114
    }
115
 
116
    // ** copy constructors
117
 
118
    public SetMap(CollectionMap2<K, Set<V>, ? extends V> m) {
119
        super(m);
120
    }
121
 
122
    public SetMap(Map<? extends K, ? extends Collection<? extends V>> m) {
123
        super(m);
124
    }
125
 
65 ilm 126
    @Override
81 ilm 127
    public Set<V> createCollection(Collection<? extends V> v) {
65 ilm 128
        return new HashSet<V>(v);
129
    }
83 ilm 130
 
131
    @Override
177 ilm 132
    public SetMap<K, V> clone() {
83 ilm 133
        return (SetMap<K, V>) super.clone();
134
    }
65 ilm 135
}