1 package org.simantics.db.service;
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) {
11 clusterUID = ClusterUID.Null;
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;
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);
28 public int getIndex() {
31 public ClusterUID asCID() {
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);
42 public String serialized() {
43 return /*clusterUID.first +*/ "0#" + clusterUID.second + "#" + index;
45 public void toLong(long[] longs, int offset) {
46 int i = clusterUID.toLong(longs, offset);
50 public String toString() {
51 return clusterUID.toString() + "." + index;
54 public boolean equals(Object o) {
57 else if (!(o instanceof ResourceUID))
59 ResourceUID x = (ResourceUID)o;
60 return clusterUID.equals(x.clusterUID) && index == x.index;
63 public int hashCode() {
64 int result = clusterUID.hashCode();
65 result = 31 * result + index;