]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/binaryPredicates/UnionPredicate.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / binaryPredicates / UnionPredicate.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.layer0.utils.binaryPredicates;
13
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Iterator;
19 import java.util.Set;
20
21 import org.simantics.db.Resource;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.utils.datastructures.Pair;
26
27 public class UnionPredicate extends BinaryPredicate {
28
29         Collection<IBinaryPredicate> predicates;
30
31         public UnionPredicate(Collection<IBinaryPredicate> predicates) {
32                 this.predicates = predicates;
33         }
34         
35         public UnionPredicate(IBinaryPredicate ... predicates) {
36                 this(Arrays.asList(predicates));
37         }
38
39         @Override
40         public Collection<Resource> getObjects(ReadGraph g, Resource subject) throws DatabaseException {
41                 Iterator<IBinaryPredicate> it = predicates.iterator();
42                 
43                 while(it.hasNext()) {
44                         Collection<Resource> result = it.next().getObjects(g, subject);
45                         if(!result.isEmpty()) {
46                                 while(it.hasNext()) {
47                                         Collection<Resource> temp = it.next().getObjects(g, subject);
48                                         if(!temp.isEmpty()) {
49                                                 Set<Resource> merged = new HashSet<Resource>(result.size() + temp.size());
50                                                 merged.addAll(result);
51                                                 merged.addAll(temp);
52                                                 while(it.hasNext())
53                                                         merged.addAll(it.next().getObjects(g, subject));
54                                                 return merged;
55                                         }
56                                 }
57                                 return result;
58                         }
59                 }
60                 return Collections.emptyList();
61         }
62
63         @Override
64         public Collection<Pair<Resource, Resource>> getStatements(ReadGraph g) throws DatabaseException {
65                 Iterator<IBinaryPredicate> it = predicates.iterator();
66                 
67                 while(it.hasNext()) {
68                         Collection<Pair<Resource, Resource>> result = it.next().getStatements(g);
69                         if(!result.isEmpty()) {
70                                 while(it.hasNext()) {
71                                         Collection<Pair<Resource, Resource>> temp = it.next().getStatements(g);
72                                         if(!temp.isEmpty()) {
73                                                 Set<Pair<Resource, Resource>> merged = new HashSet<Pair<Resource, Resource>>(result.size() + temp.size());
74                                                 merged.addAll(result);
75                                                 merged.addAll(temp);
76                                                 while(it.hasNext())
77                                                         merged.addAll(it.next().getStatements(g));
78                                                 return merged;
79                                         }
80                                 }
81                                 return result;
82                         }
83                 }
84                 return Collections.emptyList();
85         }
86
87         @Override
88         public Collection<Resource> getSubjects(ReadGraph g, Resource object) throws DatabaseException {
89                 Iterator<IBinaryPredicate> it = predicates.iterator();
90                 
91                 while(it.hasNext()) {
92                         Collection<Resource> result = it.next().getSubjects(g, object);
93                         if(!result.isEmpty()) {
94                                 while(it.hasNext()) {
95                                         Collection<Resource> temp = it.next().getSubjects(g, object);
96                                         if(!temp.isEmpty()) {
97                                                 Set<Resource> merged = new HashSet<Resource>(result.size() + temp.size());
98                                                 merged.addAll(result);
99                                                 merged.addAll(temp);
100                                                 while(it.hasNext())
101                                                         merged.addAll(it.next().getSubjects(g, object));
102                                                 return merged;
103                                         }
104                                 }
105                                 return result;
106                         }
107                 }
108                 return Collections.emptyList();
109         }
110
111         @Override
112         public boolean has(ReadGraph g, Resource subject, Resource object) throws DatabaseException {
113                 for(IBinaryPredicate pred : predicates)
114                         if(pred.has(g, subject, object))
115                                 return true;
116                 return false;
117         }
118
119         @Override
120         public boolean supportsGetObjects() {
121                 for(IBinaryPredicate pred : predicates)
122                         if(!pred.supportsGetObjects())
123                                 return false;
124                 return true;
125         }
126
127         @Override
128         public boolean supportsGetStatements() {
129                 for(IBinaryPredicate pred : predicates)
130                         if(!pred.supportsGetStatements())
131                                 return false;
132                 return true;
133         }
134
135         @Override
136         public boolean supportsGetSubjects() {
137                 for(IBinaryPredicate pred : predicates)
138                         if(!pred.supportsGetSubjects())
139                                 return false;
140                 return true;
141         }
142
143         @Override
144         public void add(WriteGraph g, Resource subject, Resource object) {
145                 throw new UnsupportedOperationException();
146         }
147
148         @Override
149         public void remove(WriteGraph g, Resource subject, Resource object) {
150                 throw new UnsupportedOperationException();
151         }
152
153         @Override
154         public boolean supportsAdditions() {
155                 return false;
156         }
157
158         @Override
159         public boolean supportsRemovals() {
160                 return false;
161         }
162
163         @Override
164         public int hashCode() {
165                 final int prime = 31;
166                 int result = 1;
167                 result = prime * result
168                                 + ((predicates == null) ? 0 : predicates.hashCode());
169                 return result;
170         }
171
172         @Override
173         public boolean equals(Object obj) {
174                 if (this == obj)
175                         return true;
176                 if (obj == null)
177                         return false;
178                 if (getClass() != obj.getClass())
179                         return false;
180                 UnionPredicate other = (UnionPredicate) obj;
181                 if (predicates == null) {
182                         if (other.predicates != null)
183                                 return false;
184                 } else if (!predicates.equals(other.predicates))
185                         return false;
186                 return true;
187         }
188
189 }