OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 174 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 19... Line 19...
19
import java.util.List;
19
import java.util.List;
20
import java.util.Map;
20
import java.util.Map;
21
import java.util.Map.Entry;
21
import java.util.Map.Entry;
22
import java.util.Objects;
22
import java.util.Objects;
23
import java.util.Set;
23
import java.util.Set;
-
 
24
import java.util.function.Function;
24
 
25
 
25
import com.google.gson.reflect.TypeToken;
26
import com.google.gson.reflect.TypeToken;
26
 
27
 
27
import net.jcip.annotations.GuardedBy;
28
import net.jcip.annotations.GuardedBy;
28
import net.jcip.annotations.ThreadSafe;
29
import net.jcip.annotations.ThreadSafe;
Line 185... Line 186...
185
        @SuppressWarnings("unchecked")
186
        @SuppressWarnings("unchecked")
186
        final Collection<T> res = (Collection<T>) object;
187
        final Collection<T> res = (Collection<T>) object;
187
        return res;
188
        return res;
188
    }
189
    }
189
 
190
    
-
 
191
    public final <T> Collection<T> computeCollectionIfAbsent(Object k, Class<T> clazz, Function<Object, ? extends Collection<T>> mappingFunction) {
-
 
192
        Collection<T> res;
-
 
193
        synchronized (this) {
-
 
194
            res = this.getCollection(k, clazz);
-
 
195
            // same algorithm as java.util.Map
-
 
196
            if (res == null) {
-
 
197
                res = mappingFunction.apply(k);
-
 
198
                if (res != null)
-
 
199
                    this.putCollection(k, res, clazz);
-
 
200
            }
-
 
201
        }
-
 
202
        return res;
-
 
203
    }
-
 
204
 
-
 
205
 
190
    public final <T> List<T> getList(Object k, Class<T> clazz) {
206
    public final <T> List<T> getList(Object k, Class<T> clazz) {
191
        return (List<T>) this.getCollection(k, clazz);
207
        return (List<T>) this.getCollection(k, clazz);
192
    }
208
    }
193
 
209
 
194
    public final <T> Set<T> getSet(Object k, Class<T> clazz) {
210
    public final <T> Set<T> getSet(Object k, Class<T> clazz) {
Line 200... Line 216...
200
        @SuppressWarnings("unchecked")
216
        @SuppressWarnings("unchecked")
201
        final Map<K, V> res = (Map<K, V>) object;
217
        final Map<K, V> res = (Map<K, V>) object;
202
        return res;
218
        return res;
203
    }
219
    }
204
 
220
 
-
 
221
    public final <K, V> Map<K, V> computeMapIfAbsent(Object k, Class<K> keyClass, Class<V> valueClass, Function<Object, ? extends Map<K, V>> mappingFunction) {
-
 
222
        Map<K, V> res;
-
 
223
        synchronized (this) {
-
 
224
            res = this.getMap(k, keyClass, valueClass);
-
 
225
            // same algorithm as java.util.Map
-
 
226
            if (res == null) {
-
 
227
                res = mappingFunction.apply(k);
-
 
228
                if (res != null)
-
 
229
                    this.putMap(k, res, keyClass, valueClass);
-
 
230
            }
-
 
231
        }
-
 
232
        return res;
-
 
233
    }
-
 
234
 
205
    public final <T> T getGeneric(Object k, TypeToken<T> clazz) {
235
    public final <T> T getGeneric(Object k, TypeToken<T> clazz) {
206
        final Object object = checkType(k, clazz, "putGeneric()");
236
        final Object object = checkType(k, clazz, "putGeneric()");
207
        @SuppressWarnings("unchecked")
237
        @SuppressWarnings("unchecked")
208
        final T res = (T) object;
238
        final T res = (T) object;
209
        return res;
239
        return res;
210
    }
240
    }
211
 
241
 
-
 
242
    public final <T> T computeIfAbsent(Object k, TypeToken<T> clazz, Function<Object, ? extends T> mappingFunction) {
-
 
243
        T res;
-
 
244
        synchronized (this) {
-
 
245
            res = this.getGeneric(k, clazz);
-
 
246
            // same algorithm as java.util.Map
-
 
247
            if (res == null) {
-
 
248
                res = mappingFunction.apply(k);
-
 
249
                if (res != null)
-
 
250
                    this.putGeneric(k, res, clazz);
-
 
251
            }
-
 
252
        }
-
 
253
        return res;
-
 
254
    }
-
 
255
 
212
    @Override
256
    @Override
213
    public synchronized String toString() {
257
    public synchronized String toString() {
214
        return this.getClass().getSimpleName() + " of " + this.map.keySet();
258
        return this.getClass().getSimpleName() + " of " + this.map.keySet();
215
    }
259
    }
216
}
260
}