]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db/src/org/simantics/db/service/ResourceUID.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db / src / org / simantics / db / service / ResourceUID.java
1 package org.simantics.db.service;\r
2 \r
3 \r
4 \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
10         assert(null == a);\r
11         clusterUID = ClusterUID.Null;\r
12         index = 0;\r
13     }\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
19     }\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
27     }\r
28     public int getIndex() {\r
29         return index;\r
30     }\r
31     public ClusterUID asCID() {\r
32         return clusterUID;\r
33     }\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
41     }\r
42     public String serialized() {\r
43         return /*clusterUID.first +*/ "0#" + clusterUID.second + "#" + index;\r
44     }\r
45     public void toLong(long[] longs, int offset) {\r
46         int i = clusterUID.toLong(longs, offset);\r
47         longs[i] = index;\r
48     }\r
49     @Override\r
50     public String toString() {\r
51         return clusterUID.toString() + "." + index;\r
52     }\r
53     @Override\r
54     public boolean equals(Object o) {\r
55         if (this == o)\r
56             return true;\r
57         else if (!(o instanceof ResourceUID))\r
58             return false;\r
59         ResourceUID x = (ResourceUID)o;\r
60         return clusterUID.equals(x.clusterUID) && index == x.index;\r
61     }\r
62     @Override\r
63     public int hashCode() {\r
64         int result = clusterUID.hashCode();\r
65         result = 31 * result + index;\r
66         return result;\r
67     }\r
68 }\r