]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/InternalStatement.java
Disabled BOOKKEEPING flag for normal use
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / InternalStatement.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.graph;
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.impl.support.ResourceSupport;
18
19 public final class InternalStatement implements Statement, Comparable<Statement> {
20         
21         final ResourceSupport support;
22     final int s;
23     final int p;
24     final int o;
25     
26     public InternalStatement(ResourceSupport support, final int s, final int p, final int o) {
27         this.support = support;
28         this.s = s;
29         this.p = p;
30         this.o = o;
31     }
32     
33         @Override
34         public Resource getSubject() {
35                 return new ResourceImpl(support, s);
36         }
37
38         @Override
39         public Resource getPredicate() {
40                 return new ResourceImpl(support, p);
41         }
42
43         @Override
44         public Resource getObject() {
45                 return new ResourceImpl(support, o);
46         }
47
48         @Override
49         public boolean isAsserted(Resource subject) {
50                 return ((ResourceImpl)subject).id != s;
51         }
52     
53     @Override
54     final public int hashCode() {
55         return 41 * ( 31 * s + p) + o;
56     }
57
58     final private int id(Resource r) {
59         return ((ResourceImpl)r).id;
60     }
61     
62     @Override
63     public boolean equals(Object object) {
64         if (this == object)
65             return true;
66         else if (object == null)
67             return false;
68         else if (!(object instanceof Statement))
69             return false;
70         Statement r = (Statement)object;
71         return s == id(r.getSubject()) && p == id(r.getPredicate()) && o== id(r.getObject());
72     }
73     
74     @Override
75     public int compareTo(Statement other) {
76         int result = compare(s,id(other.getSubject()));
77         if(result != 0) return result;
78         result = compare(p,id(other.getPredicate()));
79         if(result != 0) return result;
80         return compare(o,id(other.getObject()));
81     }
82
83     private int compare(int x, int y) {
84         return (x < y) ? -1 : ((x == y) ? 0 : 1);
85     }
86
87 }
88