]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop.mapping/src/org/simantics/interop/mapping/data/ResourceIdentifiable.java
refs #3483
[simantics/interop.git] / org.simantics.interop.mapping / src / org / simantics / interop / mapping / data / ResourceIdentifiable.java
diff --git a/org.simantics.interop.mapping/src/org/simantics/interop/mapping/data/ResourceIdentifiable.java b/org.simantics.interop.mapping/src/org/simantics/interop/mapping/data/ResourceIdentifiable.java
new file mode 100644 (file)
index 0000000..4791d5b
--- /dev/null
@@ -0,0 +1,68 @@
+package org.simantics.interop.mapping.data;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.db.Resource;\r
+\r
+public class ResourceIdentifiable implements Identifiable {\r
+\r
+       protected Resource resource;\r
+       \r
+       public ResourceIdentifiable(Resource resource) {\r
+               this.resource = resource;\r
+       }\r
+       \r
+       public Resource getResource() {\r
+               return resource;\r
+       }\r
+       \r
+       @Override\r
+       public int hashCode() {\r
+               return resource.hashCode();\r
+       }\r
+       \r
+       @SuppressWarnings("unchecked")\r
+       public <T> T getAdapter(Class<T> clazz) {\r
+               if (clazz.equals(Resource.class))\r
+                       return (T)resource;\r
+               if (clazz.equals(Resource[].class))\r
+                       return (T) new Resource[]{resource};\r
+               if (clazz.equals(String.class)) {\r
+                       // TODO : have to use org.simantics.db.common.utils.Transaction to get access to ReadGraph.\r
+                       return (T)resource.toString();//NameUtils.getSafeName(g,resource);\r
+               }\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public boolean equals(Object arg0) {\r
+               if (arg0 == null)\r
+                       return false;\r
+               if (this.getClass() != arg0.getClass())\r
+                       return false;\r
+               ResourceIdentifiable other = (ResourceIdentifiable)arg0;\r
+               return resource.equals(other.resource);\r
+       }\r
+       \r
+       @Override\r
+       public Identifiable merge(Identifiable other) {\r
+               if (other instanceof ResourceIdentifiable) {\r
+                       ResourceSetIdentifiable i = new ResourceSetIdentifiable(this.resource,((ResourceIdentifiable)other).resource);\r
+                       return i;\r
+               } else if (other instanceof ResourceSetIdentifiable) {\r
+                       Collection<Resource> coll = new ArrayList<Resource>();\r
+                       coll.add(this.resource);\r
+                       coll.addAll(((ResourceSetIdentifiable)other).getResources());\r
+                       ResourceSetIdentifiable i = new ResourceSetIdentifiable(resource,coll);\r
+                       return i;\r
+               }\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public String toString() {\r
+               return resource.toString();\r
+       }\r
+       \r
+}\r