]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/ResourceImpl.java
Fixed multiple issues causing dangling references to discarded queries
[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.DevelopmentKeys;
15 import org.simantics.db.Resource;
16 import org.simantics.db.impl.support.ResourceSupport;
17 import org.simantics.db.service.ClusterUID;
18
19 final public class ResourceImpl implements Resource {
20
21     final public ResourceSupport support;
22     final public int id;
23
24     public ResourceImpl(final ResourceSupport support, final int id) {
25         assert(id != 0);
26         this.support = support;
27         this.id = id;
28     }
29
30     @Override
31     final public long getResourceId() {
32         return support.getRandomAccessId(id);
33     }
34
35     @Override
36     public boolean isPersistent() {
37         return id > 0;
38     }
39     
40     @Override
41     public Resource get() {
42         return this;
43     }
44
45     @Override
46     public int hashCode() {
47         return id;
48     }
49
50     @Override
51     public int getThreadHash() {
52         return id >>> 16;
53     }
54     
55     @Override
56     public boolean equals(Object object) {
57         if (this == object)
58             return true;
59         else if (object == null)
60             return false;
61         else if (!(object instanceof ResourceImpl))
62             return false;
63         ResourceImpl r = (ResourceImpl)object;
64         return r.id == id;
65     }
66     
67     @Override
68     public boolean equalsResource(Resource other) {
69         ResourceImpl r = (ResourceImpl)other;
70         return r.id == id;
71     }
72
73     @Override
74     public int compareTo(Resource o) {
75         if(this==o)
76             return 0;
77         if(o instanceof ResourceImpl)
78             return id - ((ResourceImpl)o).id;
79         else
80             return 1;
81     }
82
83     @Override
84     public String toString() {
85         StringBuilder sb = new StringBuilder(32);
86         try {
87             long rid = getResourceId();
88             if(DevelopmentKeys.VERBOSE) {
89                 sb.append("[id=");
90                 sb.append(id);
91                 sb.append(" - rid=");
92                 sb.append(rid);
93                 sb.append(" i=");
94                 short ri = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(id);
95                 sb.append(ri);
96                 int ck = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(id);
97                 sb.append(" c=");
98                 sb.append(ck);
99                 sb.append("]");
100             } else {
101                 sb.append("[id=$");
102                 sb.append(rid);
103                 sb.append("]");
104             }
105         } catch (Exception e) {
106             sb.append("[internal id=");
107             sb.append(id);
108             sb.append(", persistent resource id failed]");
109         }
110         return sb.toString();
111     }
112     public String toString(ClusterSupport support) {
113         StringBuilder sb = new StringBuilder(32);
114         long rid = getResourceId();
115         if (DevelopmentKeys.VERBOSE) {
116             sb.append("[id=");
117             sb.append(id);
118             sb.append(" - rid=");
119             sb.append(rid);
120             sb.append(" i=");
121             short ri = ClusterTraitsBase.getResourceIndexFromResourceKeyNoThrow(id);
122             sb.append(ri);
123             int ck = ClusterTraitsBase.getClusterKeyFromResourceKeyNoThrow(id);
124             sb.append(" ck=");
125             sb.append(ck);
126             ClusterBase cb = support.getClusterByClusterKey(ck);
127             if (null != cb) {
128                 ClusterUID clusterUID = cb.getClusterUID();
129                 sb.append(" c=");
130                 sb.append(clusterUID.toString());
131             }
132             sb.append("]");
133         } else {
134             sb.append("[id=$");
135             sb.append(rid);
136             sb.append("]");
137         }
138         return sb.toString();
139     }
140 }