1 package org.simantics.db.service;
\r
5 public final class ResourceUID {
\r
6 public static final ResourceUID Null = new ResourceUID(null);
\r
7 private final ClusterUID clusterUID;
\r
8 private final short index;
\r
9 private ResourceUID(ResourceUID a) {
\r
11 clusterUID = ClusterUID.Null;
\r
14 public ResourceUID(long first, long second, long index) {
\r
15 this.clusterUID = ClusterUID.make(first, second);
\r
16 if (index < 1 || index > 1<<14) // TODO: use ClusterTraits.RESOURCE_USED_BITS
\r
17 throw new IllegalArgumentException("Trying to create illegal resource. index=" + index + " cluster=" + clusterUID);
\r
18 this.index = (short)index;
\r
20 public ResourceUID(long[] longs, int offset) {
\r
21 if (longs.length < offset + 3)
\r
22 throw new IllegalArgumentException("Not enough data to create resource uid. length=" + longs.length + " offset=" + offset);
\r
23 this.clusterUID = ClusterUID.make(longs[offset+0], longs[offset+1]);
\r
24 index = (short)longs[offset+2];
\r
25 if (index < 1 || index > 1<<14) // TODO: use ClusterTraits.RESOURCE_USED_BITS
\r
26 throw new IllegalArgumentException("Trying to create illegal resource. index=" + index + " cluster=" + clusterUID);
\r
28 public int getIndex() {
\r
31 public ClusterUID asCID() {
\r
34 public static ResourceUID parse(String s) {
\r
35 String[] ss = s.split("#");
\r
36 if(ss.length != 3) throw new IllegalArgumentException("'" + s + "' is not a valid serialized ResourceUID");
\r
37 long first = Long.parseLong(ss[0]);
\r
38 long second = Long.parseLong(ss[1]);
\r
39 short index = Short.parseShort(ss[2]);
\r
40 return new ResourceUID(first, second, index);
\r
42 public String serialized() {
\r
43 return /*clusterUID.first +*/ "0#" + clusterUID.second + "#" + index;
\r
45 public void toLong(long[] longs, int offset) {
\r
46 int i = clusterUID.toLong(longs, offset);
\r
50 public String toString() {
\r
51 return clusterUID.toString() + "." + index;
\r
54 public boolean equals(Object o) {
\r
57 else if (!(o instanceof ResourceUID))
\r
59 ResourceUID x = (ResourceUID)o;
\r
60 return clusterUID.equals(x.clusterUID) && index == x.index;
\r
63 public int hashCode() {
\r
64 int result = clusterUID.hashCode();
\r
65 result = 31 * result + index;
\r