]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/InternalStatement.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / InternalStatement.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.impl.graph;\r
13 \r
14 import org.simantics.db.Resource;\r
15 import org.simantics.db.Statement;\r
16 import org.simantics.db.impl.ResourceImpl;\r
17 import org.simantics.db.impl.support.ResourceSupport;\r
18 \r
19 public final class InternalStatement implements Statement, Comparable<Statement> {\r
20         \r
21         final ResourceSupport support;\r
22     final int s;\r
23     final int p;\r
24     final int o;\r
25     \r
26     public InternalStatement(ResourceSupport support, final int s, final int p, final int o) {\r
27         this.support = support;\r
28         this.s = s;\r
29         this.p = p;\r
30         this.o = o;\r
31     }\r
32     \r
33         @Override\r
34         public Resource getSubject() {\r
35                 return new ResourceImpl(support, s);\r
36         }\r
37 \r
38         @Override\r
39         public Resource getPredicate() {\r
40                 return new ResourceImpl(support, p);\r
41         }\r
42 \r
43         @Override\r
44         public Resource getObject() {\r
45                 return new ResourceImpl(support, o);\r
46         }\r
47 \r
48         @Override\r
49         public boolean isAsserted(Resource subject) {\r
50                 return ((ResourceImpl)subject).id != s;\r
51         }\r
52     \r
53     @Override\r
54     final public int hashCode() {\r
55         return 41 * ( 31 * s + p) + o;\r
56     }\r
57 \r
58     final private int id(Resource r) {\r
59         return ((ResourceImpl)r).id;\r
60     }\r
61     \r
62     @Override\r
63     public boolean equals(Object object) {\r
64         if (this == object)\r
65             return true;\r
66         else if (object == null)\r
67             return false;\r
68         else if (!(object instanceof Statement))\r
69             return false;\r
70         Statement r = (Statement)object;\r
71         return s == id(r.getSubject()) && p == id(r.getPredicate()) && o== id(r.getObject());\r
72     }\r
73     \r
74     @Override\r
75     public int compareTo(Statement other) {\r
76         int result = compare(s,id(other.getSubject()));\r
77         if(result != 0) return result;\r
78         result = compare(p,id(other.getPredicate()));\r
79         if(result != 0) return result;\r
80         return compare(o,id(other.getObject()));\r
81     }\r
82 \r
83     private int compare(int x, int y) {\r
84         return (x < y) ? -1 : ((x == y) ? 0 : 1);\r
85     }\r
86 \r
87 }\r
88 \r