1 package org
.argeo
.api
.uuid
;
3 import java
.security
.SecureRandom
;
4 import java
.util
.function
.Supplier
;
6 /** A factory for node id base */
7 public interface NodeIdSupplier
extends Supplier
<Long
> {
8 static long toNodeIdBase(byte[] node
) {
9 assert node
.length
== 6;
10 return UuidFactory
.LEAST_SIG_RFC4122_VARIANT
| (node
[0] & 0xFFL
) //
11 | ((node
[1] & 0xFFL
) << 8) //
12 | ((node
[2] & 0xFFL
) << 16) //
13 | ((node
[3] & 0xFFL
) << 24) //
14 | ((node
[4] & 0xFFL
) << 32) //
15 | ((node
[5] & 0xFFL
) << 40); //
18 static boolean isNoMacAddressNodeId(byte[] nodeId
) {
19 return (nodeId
[0] & 1) != 0;
22 static byte[] randomNodeId() {
23 SecureRandom random
= new SecureRandom();
24 byte[] nodeId
= new byte[6];
25 random
.nextBytes(nodeId
);
30 * Force this node id to be identified as no MAC address.
32 * @see "https://datatracker.ietf.org/doc/html/rfc4122#section-4.5"
34 static void forceToNoMacAddress(byte[] nodeId
, int offset
) {
35 assert nodeId
!= null && offset
< nodeId
.length
;
36 nodeId
[offset
] = (byte) (nodeId
[offset
] | 1);
42 /** Guarantees that a byte array of length 6 will be returned. */
43 static byte[] toNodeIdBytes(byte[] source
, int offset
) {
46 if (offset
< 0 || offset
+ 6 > source
.length
)
47 throw new ArrayIndexOutOfBoundsException(offset
);
48 byte[] nodeId
= new byte[6];
49 System
.arraycopy(source
, offset
, nodeId
, 0, 6);