]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db/src/org/simantics/db/service/ClusterUID.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db / src / org / simantics / db / service / ClusterUID.java
1 package org.simantics.db.service;
2
3 import java.nio.ByteBuffer;
4 import java.nio.ByteOrder;
5
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);
12     }
13 //    public final long first;
14     public final long second;
15 //    public transient int arrayOffset = -1;
16
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;
23
24     public ClusterUID() {
25 //        first = 0;
26         second = 0;
27     }
28
29     public ClusterUID(long first, long second) {
30         assert(first == 0);
31 //        this.first = first;
32         this.second = second;
33     }
34
35     public synchronized static ClusterUID make(long first, long second) {
36
37         assert(first == 0);
38
39         if(second == secondCache1) return clusterCache1;
40         else if(second == secondCache2) return clusterCache2;
41         else {
42             ClusterUID result = new ClusterUID(first, second);
43             secondCache2 = secondCache1; clusterCache2 = clusterCache1;
44             secondCache1 = second; clusterCache1 = result;
45             return result;
46         }
47
48     }
49
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());
56     }
57
58     @Override
59     public String toString() {
60         return String.format("%x.%x", 0, second);
61     }
62     public static int getLongLength() {
63         return 2;
64     }
65     public int toByte(byte[] bytes, int offset) {
66         Bytes.writeLE(bytes, offset+0, 0);
67         Bytes.writeLE(bytes, offset+8, second);
68         return offset + 16;
69     }
70     public int toLong(long[] longs, int offset) {
71         longs[offset++] = 0;
72         longs[offset++] = second;
73         return offset;
74     }
75     public byte[] asBytes() {
76         byte[] bytes = new byte[16];
77         toByte(bytes, 0);
78         return bytes;
79     }
80     public ResourceUID toRID(int resourceIndex) {
81         return new ResourceUID(0, second, resourceIndex);
82     }
83     @Override
84     public boolean equals(Object o) {
85         if (this == o)
86             return true;
87         else if (!(o instanceof ClusterUID))
88             return false;
89         ClusterUID x = (ClusterUID)o;
90         return /*first == x.first &&*/ second == x.second;
91     }
92     @Override
93     public int hashCode() {
94         int result = 17;
95 //        int f = (int)(0 ^ (0 >>> 32));
96 //        result = 31 * result + f;
97         int s = (int)(second ^ (second >>> 32));
98         result = 31 * result + s;
99         return result;
100     }
101 }