]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/Queries.java
Fixed invalid comparisons which were identified by Eclipse IDE 2018-09
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / Queries.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.common.request;
13
14 import java.util.Collection;
15
16 import org.simantics.databoard.binding.Binding;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.uri.ResourceToPossibleURI;
20 import org.simantics.db.common.uri.ResourceToURI;
21 import org.simantics.db.common.utils.NameUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.request.AsyncRead;
24 import org.simantics.db.request.Read;
25 import org.simantics.layer0.Layer0;
26
27 /**
28  * Facade class that aggregates the most common queries.
29  * 
30  * Example:
31  * 
32  *   System.out.println(  session.read( Queries.adapt(r, String.class) )  );
33  *
34  * @author Tuukka Lehtonen
35  * @author Toni Kalajainen
36  */
37 public class Queries {
38
39     public static AsyncRead<Resource> resource(String uri) {
40         return new org.simantics.db.common.primitiverequest.Resource(uri);
41     }
42
43     public static Read<String> name(final Resource resource) {
44         return new ResourceRead<String>(resource) {
45             @Override
46             public String perform(ReadGraph graph) throws DatabaseException {
47                 return graph.getRelatedValue(resource, Layer0.getInstance(graph).HasName);
48             }
49         };
50     }
51
52     public static Read<String> safeName(final Resource resource) {
53         return new ResourceRead<String>(resource) {
54             @Override
55             public String perform(ReadGraph graph) throws DatabaseException {
56                 return NameUtils.getSafeName(graph, resource);
57             }
58         };
59     }
60
61     public static Read<String> name(final String uri) {
62         return new UnaryRead<String, String>(uri) {
63             @Override
64             public String perform(ReadGraph graph) throws DatabaseException {
65                 Resource resource = graph.getResource(uri);
66                 return graph.getRelatedValue(resource, Layer0.getInstance(graph).HasName);
67             }
68         };
69     }
70
71     public static Read<String> uri(Resource resource) {
72         return new ResourceToURI(resource);
73     }
74
75     public static Read<String> possibleUri(Resource resource) {
76         return new ResourceToPossibleURI(resource);
77     }
78
79     public static <T> Read<T> adapt(Resource resource, Class<T> target)
80     {
81         return new Adapt<T>(resource, target);
82     }
83
84     public static <T> Read<T> adapt(Resource resource, Class<T> target, boolean allowNull)
85     {
86         return new Adapt<T>(resource, target, allowNull);
87     }
88
89     public static <T> Read<T> adapt(Resource resource, Class<T> target, boolean allowNull, boolean uniqueResult)
90     {
91         return new Adapt<T>(resource, target, allowNull, uniqueResult);
92     }
93
94     public static Read<Boolean> isInstanceOf(Resource resource, Resource type)
95     {
96         return new IsInstanceOfQuery(resource, type);
97     }
98
99     public static Read<Boolean> isInstanceOf(Resource resource, String type)
100     {
101         return new IsInstanceOfQuery2(resource, type);
102     }
103
104     public static <T> Read<T> getRelatedValue(Resource subject, Resource relation, Binding binding)
105     {
106         return new ReadRelatedValue<T>(subject, relation, binding);
107     }
108
109     public static <T> Read<T> getRelatedValue(Resource subject, String relation, Binding binding)
110     {
111         return new ReadRelatedValue2<T>(subject, relation, binding);
112     }
113
114     public static <T> Read<T> getPossibleRelatedValue(Resource subject, Resource relation, Binding binding)
115     {
116         return new ReadPossibleRelatedValue<T>(subject, relation, binding);
117     }
118     
119     public static Read<Boolean> hasTag(Resource subject, Resource tag) {
120         return new HasTag(subject, tag);
121     }
122
123     public static ObjectsWithType objectsWithType(Resource subject, Resource subrelationOf, Resource instanceOf) {
124         return new ObjectsWithType(subject, subrelationOf, instanceOf);
125     }
126
127         public static Read<Resource> possibleObjectWithType(final Resource resource, final Resource predicate, final Resource type) {
128                 return new PossibleObjectWithType(resource, predicate, type);
129         }
130         
131         public static Read<Resource> possibleObject(final Resource subject, final Resource predicate) {
132                 return new Read<Resource>() {
133                         @Override
134                         public Resource perform(ReadGraph graph) throws DatabaseException {
135                                 return graph.getPossibleObject(subject, predicate);
136                         }
137                 };              
138         }
139         
140         public static Read<Collection<Resource>> objects(final Resource subject, final Resource predicate) {
141                 return new Read<Collection<Resource>>() {
142                         @Override
143                         public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
144                                 return graph.getObjects(subject, predicate);
145                         }
146                 };              
147         }       
148         
149     
150 }
151
152
153 class IsInstanceOfQuery implements Read<Boolean> {
154
155     final protected Resource resource;
156     final protected Resource type;
157
158     public IsInstanceOfQuery(Resource resource, Resource type) {
159         this.resource = resource;
160         this.type = type;
161     }
162
163     @Override
164     public int hashCode() {
165         return resource.hashCode() + 13 * type.hashCode();
166     }
167
168     @Override
169     public boolean equals(Object object) {
170         if (this == object)
171             return true;
172         else if (object == null)
173             return false;
174         else if (getClass() != object.getClass())
175             return false;
176         IsInstanceOfQuery other = (IsInstanceOfQuery) object;
177         return resource.equals(other.resource) && type.equals(other.type);
178     }
179
180     @Override
181     public Boolean perform(ReadGraph graph) throws DatabaseException {
182         return graph.isInstanceOf(resource, type);
183     }
184
185 }
186
187 class IsInstanceOfQuery2 implements Read<Boolean> {
188
189     final protected Resource resource;
190     final protected String type;
191
192     public IsInstanceOfQuery2(Resource resource, String type) {
193         this.resource = resource;
194         this.type = type;
195     }
196
197     @Override
198     public int hashCode() {
199         return resource.hashCode() + 13 * type.hashCode();
200     }
201
202     @Override
203     public boolean equals(Object object) {
204         if (this == object)
205             return true;
206         else if (object == null)
207             return false;
208         else if (getClass() != object.getClass())
209             return false;
210         IsInstanceOfQuery2 other = (IsInstanceOfQuery2) object;
211         return resource.equals(other.resource) && type.equals(other.type);
212     }
213
214     @Override
215     public Boolean perform(ReadGraph graph) throws DatabaseException {
216         Resource typeResource = graph.getResource(type);
217         return graph.isInstanceOf(resource, typeResource);
218     }
219
220 }
221
222
223 class ReadRelatedValue<T> implements Read<T> {
224
225     Resource subject;
226     Resource relation;
227     Binding binding;
228
229     public ReadRelatedValue(Resource subject, Resource relation, Binding binding) {
230         this.subject = subject;
231         this.relation = relation;
232         this.binding = binding;
233     }
234
235     @Override
236     public int hashCode() {
237         return subject.hashCode() + 13 * relation.hashCode() + 7*binding.hashCode();
238     }
239
240     @Override
241     public boolean equals(Object object) {
242         if (this == object)
243             return true;
244         else if (object == null)
245             return false;
246         else if (getClass() != object.getClass())
247             return false;
248         ReadRelatedValue<?> other = (ReadRelatedValue<?>) object;
249         return subject.equals(other.subject) &&
250         relation.equals(other.relation) &&
251         binding.equals(other.binding);
252     }
253
254     @SuppressWarnings("unchecked")
255     @Override
256     public T perform(ReadGraph graph) throws DatabaseException {
257         return (T) graph.getRelatedValue(subject, relation, binding);
258     }
259 }
260
261
262 class ReadRelatedValue2<T> implements Read<T> {
263
264     Resource subject;
265     String relation;
266     Binding binding;
267
268     public ReadRelatedValue2(Resource subject, String relation, Binding binding) {
269         this.subject = subject;
270         this.relation = relation;
271         this.binding = binding;
272     }
273
274     @Override
275     public int hashCode() {
276         return subject.hashCode() + 13 * relation.hashCode() + 7*binding.hashCode();
277     }
278
279     @Override
280     public boolean equals(Object object) {
281         if (this == object)
282             return true;
283         else if (object == null)
284             return false;
285         else if (getClass() != object.getClass())
286             return false;
287         ReadRelatedValue2<?> other = (ReadRelatedValue2<?>) object;
288         return subject.equals(other.subject) &&
289         relation.equals(other.relation) &&
290         binding.equals(other.binding);
291     }
292
293     @SuppressWarnings("unchecked")
294     @Override
295     public T perform(ReadGraph graph) throws DatabaseException {
296         Resource relationResource = graph.getResource(relation);
297         return (T) graph.getRelatedValue(subject, relationResource, binding);
298     }
299 }
300
301 class ReadPossibleRelatedValue<T> implements Read<T> {
302
303     Resource subject;
304     Resource relation;
305     Binding binding;
306
307     public ReadPossibleRelatedValue(Resource subject, Resource relation, Binding binding) {
308         this.subject = subject;
309         this.relation = relation;
310         this.binding = binding;
311     }
312
313     @Override
314     public int hashCode() {
315         return subject.hashCode() + 13 * relation.hashCode() + 7*binding.hashCode() + 5453;
316     }
317
318     @Override
319     public boolean equals(Object object) {
320         if (this == object)
321             return true;
322         else if (object == null)
323             return false;
324         else if (getClass() != object.getClass())
325             return false;
326         ReadPossibleRelatedValue<?> other = (ReadPossibleRelatedValue<?>) object;
327         return subject.equals(other.subject) &&
328         relation.equals(other.relation) &&
329         binding.equals(other.binding);
330     }
331
332     @SuppressWarnings("unchecked")
333     @Override
334     public T perform(ReadGraph graph) throws DatabaseException {
335         return (T) graph.getPossibleRelatedValue(subject, relation, binding);
336     }
337 }
338
339 class HasTag extends ResourceRead2<Boolean> {
340     public HasTag(Resource subject, Resource tag) {
341         super(subject, tag);
342     }
343
344     @Override
345     public Boolean perform(ReadGraph graph) throws DatabaseException {
346         return Boolean.valueOf(graph.hasStatement(resource, resource2, resource));
347     }
348 }