]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/ResourceImpl.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / ResourceImpl.java
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/ResourceImpl.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/ResourceImpl.java
new file mode 100644 (file)
index 0000000..77764a3
--- /dev/null
@@ -0,0 +1,139 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.impl;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.impl.support.ResourceSupport;\r
+import org.simantics.db.service.ClusterUID;\r
+\r
+final public class ResourceImpl implements Resource {\r
+\r
+    final public ResourceSupport support;\r
+    final public int id;\r
+\r
+    public ResourceImpl(final ResourceSupport support, final int id) {\r
+        assert(id != 0);\r
+        this.support = support;\r
+        this.id = id;\r
+    }\r
+\r
+    @Override\r
+    final public long getResourceId() {\r
+        return support.getRandomAccessId(id);\r
+    }\r
+\r
+    @Override\r
+    public boolean isPersistent() {\r
+        return id > 0;\r
+    }\r
+    \r
+    @Override\r
+    public Resource get() {\r
+        return this;\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return id;\r
+    }\r
+\r
+    @Override\r
+    public int getThreadHash() {\r
+        return id >>> 16;\r
+    }\r
+    \r
+    @Override\r
+    public boolean equals(Object object) {\r
+        if (this == object)\r
+            return true;\r
+        else if (object == null)\r
+            return false;\r
+        else if (!(object instanceof ResourceImpl))\r
+            return false;\r
+        ResourceImpl r = (ResourceImpl)object;\r
+        return r.id == id;\r
+    }\r
+    \r
+    @Override\r
+    public boolean equalsResource(Resource other) {\r
+        ResourceImpl r = (ResourceImpl)other;\r
+        return r.id == id;\r
+    }\r
+\r
+    @Override\r
+    public int compareTo(Resource o) {\r
+        if(this==o)\r
+            return 0;\r
+        if(o instanceof ResourceImpl)\r
+            return id - ((ResourceImpl)o).id;\r
+        else\r
+            return 1;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        StringBuilder sb = new StringBuilder(32);\r
+        try {\r
+            long rid = getResourceId();\r
+            if(DebugPolicy.VERBOSE) {\r
+                sb.append("[id=");\r
+                sb.append(id);\r
+                sb.append(" - rid=");\r
+                sb.append(rid);\r
+                sb.append(" i=");\r
+                short ri = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(id);\r
+                sb.append(ri);\r
+                int ck = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(id);\r
+                sb.append(" c=");\r
+                sb.append(ck);\r
+                sb.append("]");\r
+            } else {\r
+                sb.append("[id=$");\r
+                sb.append(rid);\r
+                sb.append("]");\r
+            }\r
+        } catch (Exception e) {\r
+            sb.append("[internal id=");\r
+            sb.append(id);\r
+            sb.append(", persistent resource id failed]");\r
+        }\r
+        return sb.toString();\r
+    }\r
+    public String toString(ClusterSupport support) {\r
+        StringBuilder sb = new StringBuilder(32);\r
+        long rid = getResourceId();\r
+        if (DebugPolicy.VERBOSE) {\r
+            sb.append("[id=");\r
+            sb.append(id);\r
+            sb.append(" - rid=");\r
+            sb.append(rid);\r
+            sb.append(" i=");\r
+            short ri = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(id);\r
+            sb.append(ri);\r
+            int ck = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(id);\r
+            sb.append(" ck=");\r
+            sb.append(ck);\r
+            ClusterBase cb = support.getClusterByClusterKey(ck);\r
+            if (null != cb) {\r
+                ClusterUID clusterUID = cb.getClusterUID();\r
+                sb.append(" c=");\r
+                sb.append(clusterUID.toString());\r
+            }\r
+            sb.append("]");\r
+        } else {\r
+            sb.append("[id=$");\r
+            sb.append(rid);\r
+            sb.append("]");\r
+        }\r
+        return sb.toString();\r
+    }\r
+}\r