]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db/src/org/simantics/db/service/ResourceUID.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db / src / org / simantics / db / service / ResourceUID.java
1 package org.simantics.db.service;
2
3
4
5 public final class ResourceUID {
6     public static final ResourceUID Null = new ResourceUID(null);
7     private final ClusterUID clusterUID;
8     private final short index;
9     private ResourceUID(ResourceUID a) {
10         assert(null == a);
11         clusterUID = ClusterUID.Null;
12         index = 0;
13     }
14     public ResourceUID(long first, long second, long index) {
15         this.clusterUID = ClusterUID.make(first, second);
16         if (index < 1 || index > 1<<14) // TODO: use ClusterTraits.RESOURCE_USED_BITS
17             throw new IllegalArgumentException("Trying to create illegal resource. index=" + index + " cluster=" + clusterUID);
18         this.index = (short)index;
19     }
20     public ResourceUID(long[] longs, int offset) {
21         if (longs.length < offset + 3)
22             throw new IllegalArgumentException("Not enough data to create resource uid. length=" + longs.length + " offset=" + offset); 
23         this.clusterUID = ClusterUID.make(longs[offset+0], longs[offset+1]);
24         index = (short)longs[offset+2];
25         if (index < 1 || index > 1<<14) // TODO: use ClusterTraits.RESOURCE_USED_BITS
26             throw new IllegalArgumentException("Trying to create illegal resource. index=" + index + " cluster=" + clusterUID);
27     }
28     public int getIndex() {
29         return index;
30     }
31     public ClusterUID asCID() {
32         return clusterUID;
33     }
34     public static ResourceUID parse(String s) {
35         String[] ss = s.split("#");
36         if(ss.length != 3) throw new IllegalArgumentException("'" + s + "' is not a valid serialized ResourceUID");
37         long first = Long.parseLong(ss[0]);
38         long second = Long.parseLong(ss[1]);
39         short index = Short.parseShort(ss[2]);
40         return new ResourceUID(first, second, index);
41     }
42     public String serialized() {
43         return /*clusterUID.first +*/ "0#" + clusterUID.second + "#" + index;
44     }
45     public void toLong(long[] longs, int offset) {
46         int i = clusterUID.toLong(longs, offset);
47         longs[i] = index;
48     }
49     @Override
50     public String toString() {
51         return clusterUID.toString() + "." + index;
52     }
53     @Override
54     public boolean equals(Object o) {
55         if (this == o)
56             return true;
57         else if (!(o instanceof ResourceUID))
58             return false;
59         ResourceUID x = (ResourceUID)o;
60         return clusterUID.equals(x.clusterUID) && index == x.index;
61     }
62     @Override
63     public int hashCode() {
64         int result = clusterUID.hashCode();
65         result = 31 * result + index;
66         return result;
67     }
68 }