|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.typedb.common.collection; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Collection; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.HashSet; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.NavigableSet; |
| 17 | +import java.util.Objects; |
| 18 | +import java.util.Set; |
| 19 | + |
| 20 | +public class Collections { |
| 21 | + |
| 22 | + @SafeVarargs |
| 23 | + public static <K, V> Map<K, V> map(Pair<K, V>... pairs) { |
| 24 | + Map<K, V> map = new HashMap<>(); |
| 25 | + for (Pair<K, V> tuple : pairs) { |
| 26 | + map.put(tuple.first(), tuple.second()); |
| 27 | + } |
| 28 | + return java.util.Collections.unmodifiableMap(map); |
| 29 | + } |
| 30 | + |
| 31 | + public static <K, V> Map<K, V> map(Map<K, V> map) { |
| 32 | + return java.util.Collections.unmodifiableMap(map); |
| 33 | + } |
| 34 | + |
| 35 | + @SafeVarargs |
| 36 | + public static <T> Set<T> set(T... items) { |
| 37 | + return set(Arrays.asList(items)); |
| 38 | + } |
| 39 | + |
| 40 | + public static <T> Set<T> set(Collection<T> collection) { |
| 41 | + Set<T> set = new HashSet<>(collection); |
| 42 | + return java.util.Collections.unmodifiableSet(set); |
| 43 | + } |
| 44 | + |
| 45 | + @SafeVarargs |
| 46 | + public static <T> Set<T> set(Collection<T> collection, T item, T... items) { |
| 47 | + Set<T> combined = new HashSet<>(collection); |
| 48 | + combined.add(item); |
| 49 | + combined.addAll(Arrays.asList(items)); |
| 50 | + return java.util.Collections.unmodifiableSet(combined); |
| 51 | + } |
| 52 | + |
| 53 | + @SafeVarargs |
| 54 | + public static <T> Set<T> concatToSet(Collection<? extends T> collection, Collection<? extends T>... collections) { |
| 55 | + Set<T> combined = new HashSet<>(collection); |
| 56 | + for (Collection<? extends T> c : collections) combined.addAll(c); |
| 57 | + return java.util.Collections.unmodifiableSet(combined); |
| 58 | + } |
| 59 | + |
| 60 | + @SafeVarargs |
| 61 | + public static <T> List<T> list(T... items) { |
| 62 | + return java.util.Collections.unmodifiableList(Arrays.asList(items)); |
| 63 | + } |
| 64 | + |
| 65 | + public static <T> List<T> list(Collection<T> collection) { |
| 66 | + List<T> list = new ArrayList<>(collection); |
| 67 | + return java.util.Collections.unmodifiableList(list); |
| 68 | + } |
| 69 | + |
| 70 | + @SafeVarargs |
| 71 | + public static <T> List<T> list(Collection<T> collection, T item, T... array) { |
| 72 | + List<T> combined = new ArrayList<>(collection); |
| 73 | + combined.add(item); |
| 74 | + combined.addAll(Arrays.asList(array)); |
| 75 | + return java.util.Collections.unmodifiableList(combined); |
| 76 | + } |
| 77 | + |
| 78 | + @SafeVarargs |
| 79 | + public static <T> List<T> concatToList(Collection<? extends T> collection, Collection<? extends T>... collections) { |
| 80 | + List<T> combined = new ArrayList<>(collection); |
| 81 | + for (Collection<? extends T> c : collections) combined.addAll(c); |
| 82 | + return java.util.Collections.unmodifiableList(combined); |
| 83 | + } |
| 84 | + |
| 85 | + public static <A, B> Pair<A, B> pair(A first, B second) { |
| 86 | + return new Pair<>(first, second); |
| 87 | + } |
| 88 | + |
| 89 | + public static <A, B, C> Triple<A, B, C> triple(A first, B second, C third) { |
| 90 | + return new Triple<>(first, second, third); |
| 91 | + } |
| 92 | + |
| 93 | + @SafeVarargs |
| 94 | + public static <T> boolean containsAll(Collection<T> collection, T... values) { |
| 95 | + for (T value : values) { |
| 96 | + if (!collection.contains(value)) return false; |
| 97 | + } |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + public static <T> boolean arrayContains(T[] values, T value) { |
| 102 | + for (final T v : values) { |
| 103 | + if (Objects.equals(value, v)) return true; |
| 104 | + } |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + public static <T> Set<T> intersection(Set<T> set1, Set<T> set2) { |
| 109 | + Set<T> minSet; |
| 110 | + Set<T> maxSet; |
| 111 | + if (set1.size() < set2.size()) { |
| 112 | + minSet = set1; |
| 113 | + maxSet = set2; |
| 114 | + } else { |
| 115 | + minSet = set2; |
| 116 | + maxSet = set1; |
| 117 | + } |
| 118 | + Set<T> intersection = new HashSet<>(); |
| 119 | + for (T elem : minSet) { |
| 120 | + if (maxSet.contains(elem)) intersection.add(elem); |
| 121 | + } |
| 122 | + return intersection; |
| 123 | + } |
| 124 | + |
| 125 | + public static <T> boolean hasIntersection(Set<T> set1, Set<T> set2) { |
| 126 | + Set<T> minSet; |
| 127 | + Set<T> maxSet; |
| 128 | + if (set1.size() < set2.size()) { |
| 129 | + minSet = set1; |
| 130 | + maxSet = set2; |
| 131 | + } else { |
| 132 | + minSet = set2; |
| 133 | + maxSet = set1; |
| 134 | + } |
| 135 | + for (T elem : minSet) { |
| 136 | + if (maxSet.contains(elem)) return true; |
| 137 | + } |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Optimised set intersection detection when using sorted sets |
| 143 | + */ |
| 144 | + public static <T extends Comparable<T>> boolean hasIntersection(NavigableSet<T> set1, NavigableSet<T> set2) { |
| 145 | + NavigableSet<T> active = set1; |
| 146 | + NavigableSet<T> other = set2; |
| 147 | + if (active.isEmpty()) return false; |
| 148 | + T currentKey = active.first(); |
| 149 | + while (currentKey != null) { |
| 150 | + T otherKey = other.ceiling(currentKey); |
| 151 | + if (otherKey != null && otherKey.equals(currentKey)) return true; |
| 152 | + currentKey = otherKey; |
| 153 | + NavigableSet<T> tmp = other; |
| 154 | + other = active; |
| 155 | + active = tmp; |
| 156 | + } |
| 157 | + return false; |
| 158 | + } |
| 159 | +} |
0 commit comments