Dispose ClientChangesImpl ChangeSets to minimize memory footprint
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / DirectStatementsImpl.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 gnu.trove.list.array.TIntArrayList;
15
16 import java.util.Collection;
17 import java.util.Iterator;
18
19 import org.simantics.db.DirectStatements;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Statement;
22 import org.simantics.db.impl.ResourceImpl;
23 import org.simantics.db.impl.support.ResourceSupport;
24
25 final public class DirectStatementsImpl implements DirectStatements {
26
27         final ResourceSupport support;
28         final int subject;
29         final TIntArrayList statements = new TIntArrayList();
30         
31         DirectStatementsImpl(ResourceSupport support, int subject) {
32                 this.support = support;
33                 this.subject = subject;
34         }
35         
36         void addStatement(int p, int o) {
37                 statements.add(p);
38                 statements.add(o);
39         }
40         
41         @Override
42         public Resource getSubject() {
43                 return new ResourceImpl(support, subject);
44         }
45
46         @Override
47         public boolean add(Statement arg0) {
48                 throw new Error("Not supported");
49         }
50
51         @Override
52         public boolean addAll(Collection<? extends Statement> arg0) {
53                 throw new Error("Not supported");
54         }
55
56         @Override
57         public void clear() {
58                 throw new Error("Not supported");
59         }
60
61         @Override
62         public boolean contains(Object arg0) {
63         Statement stm = (Statement)arg0;
64         ResourceImpl p = (ResourceImpl)stm.getPredicate();
65         ResourceImpl o = (ResourceImpl)stm.getObject();
66         for(int i=0;i<statements.size();i+=2) {
67             int pi = statements.getQuick(i);
68             int oi = statements.getQuick(i+1);
69             if(p.id == pi && o.id == oi) return true;
70         }
71         return false;
72         }
73
74         @Override
75         public boolean containsAll(Collection<?> arg0) {
76                 throw new Error("Not supported");
77         }
78
79         @Override
80         public boolean isEmpty() {
81                 throw new Error("Not supported");
82         }
83
84         @Override
85         public Iterator<Statement> iterator() {
86                 return new Iterator<Statement>() {
87
88                         int index = 0;
89                         int max = statements.size();
90                         
91                         @Override
92                         public boolean hasNext() {
93                                 return index < max; 
94                         }
95
96                         @Override
97                         public Statement next() {
98                                 return new DirectStatementImpl(support, subject, statements.getQuick(index++), statements.getQuick(index++));
99                         }
100
101                         @Override
102                         public void remove() {
103                                 throw new Error("Not supported");
104                         }
105                         
106                 };
107         }
108
109         @Override
110         public boolean remove(Object arg0) {
111                 throw new Error("Not supported");
112         }
113
114         @Override
115         public boolean removeAll(Collection<?> arg0) {
116                 throw new Error("Not supported");
117         }
118
119         @Override
120         public boolean retainAll(Collection<?> arg0) {
121                 throw new Error("Not supported");
122         }
123
124         @Override
125         public int size() {
126                 return statements.size() >> 1;
127         }
128
129         @Override
130         public Object[] toArray() {
131                 throw new Error("Not supported");
132         }
133
134         @Override
135         public <T> T[] toArray(T[] arg0) {
136                 throw new Error("Not supported");
137         }
138         
139 }