]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/StatementImpl.java
Fixed CollectionSupportImpl.ResourceList iteration order
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / StatementImpl.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 fi.vtt.simantics.procore.internal;
13
14 import org.simantics.db.Resource;
15 import org.simantics.db.Statement;
16 import org.simantics.db.impl.ResourceImpl;
17 import org.simantics.db.procore.protocol.Constants;
18
19
20 public abstract class StatementImpl implements Statement {
21     public final static StatementImpl[] NONE = new StatementImpl[0];
22     public final ResourceImpl subject;
23     public final ResourceImpl predicate;
24     protected ResourceImpl object;
25     public StatementImpl(ResourceImpl subject, ResourceImpl predicate) {
26         if (null == subject)
27             throw new IllegalArgumentException();
28         if(null == predicate)
29             throw new IllegalArgumentException();
30         this.subject = (ResourceImpl)subject;
31         this.predicate = (ResourceImpl)predicate;
32     }
33     
34     public ResourceImpl getObjectImpl() {
35         return object;
36     }
37     @Override
38     public Resource getObject() {
39         return object;
40     }
41
42     @Override
43     final public Resource getPredicate() {
44         return predicate;
45     }
46
47     @Override
48     final public Resource getSubject() {
49         return subject;
50     }
51
52     @Override
53     public boolean isAsserted(Resource testSubject) {
54         return !subject.equals(testSubject);
55     }
56     
57     public abstract boolean equals(long s, long p, long o);
58     
59   }
60
61 final class StatementImplOld extends StatementImpl {
62     
63     public StatementImplOld(ResourceImpl subject, ResourceImpl predicate, ResourceImpl object) {
64         super(subject, predicate);
65         this.object = (ResourceImpl)object;
66     }
67     @Override
68     public int hashCode() {
69         return subject.hashCode() + (int)object.id; 
70     }
71
72     @Override
73     public boolean equals(long s, long p, long o) {
74         return (subject.id == s) && (predicate.id == p) && (object.id == o);
75     }
76
77     @Override
78     public boolean equals(Object obj) {
79         if (this == obj)
80             return true;
81         else if (!(obj instanceof StatementImpl))
82             return false;
83         StatementImpl r = (StatementImpl)obj;
84         return r.equals(subject.id, predicate.id, object.id);
85     }
86 }
87
88 final class StatementImplNew extends StatementImpl {
89     public final long objectResourceId;
90     public final long objectClusterId;
91     public final GraphSession graphSession;
92     public StatementImplNew(ResourceImpl subject, ResourceImpl predicate, long id, long cluster, GraphSession graphSession) {
93         super(subject, predicate);
94         this.object = null;
95         this.objectResourceId = id;
96         if (Constants.ReservedClusterId == cluster)
97             throw new IllegalArgumentException("Argh!");
98         this.objectClusterId = cluster;
99         this.graphSession = graphSession;
100     }
101
102     @Override
103     final public Resource getObject() {
104 //        if (null == object)
105 //            try {
106 //                object = graphSession.getResourceOrThrow(objectResourceId, objectClusterId);
107 //            } catch (ResourceNotFoundException e) {
108 //                e.printStackTrace();
109 //                return null;
110 //            }
111 //        return object;
112         throw new Error("No can do.");
113     }
114
115     @Override
116     public ResourceImpl getObjectImpl() {
117 //        if (null == object)
118 //            try {
119 //                object = graphSession.getResourceOrThrow(objectResourceId, objectClusterId);
120 //            } catch (ResourceNotFoundException e) {
121 //                e.printStackTrace();
122 //                return null;
123 //            }
124 //        return object;
125         throw new Error("No can do.");
126     }
127
128     @Override
129     public int hashCode() {
130         return subject.hashCode() + (int)objectResourceId; 
131     }
132
133     @Override
134     public boolean equals(long s, long p, long o) {
135         return (subject.id == s) && (predicate.id == p) && (objectResourceId == o);
136     }
137
138     @Override
139     public boolean equals(Object obj) {
140         if (this == obj)
141             return true;
142         else if (obj == null)
143             return false;
144         else if (StatementImpl.class != obj.getClass())
145             return false;
146         StatementImpl r = (StatementImpl)obj;
147         return r.equals(subject.id, predicate.id, objectResourceId);
148     }
149 }