|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-2025 OnixByte. |
| 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 | + * |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.onixbyte.guid.impl; |
| 19 | + |
| 20 | +import com.onixbyte.guid.GuidCreator; |
| 21 | + |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import java.security.SecureRandom; |
| 24 | +import java.util.UUID; |
| 25 | + |
| 26 | +/** |
| 27 | + * A {@code SequentialUuidCreator} is responsible for generating UUIDs following the UUID version 7 specification, which |
| 28 | + * combines a timestamp with random bytes to create time-ordered unique identifiers. |
| 29 | + * <p> |
| 30 | + * This implementation utilises a cryptographically strong {@link SecureRandom} instance to produce the random |
| 31 | + * component of the UUID. The first 6 bytes of the UUID encode the current timestamp in milliseconds, ensuring that |
| 32 | + * generated UUIDs are roughly ordered by creation time. |
| 33 | + * <p> |
| 34 | + * The generated UUID adheres strictly to the layout and variant bits of UUID version 7 as defined in the specification. |
| 35 | + * </p> |
| 36 | + * |
| 37 | + * @implNote This class implements the {@link GuidCreator} interface, providing UUID instances as unique identifiers. |
| 38 | + */ |
| 39 | +public class SequentialUuidCreator implements GuidCreator<UUID> { |
| 40 | + |
| 41 | + private final SecureRandom random; |
| 42 | + |
| 43 | + /** |
| 44 | + * Constructs a new {@code SequentialUuidCreator} with its own {@link SecureRandom} instance. |
| 45 | + */ |
| 46 | + public SequentialUuidCreator() { |
| 47 | + this.random = new SecureRandom(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Generates and returns the next UUID version 7 identifier. |
| 52 | + * |
| 53 | + * @return a {@link UUID} instance representing a unique, time-ordered identifier |
| 54 | + */ |
| 55 | + @Override |
| 56 | + public UUID nextId() { |
| 57 | + var value = randomBytes(); |
| 58 | + var buf = ByteBuffer.wrap(value); |
| 59 | + var high = buf.getLong(); |
| 60 | + var low = buf.getLong(); |
| 61 | + return new UUID(high, low); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Produces a byte array representation of a UUID version 7, |
| 66 | + * combining the current timestamp with random bytes. |
| 67 | + * |
| 68 | + * @return a 16-byte array conforming to UUIDv7 layout and variant bits |
| 69 | + */ |
| 70 | + private byte[] randomBytes() { |
| 71 | + var value = new byte[16]; |
| 72 | + random.nextBytes(value); |
| 73 | + |
| 74 | + var timestamp = ByteBuffer.allocate(Long.BYTES); |
| 75 | + timestamp.putLong(System.currentTimeMillis()); |
| 76 | + |
| 77 | + System.arraycopy(timestamp.array(), 2, value, 0, 6); |
| 78 | + |
| 79 | + // Set version to 7 (UUIDv7) |
| 80 | + value[6] = (byte) ((value[6] & 0x0F) | 0x70); |
| 81 | + |
| 82 | + // Set variant bits as per RFC 4122 |
| 83 | + value[8] = (byte) ((value[8] & 0x3F) | 0x80); |
| 84 | + |
| 85 | + return value; |
| 86 | + } |
| 87 | +} |
0 commit comments