]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQuery.java
ee89bedf76f78de87234f5d83535ad2c66124cc6
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / UnaryQuery.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.query;
13
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.request.RequestFlags;
17
18 abstract public class UnaryQuery<Procedure> extends CacheEntryBase implements Query {
19         
20     final public int id;
21
22     public UnaryQuery(int r) {
23         assert(r != 0);
24         this.id = r;
25     }
26     
27     @Override
28     int makeHash() {
29         return id;
30     }
31     
32     @Override
33     public int type() {
34         return RequestFlags.INVALIDATE;
35     }
36     
37     @Override
38     final public boolean equals(Object object) {
39         if (this == object)
40             return true;
41         else if (object == null)
42             return false;
43         else if (getClass() != object.getClass())
44             return false;
45         UnaryQuery<?> other = (UnaryQuery<?>)object;
46         return id == other.id;
47     }
48     
49     @Override
50     final public Query getQuery() {
51         assert(!isDiscarded());
52         return this;
53     }
54     
55     @Override
56     public void recompute(ReadGraphImpl graph, Object provider, CacheEntry entry) {
57         recompute(graph, (QueryProcessor)provider);
58     }
59     
60     @Override
61     public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
62         performFromCache(graph, (QueryProcessor)provider, (Procedure)procedure);
63     }
64     
65     abstract public void recompute(ReadGraphImpl graph, QueryProcessor provider);
66     abstract public Object computeForEach(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure, boolean store);
67     abstract public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, Procedure procedure);
68     abstract public void putEntry(QueryProcessor provider);
69     abstract public void removeEntry(QueryProcessor provider);
70     abstract public UnaryQuery<Procedure> getEntry(QueryProcessor provider);
71         
72     public Object get(ReadGraphImpl graph, QueryProcessor processor, Object procedure) throws Throwable {
73         if(procedure != null) {
74             performFromCache(graph, processor, procedure);
75         } else {
76             checkAndThrow();
77         }
78         return getResult();
79     }
80     
81     @Override
82     boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
83                 return graph.processor.isImmutable(id);
84     }
85     
86 }