]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.uuid/src/org/argeo/api/uuid/NodeIdSupplier.java
Improve build and local deployment
[lgpl/argeo-commons.git] / org.argeo.api.uuid / src / org / argeo / api / uuid / NodeIdSupplier.java
1 package org.argeo.api.uuid;
2
3 import java.security.SecureRandom;
4 import java.util.function.Supplier;
5
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); //
16 }
17
18 static boolean isNoMacAddressNodeId(byte[] nodeId) {
19 return (nodeId[0] & 1) != 0;
20 }
21
22 static byte[] randomNodeId() {
23 SecureRandom random = new SecureRandom();
24 byte[] nodeId = new byte[6];
25 random.nextBytes(nodeId);
26 return nodeId;
27 }
28
29 /**
30 * Force this node id to be identified as no MAC address.
31 *
32 * @see "https://datatracker.ietf.org/doc/html/rfc4122#section-4.5"
33 */
34 static void forceToNoMacAddress(byte[] nodeId, int offset) {
35 assert nodeId != null && offset < nodeId.length;
36 nodeId[offset] = (byte) (nodeId[offset] | 1);
37 }
38
39 /*
40 * SPI UTILITIES
41 */
42 /** Guarantees that a byte array of length 6 will be returned. */
43 static byte[] toNodeIdBytes(byte[] source, int offset) {
44 if (source == null)
45 return null;
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);
50 return nodeId;
51 }
52 }