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