1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.layer0.utils.binaryPredicates;
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;
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;
27 public class UnionPredicate extends BinaryPredicate {
29 Collection<IBinaryPredicate> predicates;
31 public UnionPredicate(Collection<IBinaryPredicate> predicates) {
32 this.predicates = predicates;
35 public UnionPredicate(IBinaryPredicate ... predicates) {
36 this(Arrays.asList(predicates));
40 public Collection<Resource> getObjects(ReadGraph g, Resource subject) throws DatabaseException {
41 Iterator<IBinaryPredicate> it = predicates.iterator();
44 Collection<Resource> result = it.next().getObjects(g, subject);
45 if(!result.isEmpty()) {
47 Collection<Resource> temp = it.next().getObjects(g, subject);
49 Set<Resource> merged = new HashSet<Resource>(result.size() + temp.size());
50 merged.addAll(result);
53 merged.addAll(it.next().getObjects(g, subject));
60 return Collections.emptyList();
64 public Collection<Pair<Resource, Resource>> getStatements(ReadGraph g) throws DatabaseException {
65 Iterator<IBinaryPredicate> it = predicates.iterator();
68 Collection<Pair<Resource, Resource>> result = it.next().getStatements(g);
69 if(!result.isEmpty()) {
71 Collection<Pair<Resource, Resource>> temp = it.next().getStatements(g);
73 Set<Pair<Resource, Resource>> merged = new HashSet<Pair<Resource, Resource>>(result.size() + temp.size());
74 merged.addAll(result);
77 merged.addAll(it.next().getStatements(g));
84 return Collections.emptyList();
88 public Collection<Resource> getSubjects(ReadGraph g, Resource object) throws DatabaseException {
89 Iterator<IBinaryPredicate> it = predicates.iterator();
92 Collection<Resource> result = it.next().getSubjects(g, object);
93 if(!result.isEmpty()) {
95 Collection<Resource> temp = it.next().getSubjects(g, object);
97 Set<Resource> merged = new HashSet<Resource>(result.size() + temp.size());
98 merged.addAll(result);
101 merged.addAll(it.next().getSubjects(g, object));
108 return Collections.emptyList();
112 public boolean has(ReadGraph g, Resource subject, Resource object) throws DatabaseException {
113 for(IBinaryPredicate pred : predicates)
114 if(pred.has(g, subject, object))
120 public boolean supportsGetObjects() {
121 for(IBinaryPredicate pred : predicates)
122 if(!pred.supportsGetObjects())
128 public boolean supportsGetStatements() {
129 for(IBinaryPredicate pred : predicates)
130 if(!pred.supportsGetStatements())
136 public boolean supportsGetSubjects() {
137 for(IBinaryPredicate pred : predicates)
138 if(!pred.supportsGetSubjects())
144 public void add(WriteGraph g, Resource subject, Resource object) {
145 throw new UnsupportedOperationException();
149 public void remove(WriteGraph g, Resource subject, Resource object) {
150 throw new UnsupportedOperationException();
154 public boolean supportsAdditions() {
159 public boolean supportsRemovals() {
164 public int hashCode() {
165 final int prime = 31;
167 result = prime * result
168 + ((predicates == null) ? 0 : predicates.hashCode());
173 public boolean equals(Object obj) {
178 if (getClass() != obj.getClass())
180 UnionPredicate other = (UnionPredicate) obj;
181 if (predicates == null) {
182 if (other.predicates != null)
184 } else if (!predicates.equals(other.predicates))