OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
83 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
 
16
import org.openconcerto.utils.CollectionMap2.Mode;
17
 
18
import java.util.Collection;
180 ilm 19
import java.util.Collections;
83 ilm 20
import java.util.List;
21
import java.util.Map;
180 ilm 22
import java.util.Objects;
83 ilm 23
import java.util.Set;
180 ilm 24
import java.util.function.Function;
83 ilm 25
 
26
public interface CollectionMap2Itf<K, C extends Collection<V>, V> extends Map<K, C> {
27
 
28
    public static interface ListMapItf<K, V> extends CollectionMap2Itf<K, List<V>, V> {
180 ilm 29
        /**
30
         * Change this instance and return an unmodifiable Map view. After this method, this
31
         * instance shouldn't be modified, e.g. if a new entry (with a modifiable collection) is
32
         * added then the returned object will be able to modify it. Contrary to
33
         * {@link ListMap#unmodifiableMap(ListMapItf)}, the returned object doesn't allocate any
34
         * memory.
35
         *
36
         * @return an unmodifiable Map view.
37
         * @see CollectionMap2Itf#convertToUnmodifiableMap(Function)
38
         */
39
        public default Map<K, List<V>> convertToUnmodifiableMap() {
40
            return convertToUnmodifiableMap(Collections::unmodifiableList);
41
        }
83 ilm 42
    }
43
 
44
    public static interface SetMapItf<K, V> extends CollectionMap2Itf<K, Set<V>, V> {
180 ilm 45
        /**
46
         * Change this instance and return an unmodifiable Map view. After this method, this
47
         * instance shouldn't be modified, e.g. if a new entry (with a modifiable collection) is
48
         * added then the returned object will be able to modify it. Contrary to
49
         * {@link SetMap#unmodifiableMap(ListMapItf)}, the returned object doesn't allocate any
50
         * memory.
51
         *
52
         * @return an unmodifiable Map view.
53
         * @see CollectionMap2Itf#convertToUnmodifiableMap(Function)
54
         */
55
        public default Map<K, Set<V>> convertToUnmodifiableMap() {
56
            return convertToUnmodifiableMap(Collections::unmodifiableSet);
57
        }
83 ilm 58
    }
59
 
60
    public Mode getMode();
61
 
62
    public boolean isEmptyCollSameAsNoColl();
63
 
64
    public C getNonNull(K key);
65
 
66
    /**
67
     * Get the collection mapped to the passed key. Note : <code>get(key, true, true)</code> is
68
     * equivalent to <code>get(key)</code>.
69
     *
70
     * @param key the key whose associated value is to be returned.
71
     * @param nullIfMissing only relevant if the key isn't contained : if <code>true</code>
72
     *        <code>null</code> will be returned, otherwise an empty collection.
73
     * @param nullIfPresent only relevant if the key is mapped to <code>null</code> : if
74
     *        <code>true</code> <code>null</code> will be returned, otherwise an empty collection.
75
     * @return the non {@code null} value to which the specified key is mapped, otherwise
76
     *         {@code null} or empty collection depending on the other parameters.
77
     */
78
    public C get(Object key, final boolean nullIfMissing, final boolean nullIfPresent);
79
 
80
    public C getCollection(Object key);
81
 
82
    /**
177 ilm 83
     * Returns <tt>true</tt> if the collection mapped to the passed key contains the specified
84
     * element.
85
     *
86
     * @param key the key.
87
     * @param element element whose presence in the collection is to be tested.
88
     * @return <tt>true</tt> if the collection mapped to the passed key contains the specified
89
     *         element or if the key exists, its collection is <code>null</code> and the
90
     *         {@link #getMode() mode} is {@link Mode#NULL_MEANS_ALL} <br>
91
     *         <code>false</code> if <code>!this.containsKey(key)</code>.
92
     * @throws NullPointerException if the key exists, its collection is <code>null</code> and the
93
     *         {@link #getMode() mode} is not {@link Mode#NULL_MEANS_ALL}.
94
     */
95
    public boolean containsInCollection(K key, V element) throws NullPointerException;
96
 
97
    /**
83 ilm 98
     * Returns a {@link Collection} view of all the values contained in this map. The collection is
99
     * backed by the map, so changes to the map are reflected in the collection, and vice-versa. If
100
     * the map is modified while an iteration over the collection is in progress (except through the
101
     * iterator's own <tt>remove</tt> operation), the results of the iteration are undefined. The
102
     * collection supports element removal, which removes the corresponding values from the map, via
103
     * the <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, <tt>removeAll</tt>,
104
     * <tt>retainAll</tt> and <tt>clear</tt> operations. Note that it doesn't remove entries only
105
     * values : keySet() doesn't change, use {@link #removeAllEmptyCollections()} and
106
     * {@link #removeAllNullCollections()} afterwards. It does not support the <tt>add</tt> or
107
     * <tt>addAll</tt> operations.
108
     *
109
     * @return a view all values in all entries, <code>null</code> collections are ignored.
110
     */
111
    public Collection<V> allValues();
112
 
113
    // copy passed collection
114
    public C putCollection(final K key, final Collection<? extends V> value);
115
 
116
    public boolean add(final K k, final V v);
117
 
118
    public boolean addAll(final K k, final Collection<? extends V> v);
119
 
120
    /**
121
     * Call {@link #addAll(Object, Collection)} for each entry.
122
     *
123
     * @param mm the collection map to merge.
124
     */
125
    public void merge(final Map<? extends K, ? extends Collection<? extends V>> mm);
126
 
127
    /**
128
     * Call {@link #add(Object, Object)} for each entry.
129
     *
130
     * @param scalarMap the map to merge.
131
     */
132
    public void mergeScalarMap(final Map<? extends K, ? extends V> scalarMap);
133
 
144 ilm 134
    // cannot be called just "remove" since it conflicts with the new method in java 8
135
    public boolean removeOne(final K k, final V v);
83 ilm 136
 
144 ilm 137
    public boolean removeAllInstancesOfItem(final K k, final V v);
138
 
83 ilm 139
    public boolean removeAll(final K k, final Collection<? extends V> v);
140
 
141
    public boolean removeAll(final Map<? extends K, ? extends Collection<? extends V>> mm);
142
 
143
    public boolean removeAllScalar(final Map<? extends K, ? extends V> m);
144
 
145
    public Set<K> removeAllEmptyCollections();
146
 
147
    public Set<K> removeAllNullCollections();
180 ilm 148
 
149
    public default void replaceAllNonNullValues(final Function<? super C, ? extends C> function) {
150
        this.replaceAll((k, v) -> v == null ? null : function.apply(v));
151
    }
152
 
153
    /**
154
     * Change this instance and return an unmodifiable Map view. After this method, this instance
155
     * shouldn't be modified, e.g. if a new entry (with a modifiable collection) is added then the
156
     * returned object will be able to modify it. The returned object doesn't allocate any memory.
157
     *
158
     * @param toUnmodifiable how to replace collections with unmodifiable views, never passed
159
     *        <code>null</code>.
160
     * @return an unmodifiable Map view.
161
     * @see SetMapItf#convertToUnmodifiableMap()
162
     * @see ListMapItf#convertToUnmodifiableMap()
163
     */
164
    public default Map<K, C> convertToUnmodifiableMap(final Function<? super C, ? extends C> toUnmodifiable) {
165
        this.replaceAllNonNullValues(Objects.requireNonNull(toUnmodifiable));
166
        return Collections.unmodifiableMap(this);
167
    }
83 ilm 168
}