|
| 1 | +/* |
| 2 | + * Copyright (C) 2002-2023 Sebastiano Vigna |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +package PACKAGE; |
| 19 | + |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.stream.Collector; |
| 24 | + |
| 25 | +import static java.util.stream.Collectors.collectingAndThen; |
| 26 | +import static java.util.stream.Collectors.toCollection; |
| 27 | +import static java.util.stream.Collectors.toList; |
| 28 | + |
| 29 | +/** |
| 30 | + * Make an unmodifiable set feel like a Guava immutable set. |
| 31 | + * |
| 32 | + * <p>Mostly this means copy-on-construction and exposing an explicit immutable type. |
| 33 | + * Note that unlike the Guava versions, we don't guarantee anything about the iteration order. |
| 34 | + */ |
| 35 | +@SuppressWarnings({"unused", "unchecked"}) |
| 36 | +public class IMMUTABLE_SET KEY_GENERIC extends SETS.UnmodifiableSet KEY_GENERIC { |
| 37 | + |
| 38 | + private static final long serialVersionUID = 0L; |
| 39 | + |
| 40 | + @SuppressWarnings("rawtypes") |
| 41 | + private static final IMMUTABLE_SET EMPTY = |
| 42 | + new IMMUTABLE_SET(SETS.emptySet()); |
| 43 | + |
| 44 | + private IMMUTABLE_SET(SET KEY_GENERIC set) { |
| 45 | + super(set); |
| 46 | + } |
| 47 | + |
| 48 | + public static KEY_GENERIC IMMUTABLE_SET KEY_GENERIC of() { |
| 49 | + return EMPTY; |
| 50 | + } |
| 51 | + |
| 52 | + public static KEY_GENERIC IMMUTABLE_SET KEY_GENERIC of(KEY_GENERIC_TYPE key) { |
| 53 | + return new IMMUTABLE_SET KEY_GENERIC(SETS.singleton(key)); |
| 54 | + } |
| 55 | + |
| 56 | + public static KEY_GENERIC IMMUTABLE_SET KEY_GENERIC of(KEY_GENERIC_TYPE... keys) { |
| 57 | + SET KEY_GENERIC set = new OPEN_HASH_SET KEY_GENERIC (keys.length); |
| 58 | + for (KEY_GENERIC_TYPE key : keys) { |
| 59 | + set.add(key); |
| 60 | + } |
| 61 | + return new IMMUTABLE_SET KEY_GENERIC(set); |
| 62 | + } |
| 63 | + |
| 64 | + @SuppressWarnings("deprecation") |
| 65 | + public static KEY_GENERIC IMMUTABLE_SET KEY_GENERIC copyOf(Iterable<? extends KEY_GENERIC_CLASS> iter) { |
| 66 | + if ((iter instanceof IMMUTABLE_SET)) { |
| 67 | + return (IMMUTABLE_SET KEY_GENERIC) iter; |
| 68 | + } |
| 69 | + Collection<KEY_GENERIC_CLASS> col; |
| 70 | + if (iter instanceof Collection) { |
| 71 | + col = (Collection<KEY_GENERIC_CLASS>) iter; |
| 72 | + } else { |
| 73 | + col = new ArrayList<KEY_GENERIC_CLASS>(); |
| 74 | + iter.forEach(col::add); |
| 75 | + } |
| 76 | + if (col.isEmpty()) { |
| 77 | + return of(); |
| 78 | + } |
| 79 | + if (col.size() == 1) { |
| 80 | + return of(col.iterator().next()); |
| 81 | + } |
| 82 | + if ((col instanceof SETS.UnmodifiableSet)) { |
| 83 | + return new IMMUTABLE_SET KEY_GENERIC((SET) col); |
| 84 | + } |
| 85 | + if (col instanceof COLLECTION) { |
| 86 | + return new IMMUTABLE_SET KEY_GENERIC(new OPEN_HASH_SET KEY_GENERIC((COLLECTION KEY_GENERIC) col)); |
| 87 | + } |
| 88 | + return new IMMUTABLE_SET KEY_GENERIC (new OPEN_HASH_SET KEY_GENERIC(col)); |
| 89 | + } |
| 90 | + |
| 91 | + public static KEY_GENERIC IMMUTABLE_SET KEY_GENERIC copyOf(KEY_GENERIC_TYPE[] arr) { |
| 92 | + return arr.length == 0 |
| 93 | + ? of() |
| 94 | + : arr.length == 1 |
| 95 | + ? of(arr[0]) |
| 96 | + : new IMMUTABLE_SET KEY_GENERIC(new OPEN_HASH_SET KEY_GENERIC(arr)); |
| 97 | + } |
| 98 | + |
| 99 | + public static KEY_GENERIC Collector<KEY_GENERIC_CLASS, ?, IMMUTABLE_SET KEY_GENERIC> TO_IMMUTABLE_SET() { |
| 100 | + return collectingAndThen( |
| 101 | + toCollection(OPEN_HASH_SET::new), IMMUTABLE_SET::new); |
| 102 | + } |
| 103 | + |
| 104 | + public static class Builder KEY_GENERIC { |
| 105 | + |
| 106 | + private OPEN_HASH_SET KEY_GENERIC temp; |
| 107 | + |
| 108 | + public Builder() { |
| 109 | + temp = new OPEN_HASH_SET KEY_GENERIC(); |
| 110 | + } |
| 111 | + |
| 112 | + public Builder(int initialCapacity) { |
| 113 | + temp = new OPEN_HASH_SET KEY_GENERIC(initialCapacity); |
| 114 | + } |
| 115 | + |
| 116 | + public Builder KEY_GENERIC add(KEY_GENERIC_TYPE... keys) { |
| 117 | + for (KEY_GENERIC_TYPE key : keys) { |
| 118 | + temp.add(key); |
| 119 | + } |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + @SuppressWarnings("deprecation") |
| 124 | + public Builder KEY_GENERIC addAll(Iterable<? extends KEY_GENERIC_CLASS> iter) { |
| 125 | +#if KEYS_PRIMITIVE |
| 126 | + if (iter instanceof COLLECTION) { |
| 127 | + return add(((COLLECTION KEY_GENERIC) iter).TO_KEY_ARRAY()); |
| 128 | + } |
| 129 | +#endif |
| 130 | + if (iter instanceof Collection) { |
| 131 | + temp.addAll((Collection<? extends KEY_GENERIC_CLASS>) iter); |
| 132 | + } else { |
| 133 | + iter.forEach(temp::add); |
| 134 | + } |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + public Builder KEY_GENERIC combine(Builder KEY_GENERIC other) { |
| 139 | + return addAll(other.temp); |
| 140 | + } |
| 141 | + |
| 142 | + public IMMUTABLE_SET KEY_GENERIC build() { |
| 143 | + OPEN_HASH_SET KEY_GENERIC built = temp; |
| 144 | + temp = null; |
| 145 | + built.trim(); |
| 146 | + return new IMMUTABLE_SET KEY_GENERIC(built); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments