1 package org.simantics.db.service;
3 import java.nio.ByteBuffer;
4 import java.nio.ByteOrder;
6 public final class ClusterUID {
7 public static final ClusterUID Null = new ClusterUID(0, 0);
8 public static final ClusterUID Reserved = new ClusterUID(0, 1);
9 public static final ClusterUID Builtin = new ClusterUID(0, 2);
10 public static boolean isLegal(ClusterUID clusterUID) {
11 return !clusterUID.equals(Null) && !clusterUID.equals(Reserved);
13 // public final long first;
14 public final long second;
15 // public transient int arrayOffset = -1;
17 // transient private static long firstCache1 = 0;
18 // transient private static long firstCache2 = 0;
19 transient private static long secondCache1 = 0;
20 transient private static long secondCache2 = 0;
21 transient private static ClusterUID clusterCache1 = null;
22 transient private static ClusterUID clusterCache2 = null;
29 public ClusterUID(long first, long second) {
31 // this.first = first;
35 public synchronized static ClusterUID make(long first, long second) {
39 if(second == secondCache1) return clusterCache1;
40 else if(second == secondCache2) return clusterCache2;
42 ClusterUID result = new ClusterUID(first, second);
43 secondCache2 = secondCache1; clusterCache2 = clusterCache1;
44 secondCache1 = second; clusterCache1 = result;
50 public static ClusterUID make(byte[] bytes, int offset) {
51 if (bytes.length < offset + 16)
52 throw new IllegalArgumentException("Too few bytes for ClusteUID. length=" + bytes.length + " offset=" + offset);
53 ByteBuffer bf = ByteBuffer.wrap(bytes, offset, 16);
54 bf.order(ByteOrder.LITTLE_ENDIAN);
55 return make(bf.getLong(),bf.getLong());
59 public String toString() {
60 return String.format("%x.%x", 0, second);
62 public static int getLongLength() {
65 public int toByte(byte[] bytes, int offset) {
66 Bytes.writeLE(bytes, offset+0, 0);
67 Bytes.writeLE(bytes, offset+8, second);
70 public int toLong(long[] longs, int offset) {
72 longs[offset++] = second;
75 public byte[] asBytes() {
76 byte[] bytes = new byte[16];
80 public ResourceUID toRID(int resourceIndex) {
81 return new ResourceUID(0, second, resourceIndex);
84 public boolean equals(Object o) {
87 else if (!(o instanceof ClusterUID))
89 ClusterUID x = (ClusterUID)o;
90 return /*first == x.first &&*/ second == x.second;
93 public int hashCode() {
95 // int f = (int)(0 ^ (0 >>> 32));
96 // result = 31 * result + f;
97 int s = (int)(second ^ (second >>> 32));
98 result = 31 * result + s;