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