|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 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 | +package org.bson; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +import static org.bson.assertions.Assertions.assertNotNull; |
| 23 | + |
| 24 | +/** |
| 25 | + * Represents a packed bit vector, where each element of the vector is represented by a single bit (0 or 1). |
| 26 | + * <p> |
| 27 | + * The {@link PackedBitVector} is used to store data efficiently using the BSON Binary Subtype 9 format. |
| 28 | + * |
| 29 | + * @mongodb.server.release 6.0 |
| 30 | + * @see Vector#packedBitVector(byte[], byte) |
| 31 | + * @see BsonBinary#BsonBinary(Vector) |
| 32 | + * @see BsonBinary#asVector() |
| 33 | + * @since 5.3 |
| 34 | + */ |
| 35 | +public final class PackedBitVector extends Vector { |
| 36 | + |
| 37 | + private final byte padding; |
| 38 | + private final byte[] data; |
| 39 | + |
| 40 | + PackedBitVector(final byte[] data, final byte padding) { |
| 41 | + super(DataType.PACKED_BIT); |
| 42 | + this.data = assertNotNull(data); |
| 43 | + this.padding = padding; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Retrieve the underlying byte array representing this {@link PackedBitVector} vector, where |
| 48 | + * each bit represents an element of the vector (either 0 or 1). |
| 49 | + * <p> |
| 50 | + * Note that the {@linkplain #getPadding() padding value} should be considered when interpreting the final byte of the array, |
| 51 | + * as it indicates how many least-significant bits are to be ignored. |
| 52 | + * |
| 53 | + * @return the underlying byte array representing this {@link PackedBitVector} vector. |
| 54 | + * @see #getPadding() |
| 55 | + */ |
| 56 | + public byte[] getData() { |
| 57 | + return assertNotNull(data); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns the padding value for this vector. |
| 62 | + * |
| 63 | + * <p>Padding refers to the number of least-significant bits in the final byte that are ignored when retrieving |
| 64 | + * {@linkplain #getData() the vector array}. For instance, if the padding value is 3, this means that the last byte contains |
| 65 | + * 3 least-significant unused bits, which should be disregarded during operations.</p> |
| 66 | + * <p> |
| 67 | + * |
| 68 | + * NOTE: The underlying byte array is not copied; changes to the returned array will be reflected in this instance. |
| 69 | + * |
| 70 | + * @return the padding value (between 0 and 7). |
| 71 | + */ |
| 72 | + public byte getPadding() { |
| 73 | + return this.padding; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean equals(final Object o) { |
| 78 | + if (this == o) { |
| 79 | + return true; |
| 80 | + } |
| 81 | + if (o == null || getClass() != o.getClass()) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + PackedBitVector that = (PackedBitVector) o; |
| 85 | + return padding == that.padding && Arrays.equals(data, that.data); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public int hashCode() { |
| 90 | + return Objects.hash(padding, Arrays.hashCode(data)); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public String toString() { |
| 95 | + return "PackedBitVector{" |
| 96 | + + "padding=" + padding |
| 97 | + + ", data=" + Arrays.toString(data) |
| 98 | + + ", dataType=" + getDataType() |
| 99 | + + '}'; |
| 100 | + } |
| 101 | +} |
0 commit comments