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