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 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.cc.CustomEquals;
|
16 |
import org.openconcerto.utils.cc.IClosure;
|
17 |
import org.openconcerto.utils.cc.IClosure;
|
17 |
import org.openconcerto.utils.cc.IPredicate;
|
18 |
import org.openconcerto.utils.cc.IPredicate;
|
18 |
import org.openconcerto.utils.cc.ITransformer;
|
19 |
import org.openconcerto.utils.cc.ITransformer;
|
19 |
import org.openconcerto.utils.cc.ITransformerExn;
|
20 |
import org.openconcerto.utils.cc.ITransformerExn;
|
20 |
import org.openconcerto.utils.cc.IdentityHashSet;
|
21 |
import org.openconcerto.utils.cc.IdentityHashSet;
|
Line 45... |
Line 46... |
45 |
import java.util.SortedMap;
|
46 |
import java.util.SortedMap;
|
46 |
import java.util.SortedSet;
|
47 |
import java.util.SortedSet;
|
47 |
import java.util.TreeMap;
|
48 |
import java.util.TreeMap;
|
48 |
import java.util.TreeSet;
|
49 |
import java.util.TreeSet;
|
49 |
import java.util.function.Function;
|
50 |
import java.util.function.Function;
|
- |
|
51 |
import java.util.function.Supplier;
|
50 |
import java.util.regex.Pattern;
|
52 |
import java.util.regex.Pattern;
|
- |
|
53 |
import java.util.stream.Collectors;
|
- |
|
54 |
import java.util.stream.Stream;
|
51 |
|
55 |
|
52 |
/**
|
56 |
/**
|
53 |
* Une classe regroupant des méthodes utilitaires pour les collections.
|
57 |
* Une classe regroupant des méthodes utilitaires pour les collections.
|
54 |
*
|
58 |
*
|
55 |
* @author ILM Informatique 30 sept. 2004
|
59 |
* @author ILM Informatique 30 sept. 2004
|
Line 401... |
Line 405... |
401 |
static public <C extends Collection<?>> boolean containsAny(final C coll1, final C coll2) {
|
405 |
static public <C extends Collection<?>> boolean containsAny(final C coll1, final C coll2) {
|
402 |
return !Collections.disjoint(coll1, coll2);
|
406 |
return !Collections.disjoint(coll1, coll2);
|
403 |
}
|
407 |
}
|
404 |
|
408 |
|
405 |
static public final boolean identityContains(final Collection<?> coll, final Object item) {
|
409 |
static public final boolean identityContains(final Collection<?> coll, final Object item) {
|
- |
|
410 |
final int size = coll.size();
|
- |
|
411 |
if (size > 0) {
|
- |
|
412 |
if (coll instanceof RandomAccess && coll instanceof List) {
|
- |
|
413 |
final List<?> list = (List<?>) coll;
|
- |
|
414 |
for (int i = 0; i < size; i++) {
|
- |
|
415 |
final Object v = list.get(i);
|
- |
|
416 |
if (item == v)
|
- |
|
417 |
return true;
|
- |
|
418 |
}
|
- |
|
419 |
} else {
|
406 |
for (final Object v : coll) {
|
420 |
for (final Object v : coll) {
|
407 |
if (item == v)
|
421 |
if (item == v)
|
408 |
return true;
|
422 |
return true;
|
409 |
}
|
423 |
}
|
- |
|
424 |
}
|
- |
|
425 |
}
|
410 |
return false;
|
426 |
return false;
|
411 |
}
|
427 |
}
|
412 |
|
428 |
|
- |
|
429 |
static public final boolean identityRemove(final Collection<?> coll, final Object item) {
|
- |
|
430 |
final int size = coll.size();
|
- |
|
431 |
if (size > 0) {
|
- |
|
432 |
if (coll instanceof RandomAccess && coll instanceof List) {
|
- |
|
433 |
final List<?> list = (List<?>) coll;
|
- |
|
434 |
for (int i = 0; i < size; i++) {
|
- |
|
435 |
final Object v = list.get(i);
|
- |
|
436 |
if (item == v) {
|
- |
|
437 |
list.remove(i);
|
- |
|
438 |
return true;
|
- |
|
439 |
}
|
- |
|
440 |
}
|
- |
|
441 |
} else {
|
- |
|
442 |
for (Iterator<?> i = coll.iterator(); i.hasNext();) {
|
- |
|
443 |
if (item == i.next()) {
|
- |
|
444 |
i.remove();
|
- |
|
445 |
return true;
|
- |
|
446 |
}
|
- |
|
447 |
}
|
- |
|
448 |
}
|
- |
|
449 |
}
|
- |
|
450 |
return false;
|
- |
|
451 |
}
|
- |
|
452 |
|
- |
|
453 |
static public final <T> int identityIndexOf(final List<T> coll, final T item) {
|
- |
|
454 |
return CustomEquals.indexOf(coll, item, CustomEquals.getIdentity());
|
- |
|
455 |
}
|
- |
|
456 |
|
413 |
static public final boolean identityEquals(final List<?> coll1, final List<?> coll2) {
|
457 |
static public final boolean identityEquals(final List<?> coll1, final List<?> coll2) {
|
414 |
if (coll1 == coll2)
|
458 |
if (coll1 == coll2)
|
415 |
return true;
|
459 |
return true;
|
416 |
final int size = coll1.size();
|
460 |
final int size = coll1.size();
|
417 |
if (size != coll2.size())
|
461 |
if (size != coll2.size())
|
Line 461... |
Line 505... |
461 |
public static <E> List<E> castList(final List<?> list, Class<E> c) throws ClassCastException {
|
505 |
public static <E> List<E> castList(final List<?> list, Class<E> c) throws ClassCastException {
|
462 |
if (list == null) {
|
506 |
if (list == null) {
|
463 |
return null;
|
507 |
return null;
|
464 |
}
|
508 |
}
|
465 |
|
509 |
|
- |
|
510 |
final List<E> result;
|
- |
|
511 |
if (list instanceof RandomAccess) {
|
466 |
final int size = list.size();
|
512 |
final int size = list.size();
|
467 |
final List<E> result = new ArrayList<E>(size);
|
513 |
result = new ArrayList<>(size);
|
468 |
for (int i = 0; i < list.size(); i++) {
|
514 |
for (int i = 0; i < size; i++) {
|
469 |
result.add(c.cast(list.get(i)));
|
515 |
result.add(c.cast(list.get(i)));
|
470 |
}
|
516 |
}
|
- |
|
517 |
} else {
|
- |
|
518 |
result = new LinkedList<>();
|
- |
|
519 |
for (final Object o : list) {
|
- |
|
520 |
result.add(c.cast(o));
|
- |
|
521 |
}
|
- |
|
522 |
}
|
471 |
return result;
|
523 |
return result;
|
472 |
}
|
524 |
}
|
473 |
|
525 |
|
474 |
/**
|
526 |
/**
|
475 |
* Cast Map
|
527 |
* Cast Map
|
Line 482... |
Line 534... |
482 |
* @return a new HashMap create from the <code>map</code> or null if <code>map</code> is null
|
534 |
* @return a new HashMap create from the <code>map</code> or null if <code>map</code> is null
|
483 |
* @throws ClassCastException if some item of <code>map</code> have a key which the type is not
|
535 |
* @throws ClassCastException if some item of <code>map</code> have a key which the type is not
|
484 |
* a <code>E</code> or a value which the type is not a <code>F</code>.
|
536 |
* a <code>E</code> or a value which the type is not a <code>F</code>.
|
485 |
*/
|
537 |
*/
|
486 |
public static <E, F> Map<E, F> castMap(final Map<?, ?> map, Class<E> cKey, Class<F> cValue) throws ClassCastException {
|
538 |
public static <E, F> Map<E, F> castMap(final Map<?, ?> map, Class<E> cKey, Class<F> cValue) throws ClassCastException {
|
- |
|
539 |
return castMap(map, cKey, cValue, HashMap::new);
|
- |
|
540 |
}
|
- |
|
541 |
|
- |
|
542 |
public static <E, F> Map<E, F> castMap(final Map<?, ?> map, Class<E> cKey, Class<F> cValue, final Supplier<Map<E, F>> ctor) throws ClassCastException {
|
487 |
if (map == null) {
|
543 |
if (map == null) {
|
488 |
return null;
|
544 |
return null;
|
489 |
}
|
545 |
}
|
490 |
|
546 |
|
491 |
final Map<E, F> result = new HashMap<E, F>();
|
547 |
final Map<E, F> result = ctor.get();
|
492 |
for (final Entry<?, ?> mapEntry : map.entrySet()) {
|
548 |
for (final Entry<?, ?> mapEntry : map.entrySet()) {
|
493 |
final E key;
|
549 |
final E key;
|
494 |
try {
|
550 |
try {
|
495 |
key = cKey.cast(mapEntry.getKey());
|
551 |
key = cKey.cast(mapEntry.getKey());
|
496 |
} catch (final ClassCastException ex) {
|
552 |
} catch (final ClassCastException ex) {
|
Line 505... |
Line 561... |
505 |
result.put(key, value);
|
561 |
result.put(key, value);
|
506 |
}
|
562 |
}
|
507 |
return result;
|
563 |
return result;
|
508 |
}
|
564 |
}
|
509 |
|
565 |
|
- |
|
566 |
public static <T> Stream<T> subclassFilter(final Collection<? super T> coll, final Class<T> subclass) {
|
- |
|
567 |
return coll.stream().filter(subclass::isInstance).map(subclass::cast);
|
- |
|
568 |
}
|
- |
|
569 |
|
- |
|
570 |
public static <T> List<T> subclassFilterToList(final Collection<? super T> coll, final Class<T> subclass) {
|
- |
|
571 |
return subclassFilter(coll, subclass).collect(Collectors.toList());
|
- |
|
572 |
}
|
- |
|
573 |
|
510 |
/**
|
574 |
/**
|
511 |
* The number of equals item between a and b, starting from the end.
|
575 |
* The number of equals item between a and b, starting from the end.
|
512 |
*
|
576 |
*
|
513 |
* @param <T> type of items.
|
577 |
* @param <T> type of items.
|
514 |
* @param a the first list, eg [a, b, c].
|
578 |
* @param a the first list, eg [a, b, c].
|
Line 946... |
Line 1010... |
946 |
private Object readResolve() {
|
1010 |
private Object readResolve() {
|
947 |
return EMPTY_SET;
|
1011 |
return EMPTY_SET;
|
948 |
}
|
1012 |
}
|
949 |
}
|
1013 |
}
|
950 |
|
1014 |
|
- |
|
1015 |
@SafeVarargs
|
- |
|
1016 |
public static final <T> List<T> toImmutableList(final T... items) {
|
- |
|
1017 |
final int length = items.length;
|
- |
|
1018 |
if (length == 0)
|
- |
|
1019 |
return Collections.emptyList();
|
- |
|
1020 |
else if (length == 1)
|
- |
|
1021 |
return Collections.singletonList(items[0]);
|
- |
|
1022 |
return Collections.unmodifiableList(Arrays.asList(Arrays.copyOf(items, length)));
|
- |
|
1023 |
}
|
- |
|
1024 |
|
951 |
public static final <T> List<T> toImmutableList(final Collection<? extends T> coll) {
|
1025 |
public static final <T> List<T> toImmutableList(final Collection<? extends T> coll) {
|
952 |
return toImmutableList(coll, ArrayList::new);
|
1026 |
return toImmutableList(coll, ArrayList::new);
|
953 |
}
|
1027 |
}
|
954 |
|
1028 |
|
955 |
public static final <T, C extends Collection<? extends T>> List<T> toImmutableList(final C coll, final Function<? super C, ? extends List<T>> createColl) {
|
1029 |
public static final <T, C extends Collection<? extends T>> List<T> toImmutableList(final C coll, final Function<? super C, ? extends List<T>> createColl) {
|
- |
|
1030 |
return toImmutableList(coll, createColl, true);
|
- |
|
1031 |
}
|
- |
|
1032 |
|
- |
|
1033 |
/**
|
- |
|
1034 |
* Create a new immutable list.
|
- |
|
1035 |
*
|
- |
|
1036 |
* @param <T> type of items
|
- |
|
1037 |
* @param <C> type of collection
|
- |
|
1038 |
* @param coll the collection holding the items.
|
- |
|
1039 |
* @param createColl how to copy the collection into a new list.
|
- |
|
1040 |
* @param std <code>true</code> if the created <code>createColl</code> use
|
- |
|
1041 |
* {@link Object#equals(Object)}, in that case {@link Collections#singletonList(Object)}
|
- |
|
1042 |
* can be returned.
|
- |
|
1043 |
* @return an immutable list with the same items as <code>coll</code>.
|
- |
|
1044 |
*/
|
- |
|
1045 |
public static final <T, C extends Collection<? extends T>> List<T> toImmutableList(final C coll, final Function<? super C, ? extends List<T>> createColl, final boolean std) {
|
- |
|
1046 |
final int size = coll.size();
|
956 |
if (coll.isEmpty())
|
1047 |
if (size == 0)
|
957 |
return Collections.emptyList();
|
1048 |
return Collections.emptyList();
|
- |
|
1049 |
else if (std && size == 1)
|
- |
|
1050 |
return Collections.singletonList(coll.iterator().next());
|
958 |
return Collections.unmodifiableList(createColl.apply(coll));
|
1051 |
return Collections.unmodifiableList(createColl.apply(coll));
|
959 |
}
|
1052 |
}
|
960 |
|
1053 |
|
961 |
public static final <T> Set<T> toImmutableSet(final Collection<T> coll) {
|
1054 |
public static final <T> Set<T> toImmutableSet(final Collection<T> coll) {
|
962 |
if (coll instanceof SortedSet) {
|
1055 |
if (coll instanceof SortedSet) {
|
Line 964... |
Line 1057... |
964 |
// ATTN see eclipse bug below about wrong constructor, we need SortedSet<T>, not
|
1057 |
// ATTN see eclipse bug below about wrong constructor, we need SortedSet<T>, not
|
965 |
// SortedSet<? extends T>, otherwise "Open Declaration" will match to TreeSet(SortedSet)
|
1058 |
// SortedSet<? extends T>, otherwise "Open Declaration" will match to TreeSet(SortedSet)
|
966 |
// but not at runtime.
|
1059 |
// but not at runtime.
|
967 |
return toImmutableSet((SortedSet<T>) coll, TreeSet::new);
|
1060 |
return toImmutableSet((SortedSet<T>) coll, TreeSet::new);
|
968 |
} else if (coll instanceof IdentitySet) {
|
1061 |
} else if (coll instanceof IdentitySet) {
|
969 |
return toImmutableSet((IdentitySet<? extends T>) coll, LinkedIdentitySet::new);
|
1062 |
return toImmutableSet((IdentitySet<? extends T>) coll, LinkedIdentitySet::new, false);
|
970 |
} else {
|
1063 |
} else {
|
971 |
// In doubt, keep order
|
1064 |
// In doubt, keep order
|
972 |
// ATTN LinkedHashSet extends HashSet
|
1065 |
// ATTN LinkedHashSet extends HashSet
|
973 |
return toImmutableSet(coll, coll.getClass() == HashSet.class ? HashSet::new : LinkedHashSet::new);
|
1066 |
return toImmutableSet(coll, coll.getClass() == HashSet.class ? HashSet::new : LinkedHashSet::new);
|
974 |
}
|
1067 |
}
|
975 |
}
|
1068 |
}
|
976 |
|
1069 |
|
977 |
public static final <T, C extends Collection<? extends T>> Set<T> toImmutableSet(final C coll, final Function<? super C, ? extends Set<T>> createColl) {
|
1070 |
public static final <T, C extends Collection<? extends T>> Set<T> toImmutableSet(final C coll, final Function<? super C, ? extends Set<T>> createColl) {
|
- |
|
1071 |
return toImmutableSet(coll, createColl, true);
|
- |
|
1072 |
|
- |
|
1073 |
}
|
- |
|
1074 |
|
- |
|
1075 |
/**
|
- |
|
1076 |
* Create a new immutable set.
|
- |
|
1077 |
*
|
- |
|
1078 |
* @param <T> type of items
|
- |
|
1079 |
* @param <C> type of collection
|
- |
|
1080 |
* @param coll the collection holding the items.
|
- |
|
1081 |
* @param createColl how to copy the collection into a new list.
|
- |
|
1082 |
* @param std <code>true</code> if the created <code>createColl</code> use
|
- |
|
1083 |
* {@link Object#equals(Object)}, in that case {@link Collections#singleton(Object)} can
|
- |
|
1084 |
* be returned.
|
- |
|
1085 |
* @return an immutable set with the same items as <code>coll</code>.
|
- |
|
1086 |
*/
|
- |
|
1087 |
public static final <T, C extends Collection<? extends T>> Set<T> toImmutableSet(final C coll, final Function<? super C, ? extends Set<T>> createColl, final boolean std) {
|
- |
|
1088 |
final int size = coll.size();
|
978 |
if (coll.isEmpty())
|
1089 |
if (size == 0)
|
979 |
return Collections.emptySet();
|
1090 |
return Collections.emptySet();
|
- |
|
1091 |
else if (std && size == 1)
|
- |
|
1092 |
return Collections.singleton(coll.iterator().next());
|
980 |
final Set<T> res = createColl.apply(coll);
|
1093 |
final Set<T> res = createColl.apply(coll);
|
981 |
return Collections.unmodifiableSet(res);
|
1094 |
return Collections.unmodifiableSet(res);
|
982 |
}
|
1095 |
}
|
983 |
|
1096 |
|
984 |
/**
|
1097 |
/**
|
Line 993... |
Line 1106... |
993 |
if (map instanceof SortedMap) {
|
1106 |
if (map instanceof SortedMap) {
|
994 |
// force TreeMap(SortedMap) to keep Comparator
|
1107 |
// force TreeMap(SortedMap) to keep Comparator
|
995 |
// ATTN see eclipse bug below about wrong constructor
|
1108 |
// ATTN see eclipse bug below about wrong constructor
|
996 |
return toImmutableMap((SortedMap<K, ? extends V>) map, TreeMap::new);
|
1109 |
return toImmutableMap((SortedMap<K, ? extends V>) map, TreeMap::new);
|
997 |
} else if (map instanceof IdentityHashMap) {
|
1110 |
} else if (map instanceof IdentityHashMap) {
|
998 |
return toImmutableMap((IdentityHashMap<? extends K, ? extends V>) map, IdentityHashMap::new);
|
1111 |
return toImmutableMap((IdentityHashMap<? extends K, ? extends V>) map, IdentityHashMap::new, false);
|
999 |
} else {
|
1112 |
} else {
|
1000 |
// In doubt, keep order
|
1113 |
// In doubt, keep order
|
1001 |
// ATTN LinkedHashMap extends HashMap
|
1114 |
// ATTN LinkedHashMap extends HashMap
|
1002 |
return toImmutableMap(map, map.getClass() == HashMap.class ? HashMap::new : LinkedHashMap::new);
|
1115 |
return toImmutableMap(map, map.getClass() == HashMap.class ? HashMap::new : LinkedHashMap::new);
|
1003 |
}
|
1116 |
}
|
1004 |
}
|
1117 |
}
|
1005 |
|
1118 |
|
- |
|
1119 |
public static final <K, V, InMap extends Map<? extends K, ? extends V>> Map<K, V> toImmutableMap(final InMap map, final Function<? super InMap, ? extends Map<K, V>> copyFunction) {
|
- |
|
1120 |
return toImmutableMap(map, copyFunction, true);
|
- |
|
1121 |
}
|
- |
|
1122 |
|
1006 |
/**
|
1123 |
/**
|
1007 |
* Return an immutable map with the same entries as the passed one. NOTE: <code>copyMap</code>
|
1124 |
* Return an immutable map with the same entries as the passed one. NOTE: <code>copyMap</code>
|
1008 |
* <strong>must</strong> copy the entries so that a modification of <code>map</code> doesn't
|
1125 |
* <strong>must</strong> copy the entries so that a modification of <code>map</code> doesn't
|
1009 |
* affect the copy.
|
1126 |
* affect the copy.
|
1010 |
*
|
1127 |
*
|
Line 1014... |
Line 1131... |
1014 |
* matches <code>TreeMap::new</code> to {@link TreeMap#TreeMap(SortedMap)} ignoring
|
1131 |
* matches <code>TreeMap::new</code> to {@link TreeMap#TreeMap(SortedMap)} ignoring
|
1015 |
* generic type (i.e. it is declared with "K" but we pass "? extends K") but
|
1132 |
* generic type (i.e. it is declared with "K" but we pass "? extends K") but
|
1016 |
* {@link TreeMap#TreeMap(Map)} is (correctly) executed at runtime.
|
1133 |
* {@link TreeMap#TreeMap(Map)} is (correctly) executed at runtime.
|
1017 |
* @param map the map.
|
1134 |
* @param map the map.
|
1018 |
* @param copyFunction how to copy the passed map.
|
1135 |
* @param copyFunction how to copy the passed map.
|
- |
|
1136 |
* @param std <code>true</code> if the created <code>createColl</code> use
|
- |
|
1137 |
* {@link Object#equals(Object)}, in that case
|
- |
|
1138 |
* {@link Collections#singletonMap(Object, Object)} can be returned.
|
1019 |
* @return an immutable map.
|
1139 |
* @return an immutable map.
|
1020 |
*/
|
1140 |
*/
|
1021 |
public static final <K, V, InMap extends Map<? extends K, ? extends V>> Map<K, V> toImmutableMap(final InMap map, final Function<? super InMap, ? extends Map<K, V>> copyFunction) {
|
1141 |
public static final <K, V, InMap extends Map<? extends K, ? extends V>> Map<K, V> toImmutableMap(final InMap map, final Function<? super InMap, ? extends Map<K, V>> copyFunction,
|
- |
|
1142 |
final boolean std) {
|
- |
|
1143 |
final int size = map.size();
|
1022 |
if (map.isEmpty())
|
1144 |
if (size == 0) {
|
1023 |
return Collections.emptyMap();
|
1145 |
return Collections.emptyMap();
|
- |
|
1146 |
} else if (std && size == 1) {
|
- |
|
1147 |
final Entry<? extends K, ? extends V> e = map.entrySet().iterator().next();
|
- |
|
1148 |
return Collections.singletonMap(e.getKey(), e.getValue());
|
- |
|
1149 |
}
|
1024 |
return Collections.unmodifiableMap(copyFunction.apply(map));
|
1150 |
return Collections.unmodifiableMap(copyFunction.apply(map));
|
1025 |
}
|
1151 |
}
|
1026 |
|
1152 |
|
1027 |
public static <K, V> Map<K, V> createMap(K key, V val, K key2, V val2) {
|
1153 |
public static <K, V> Map<K, V> createMap(K key, V val, K key2, V val2) {
|
1028 |
// arguments are ordered, so should the result
|
1154 |
// arguments are ordered, so should the result
|
Line 1053... |
Line 1179... |
1053 |
res.put(keyAndVal.get(i), keyAndVal.get(i + 1));
|
1179 |
res.put(keyAndVal.get(i), keyAndVal.get(i + 1));
|
1054 |
}
|
1180 |
}
|
1055 |
return res;
|
1181 |
return res;
|
1056 |
}
|
1182 |
}
|
1057 |
|
1183 |
|
- |
|
1184 |
// same as HashMap and HashSet
|
- |
|
1185 |
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
|
- |
|
1186 |
|
- |
|
1187 |
public static interface MapConstructor<K, V, M extends Map<K, V>> {
|
- |
|
1188 |
M create(int initialCapacity, float loadFactor);
|
- |
|
1189 |
}
|
- |
|
1190 |
|
- |
|
1191 |
public static final int getCapacity(final int plannedSize, final float loadFactor) {
|
- |
|
1192 |
return Math.max((int) Math.ceil(plannedSize / loadFactor), 4);
|
- |
|
1193 |
}
|
- |
|
1194 |
|
- |
|
1195 |
public static final <K, V> LinkedHashMap<K, V> newLinkedHashMap(final int plannedSize) {
|
- |
|
1196 |
return newMap(plannedSize, DEFAULT_LOAD_FACTOR, (MapConstructor<K, V, LinkedHashMap<K, V>>) LinkedHashMap::new);
|
- |
|
1197 |
}
|
- |
|
1198 |
|
- |
|
1199 |
public static final <K, V> HashMap<K, V> newHashMap(final int plannedSize) {
|
- |
|
1200 |
return newHashMap(plannedSize, DEFAULT_LOAD_FACTOR);
|
- |
|
1201 |
}
|
- |
|
1202 |
|
- |
|
1203 |
/**
|
- |
|
1204 |
* Create a new map with no rehash operations until at least a certain size.
|
- |
|
1205 |
*
|
- |
|
1206 |
* @param <K> the type of keys maintained by the new map
|
- |
|
1207 |
* @param <V> the type of mapped values
|
- |
|
1208 |
* @param plannedSize the number of entries expected to be added to the returned map.
|
- |
|
1209 |
* @param loadFactor the load factor of the returned map, not advised to be <code>1.0f</code>
|
- |
|
1210 |
* because it increases the chances of hash collisions.
|
- |
|
1211 |
* @return a new map that won't rehash until at least <code>plannedSize</code> items.
|
- |
|
1212 |
*/
|
- |
|
1213 |
public static final <K, V> HashMap<K, V> newHashMap(final int plannedSize, final float loadFactor) {
|
- |
|
1214 |
return newMap(plannedSize, loadFactor, (MapConstructor<K, V, HashMap<K, V>>) HashMap::new);
|
- |
|
1215 |
}
|
- |
|
1216 |
|
- |
|
1217 |
/**
|
- |
|
1218 |
* Create a new map with no rehash operations until at least a certain size.
|
- |
|
1219 |
*
|
- |
|
1220 |
* @param <K> the type of keys maintained by the new map
|
- |
|
1221 |
* @param <V> the type of mapped values
|
- |
|
1222 |
* @param <M> the type of the new map
|
- |
|
1223 |
* @param plannedSize the number of entries expected to be added to the returned map
|
- |
|
1224 |
* @param loadFactor the load factor, passed to <code>ctor</code>.
|
- |
|
1225 |
* @param ctor how to create the new map, will be passed the computed initial capacity and
|
- |
|
1226 |
* <code>loadFactor</code>, e.g. {@link LinkedHashMap#LinkedHashMap(int, float)
|
- |
|
1227 |
* LinkedHashMap::new}.
|
- |
|
1228 |
* @return a new map that won't rehash until at least <code>plannedSize</code> items.
|
- |
|
1229 |
*/
|
- |
|
1230 |
public static final <K, V, M extends Map<K, V>> M newMap(final int plannedSize, final float loadFactor, final MapConstructor<K, V, M> ctor) {
|
- |
|
1231 |
return ctor.create(getCapacity(plannedSize, loadFactor), loadFactor);
|
- |
|
1232 |
}
|
- |
|
1233 |
|
- |
|
1234 |
public static interface SetConstructor<V, S extends Set<V>> {
|
- |
|
1235 |
S create(int initialCapacity, float loadFactor);
|
- |
|
1236 |
}
|
- |
|
1237 |
|
- |
|
1238 |
public static final <V> LinkedHashSet<V> newLinkedHashSet(final int plannedSize) {
|
- |
|
1239 |
return newSet(plannedSize, DEFAULT_LOAD_FACTOR, (SetConstructor<V, LinkedHashSet<V>>) LinkedHashSet::new);
|
- |
|
1240 |
}
|
- |
|
1241 |
|
- |
|
1242 |
public static final <V> HashSet<V> newHashSet(final int plannedSize) {
|
- |
|
1243 |
return newHashSet(plannedSize, DEFAULT_LOAD_FACTOR);
|
- |
|
1244 |
}
|
- |
|
1245 |
|
- |
|
1246 |
public static final <V> HashSet<V> newHashSet(final int plannedSize, final float loadFactor) {
|
- |
|
1247 |
return newSet(plannedSize, loadFactor, (SetConstructor<V, HashSet<V>>) HashSet::new);
|
- |
|
1248 |
}
|
- |
|
1249 |
|
- |
|
1250 |
public static final <V, S extends Set<V>> S newSet(final int plannedSize, final float loadFactor, final SetConstructor<V, S> ctor) {
|
- |
|
1251 |
return ctor.create(getCapacity(plannedSize, loadFactor), loadFactor);
|
- |
|
1252 |
}
|
- |
|
1253 |
|
1058 |
/**
|
1254 |
/**
|
1059 |
* Creates a map with null values.
|
1255 |
* Creates a map with null values.
|
1060 |
*
|
1256 |
*
|
1061 |
* @param <K> type of key.
|
1257 |
* @param <K> type of key.
|
1062 |
* @param <V> type of value.
|
1258 |
* @param <V> type of value.
|