]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.db/src/org/simantics/db/service/ResourceUID.java b/bundles/org.simantics.db/src/org/simantics/db/service/ResourceUID.java
new file mode 100644 (file)
index 0000000..8ee9037
--- /dev/null
@@ -0,0 +1,68 @@
+package org.simantics.db.service;\r
+\r
+\r
+\r
+public final class ResourceUID {\r
+    public static final ResourceUID Null = new ResourceUID(null);\r
+    private final ClusterUID clusterUID;\r
+    private final short index;\r
+    private ResourceUID(ResourceUID a) {\r
+        assert(null == a);\r
+        clusterUID = ClusterUID.Null;\r
+        index = 0;\r
+    }\r
+    public ResourceUID(long first, long second, long index) {\r
+        this.clusterUID = ClusterUID.make(first, second);\r
+        if (index < 1 || index > 1<<14) // TODO: use ClusterTraits.RESOURCE_USED_BITS\r
+            throw new IllegalArgumentException("Trying to create illegal resource. index=" + index + " cluster=" + clusterUID);\r
+        this.index = (short)index;\r
+    }\r
+    public ResourceUID(long[] longs, int offset) {\r
+        if (longs.length < offset + 3)\r
+            throw new IllegalArgumentException("Not enough data to create resource uid. length=" + longs.length + " offset=" + offset); \r
+        this.clusterUID = ClusterUID.make(longs[offset+0], longs[offset+1]);\r
+        index = (short)longs[offset+2];\r
+        if (index < 1 || index > 1<<14) // TODO: use ClusterTraits.RESOURCE_USED_BITS\r
+            throw new IllegalArgumentException("Trying to create illegal resource. index=" + index + " cluster=" + clusterUID);\r
+    }\r
+    public int getIndex() {\r
+        return index;\r
+    }\r
+    public ClusterUID asCID() {\r
+        return clusterUID;\r
+    }\r
+    public static ResourceUID parse(String s) {\r
+       String[] ss = s.split("#");\r
+       if(ss.length != 3) throw new IllegalArgumentException("'" + s + "' is not a valid serialized ResourceUID");\r
+       long first = Long.parseLong(ss[0]);\r
+       long second = Long.parseLong(ss[1]);\r
+       short index = Short.parseShort(ss[2]);\r
+       return new ResourceUID(first, second, index);\r
+    }\r
+    public String serialized() {\r
+       return /*clusterUID.first +*/ "0#" + clusterUID.second + "#" + index;\r
+    }\r
+    public void toLong(long[] longs, int offset) {\r
+        int i = clusterUID.toLong(longs, offset);\r
+        longs[i] = index;\r
+    }\r
+    @Override\r
+    public String toString() {\r
+        return clusterUID.toString() + "." + index;\r
+    }\r
+    @Override\r
+    public boolean equals(Object o) {\r
+        if (this == o)\r
+            return true;\r
+        else if (!(o instanceof ResourceUID))\r
+            return false;\r
+        ResourceUID x = (ResourceUID)o;\r
+        return clusterUID.equals(x.clusterUID) && index == x.index;\r
+    }\r
+    @Override\r
+    public int hashCode() {\r
+        int result = clusterUID.hashCode();\r
+        result = 31 * result + index;\r
+        return result;\r
+    }\r
+}\r