|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package jdk.internal.util; |
| 27 | + |
| 28 | +import jdk.internal.ValueBased; |
| 29 | +import jdk.internal.vm.annotation.Stable; |
| 30 | + |
| 31 | +import java.util.BitSet; |
| 32 | +import java.util.function.IntPredicate; |
| 33 | + |
| 34 | +/** |
| 35 | + * Class for working with immutable BitSets. |
| 36 | + */ |
| 37 | +@ValueBased |
| 38 | +public class ImmutableBitSetPredicate implements IntPredicate { |
| 39 | + |
| 40 | + @Stable |
| 41 | + private final long[] words; |
| 42 | + |
| 43 | + private ImmutableBitSetPredicate(BitSet original) { |
| 44 | + // If this class is made public, we need to do |
| 45 | + // a defensive array copy as certain BitSet implementations |
| 46 | + // may return a shared array. The spec says the array should be _new_ though but |
| 47 | + // the consequences might be unspecified for a malicious BitSet. |
| 48 | + this.words = original.toLongArray(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean test(int bitIndex) { |
| 53 | + if (bitIndex < 0) |
| 54 | + throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex); |
| 55 | + |
| 56 | + int wordIndex = wordIndex(bitIndex); |
| 57 | + return (wordIndex < words.length) |
| 58 | + && ((words[wordIndex] & (1L << bitIndex)) != 0); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Given a bit index, return word index containing it. |
| 63 | + */ |
| 64 | + private static int wordIndex(int bitIndex) { |
| 65 | + return bitIndex >> 6; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@return a new {@link IntPredicate } representing the {@link BitSet#get(int)} method applied |
| 70 | + * on an immutable snapshot of the current state of this BitSet}. |
| 71 | + * <p> |
| 72 | + * If the returned predicate is invoked with a {@code bitIndex} that is negative, the predicate |
| 73 | + * will throw an IndexOutOfBoundsException just as the {@link BitSet#get(int)} method would. |
| 74 | + * <p> |
| 75 | + * Returned predicates are threadsafe and can be used without external synchronisation. |
| 76 | + * |
| 77 | + * @implNote The method is free to return a {@link ValueBased} implementation. |
| 78 | + * |
| 79 | + * @since 22 |
| 80 | + */ |
| 81 | + public static IntPredicate of(BitSet original) { |
| 82 | + return new ImmutableBitSetPredicate(original); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments