]> gerrit.simantics Code Review - simantics/interop.git/blob - 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
1 package org.simantics.interop.mapping.data;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 \r
6 import org.simantics.db.Resource;\r
7 \r
8 public class ResourceIdentifiable implements Identifiable {\r
9 \r
10         protected Resource resource;\r
11         \r
12         public ResourceIdentifiable(Resource resource) {\r
13                 this.resource = resource;\r
14         }\r
15         \r
16         public Resource getResource() {\r
17                 return resource;\r
18         }\r
19         \r
20         @Override\r
21         public int hashCode() {\r
22                 return resource.hashCode();\r
23         }\r
24         \r
25         @SuppressWarnings("unchecked")\r
26         public <T> T getAdapter(Class<T> clazz) {\r
27                 if (clazz.equals(Resource.class))\r
28                         return (T)resource;\r
29                 if (clazz.equals(Resource[].class))\r
30                         return (T) new Resource[]{resource};\r
31                 if (clazz.equals(String.class)) {\r
32                         // TODO : have to use org.simantics.db.common.utils.Transaction to get access to ReadGraph.\r
33                         return (T)resource.toString();//NameUtils.getSafeName(g,resource);\r
34                 }\r
35                 return null;\r
36         }\r
37         \r
38         @Override\r
39         public boolean equals(Object arg0) {\r
40                 if (arg0 == null)\r
41                         return false;\r
42                 if (this.getClass() != arg0.getClass())\r
43                         return false;\r
44                 ResourceIdentifiable other = (ResourceIdentifiable)arg0;\r
45                 return resource.equals(other.resource);\r
46         }\r
47         \r
48         @Override\r
49         public Identifiable merge(Identifiable other) {\r
50                 if (other instanceof ResourceIdentifiable) {\r
51                         ResourceSetIdentifiable i = new ResourceSetIdentifiable(this.resource,((ResourceIdentifiable)other).resource);\r
52                         return i;\r
53                 } else if (other instanceof ResourceSetIdentifiable) {\r
54                         Collection<Resource> coll = new ArrayList<Resource>();\r
55                         coll.add(this.resource);\r
56                         coll.addAll(((ResourceSetIdentifiable)other).getResources());\r
57                         ResourceSetIdentifiable i = new ResourceSetIdentifiable(resource,coll);\r
58                         return i;\r
59                 }\r
60                 return null;\r
61         }\r
62         \r
63         @Override\r
64         public String toString() {\r
65                 return resource.toString();\r
66         }\r
67         \r
68 }\r