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 | 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
 *
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.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
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
    }
85
 
65 ilm 86
    public ListMap() {
87
        super();
88
    }
89
 
83 ilm 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
 
65 ilm 108
    public ListMap(Map<? extends K, ? extends Collection<? extends V>> m) {
109
        super(m);
110
    }
111
 
83 ilm 112
    public ListMap(Map<K, List<V>> delegate, Map<? extends K, ? extends Collection<? extends V>> m) {
113
        super(delegate, m);
114
    }
115
 
65 ilm 116
    @Override
81 ilm 117
    public List<V> createCollection(Collection<? extends V> v) {
65 ilm 118
        return new ArrayList<V>(v);
119
    }
83 ilm 120
 
121
    @Override
122
    public ListMap<K, V> clone() throws CloneNotSupportedException {
123
        return (ListMap<K, V>) super.clone();
124
    }
65 ilm 125
}