--- /dev/null
+/*******************************************************************************\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