]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/StatementChangeImpl.java
DB-client fixes
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / StatementChangeImpl.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 fi.vtt.simantics.procore.internal;\r
13 \r
14 import org.simantics.db.ChangeSet.StatementChange;\r
15 import org.simantics.db.Resource;\r
16 \r
17 /**\r
18  * Intentionally implements hashCode and equals so that the {@link #claim}\r
19  * field is not counted as an ID of this instance.\r
20  * \r
21  * @author Tuukka Lehtonen\r
22  */\r
23 final class StatementChangeImpl implements StatementChange {\r
24 \r
25     private final Resource s;\r
26     private final Resource p;\r
27     private final Resource o;\r
28 \r
29     /**\r
30      * Intentionally left package-modifiable through {@link #setClaim(boolean)}\r
31      * as an implementation-internal optimization by allowing reuse of the same\r
32      * StatementChangeImpl instance.\r
33      */\r
34     private boolean claim;\r
35 \r
36     public StatementChangeImpl(final SessionImplSocket session, final int s, final int p, final int o, boolean claim) {\r
37         this.s = session.getResource(s);\r
38         this.p = session.getResource(p);\r
39         this.o = session.getResource(o);\r
40         this.claim = claim;\r
41     }\r
42     public StatementChangeImpl(final Resource s, final Resource p, final Resource o, boolean claim) {\r
43         this.s = s;\r
44         this.p = p;\r
45         this.o = o;\r
46         this.claim = claim;\r
47     }\r
48 \r
49     @Override\r
50     public Resource getObject() {\r
51         return o;\r
52     }\r
53 \r
54     @Override\r
55     final public Resource getPredicate() {\r
56         return p;\r
57     }\r
58 \r
59     @Override\r
60     final public Resource getSubject() {\r
61         return s;\r
62     }\r
63 \r
64     void setClaim(boolean claim) {\r
65         this.claim = claim;\r
66     }\r
67 \r
68     public boolean isClaim() {\r
69         return claim;\r
70     }\r
71 \r
72     @Override\r
73     public boolean isAsserted(Resource testSubject) {\r
74         return !s.equals(testSubject);\r
75     }\r
76 \r
77     @Override\r
78     public int hashCode() {\r
79         return hashCode(s, p, o);\r
80     }\r
81     \r
82     public static int hashCode(Resource s, Resource p, Resource o) {\r
83         return 41 * ( 31 * s.hashCode() + p.hashCode()) + o.hashCode();\r
84     }\r
85 \r
86     @Override\r
87     public boolean equals(Object object) {\r
88         if (this == object)\r
89             return true;\r
90         else if (object == null)\r
91             return false;\r
92         else if (StatementChangeImpl.class != object.getClass())\r
93             return false;\r
94         StatementChangeImpl r = (StatementChangeImpl) object;\r
95         return getSubject().equals(r.getSubject())\r
96         && getPredicate().equals(r.getPredicate())\r
97         && getObject().equals(r.getObject());\r
98     }\r
99 }\r
100 \r