OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 81 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 81 Rev 83
Line 11... Line 11...
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.ListMapItf;
-
 
17
 
16
import java.util.ArrayList;
18
import java.util.ArrayList;
-
 
19
import java.util.Arrays;
17
import java.util.Collection;
20
import java.util.Collection;
-
 
21
import java.util.Collections;
18
import java.util.List;
22
import java.util.List;
19
import java.util.Map;
23
import java.util.Map;
20
 
24
 
21
public class ListMap<K, V> extends CollectionMap2<K, List<V>, V> {
25
public class ListMap<K, V> extends ListAbstractMap<K, List<V>, V> implements ListMapItf<K, V> {
-
 
26
 
-
 
27
    static private class Unmodifiable<K, V> extends UnmodifiableCollectionMap<K, List<V>, V> implements ListMapItf<K, V> {
-
 
28
        Unmodifiable(CollectionMap2Itf<K, List<V>, V> delegate) {
-
 
29
            super(delegate, new UnmodifiableCollectionMap.UnmodifiableMap<K, List<V>>(delegate) {
-
 
30
                @Override
-
 
31
                protected List<V> toUnmodifiable(List<V> coll) {
-
 
32
                    return Collections.unmodifiableList(coll);
-
 
33
                }
-
 
34
            });
-
 
35
        }
-
 
36
    }
-
 
37
 
-
 
38
    static public <K, V> ListMapItf<K, V> unmodifiableMap(ListMapItf<K, V> map) {
-
 
39
        return new Unmodifiable<K, V>(map);
-
 
40
    }
-
 
41
 
-
 
42
    @SuppressWarnings({ "unchecked", "rawtypes" })
-
 
43
    private static ListMap EMPTY = new ListMap(Collections.emptyMap(), Mode.NULL_FORBIDDEN) {
-
 
44
        @Override
-
 
45
        public ListMap 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> ListMap<K, V> empty() {
-
 
53
        return EMPTY;
-
 
54
    }
-
 
55
 
-
 
56
    public static <K, V> ListMap<K, V> singleton(K key, V... values) {
-
 
57
        return singleton(key, Arrays.asList(values), true);
-
 
58
    }
-
 
59
 
-
 
60
    public static <K, V> ListMap<K, V> singleton(K key, Collection<? extends V> values) {
-
 
61
        return singleton(key, new ArrayList<V>(values), false);
-
 
62
    }
-
 
63
 
-
 
64
    private static <K, V> ListMap<K, V> singleton(K key, List<V> values, final boolean immutable) {
-
 
65
        final List<V> coll = immutable ? values : Collections.unmodifiableList(values);
-
 
66
        return new ListMap<K, V>(Collections.singletonMap(key, coll), DEFAULT_MODE) {
-
 
67
            @Override
-
 
68
            public ListMap<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> ListMap<K, V> singleton(K key, V value) {
-
 
78
        return singleton(key, Collections.singletonList(value), true);
-
 
79
    }
-
 
80
 
-
 
81
    // static method since one-argument is for copying (see CopyUtils)
-
 
82
    static public <K, V> ListMap<K, V> decorate(final Map<K, List<V>> m) {
-
 
83
        return new ListMap<K, V>(m, DEFAULT_MODE);
-
 
84
    }
22
 
85
 
23
    public ListMap() {
86
    public ListMap() {
24
        super();
87
        super();
25
    }
88
    }
26
 
89
 
-
 
90
    public ListMap(Map<K, List<V>> delegate, Mode mode) {
-
 
91
        super(delegate, mode);
-
 
92
    }
-
 
93
 
-
 
94
    public ListMap(int initialCapacity) {
-
 
95
        super(initialCapacity);
-
 
96
    }
-
 
97
 
-
 
98
    public ListMap(int initialCapacity, Mode mode, Boolean emptyCollSameAsNoColl) {
-
 
99
        super(initialCapacity, mode, emptyCollSameAsNoColl);
-
 
100
    }
-
 
101
 
-
 
102
    // ** copy constructors
-
 
103
 
-
 
104
    public ListMap(CollectionMap2<K, List<V>, ? extends V> m) {
-
 
105
        super(m);
-
 
106
    }
-
 
107
 
27
    public ListMap(Map<? extends K, ? extends Collection<? extends V>> m) {
108
    public ListMap(Map<? extends K, ? extends Collection<? extends V>> m) {
28
        super(m);
109
        super(m);
29
    }
110
    }
30
 
111
 
-
 
112
    public ListMap(Map<K, List<V>> delegate, Map<? extends K, ? extends Collection<? extends V>> m) {
-
 
113
        super(delegate, m);
-
 
114
    }
-
 
115
 
31
    @Override
116
    @Override
32
    public List<V> createCollection(Collection<? extends V> v) {
117
    public List<V> createCollection(Collection<? extends V> v) {
33
        return new ArrayList<V>(v);
118
        return new ArrayList<V>(v);
34
    }
119
    }
-
 
120
 
-
 
121
    @Override
-
 
122
    public ListMap<K, V> clone() throws CloneNotSupportedException {
-
 
123
        return (ListMap<K, V>) super.clone();
-
 
124
    }
35
}
125
}