]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.impl;\r
13 \r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.impl.support.ResourceSupport;\r
16 import org.simantics.db.service.ClusterUID;\r
17 \r
18 final public class ResourceImpl implements Resource {\r
19 \r
20     final public ResourceSupport support;\r
21     final public int id;\r
22 \r
23     public ResourceImpl(final ResourceSupport support, final int id) {\r
24         assert(id != 0);\r
25         this.support = support;\r
26         this.id = id;\r
27     }\r
28 \r
29     @Override\r
30     final public long getResourceId() {\r
31         return support.getRandomAccessId(id);\r
32     }\r
33 \r
34     @Override\r
35     public boolean isPersistent() {\r
36         return id > 0;\r
37     }\r
38     \r
39     @Override\r
40     public Resource get() {\r
41         return this;\r
42     }\r
43 \r
44     @Override\r
45     public int hashCode() {\r
46         return id;\r
47     }\r
48 \r
49     @Override\r
50     public int getThreadHash() {\r
51         return id >>> 16;\r
52     }\r
53     \r
54     @Override\r
55     public boolean equals(Object object) {\r
56         if (this == object)\r
57             return true;\r
58         else if (object == null)\r
59             return false;\r
60         else if (!(object instanceof ResourceImpl))\r
61             return false;\r
62         ResourceImpl r = (ResourceImpl)object;\r
63         return r.id == id;\r
64     }\r
65     \r
66     @Override\r
67     public boolean equalsResource(Resource other) {\r
68         ResourceImpl r = (ResourceImpl)other;\r
69         return r.id == id;\r
70     }\r
71 \r
72     @Override\r
73     public int compareTo(Resource o) {\r
74         if(this==o)\r
75             return 0;\r
76         if(o instanceof ResourceImpl)\r
77             return id - ((ResourceImpl)o).id;\r
78         else\r
79             return 1;\r
80     }\r
81 \r
82     @Override\r
83     public String toString() {\r
84         StringBuilder sb = new StringBuilder(32);\r
85         try {\r
86             long rid = getResourceId();\r
87             if(DebugPolicy.VERBOSE) {\r
88                 sb.append("[id=");\r
89                 sb.append(id);\r
90                 sb.append(" - rid=");\r
91                 sb.append(rid);\r
92                 sb.append(" i=");\r
93                 short ri = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(id);\r
94                 sb.append(ri);\r
95                 int ck = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(id);\r
96                 sb.append(" c=");\r
97                 sb.append(ck);\r
98                 sb.append("]");\r
99             } else {\r
100                 sb.append("[id=$");\r
101                 sb.append(rid);\r
102                 sb.append("]");\r
103             }\r
104         } catch (Exception e) {\r
105             sb.append("[internal id=");\r
106             sb.append(id);\r
107             sb.append(", persistent resource id failed]");\r
108         }\r
109         return sb.toString();\r
110     }\r
111     public String toString(ClusterSupport support) {\r
112         StringBuilder sb = new StringBuilder(32);\r
113         long rid = getResourceId();\r
114         if (DebugPolicy.VERBOSE) {\r
115             sb.append("[id=");\r
116             sb.append(id);\r
117             sb.append(" - rid=");\r
118             sb.append(rid);\r
119             sb.append(" i=");\r
120             short ri = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(id);\r
121             sb.append(ri);\r
122             int ck = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(id);\r
123             sb.append(" ck=");\r
124             sb.append(ck);\r
125             ClusterBase cb = support.getClusterByClusterKey(ck);\r
126             if (null != cb) {\r
127                 ClusterUID clusterUID = cb.getClusterUID();\r
128                 sb.append(" c=");\r
129                 sb.append(clusterUID.toString());\r
130             }\r
131             sb.append("]");\r
132         } else {\r
133             sb.append("[id=$");\r
134             sb.append(rid);\r
135             sb.append("]");\r
136         }\r
137         return sb.toString();\r
138     }\r
139 }\r