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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.layer0.utils.predicates;
\r
14 import java.util.Arrays;
\r
15 import java.util.Collection;
\r
16 import java.util.Collections;
\r
17 import java.util.HashSet;
\r
18 import java.util.Iterator;
\r
19 import java.util.Set;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.ReadGraph;
\r
23 import org.simantics.db.WriteGraph;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
26 public class Disjunction extends UnaryPredicate {
\r
28 Collection<IUnaryPredicate> predicates;
\r
30 public Disjunction(Collection<IUnaryPredicate> predicates) {
\r
31 this.predicates = predicates;
\r
34 public Disjunction(IUnaryPredicate ... predicates) {
\r
35 this(Arrays.asList(predicates));
\r
39 public Collection<Resource> getResources(ReadGraph g) throws DatabaseException {
\r
40 Iterator<IUnaryPredicate> it = predicates.iterator();
\r
42 while(it.hasNext()) {
\r
43 Collection<Resource> result = it.next().getResources(g);
\r
44 if(!result.isEmpty()) {
\r
45 while(it.hasNext()) {
\r
46 Collection<Resource> temp = it.next().getResources(g);
\r
47 if(!temp.isEmpty()) {
\r
48 Set<Resource> merged = new HashSet<Resource>(result.size() + temp.size());
\r
49 merged.addAll(result);
\r
50 merged.addAll(temp);
\r
52 merged.addAll(it.next().getResources(g));
\r
59 return Collections.emptyList();
\r
63 public boolean has(ReadGraph g, Resource resource) throws DatabaseException {
\r
64 for(IUnaryPredicate pred : predicates)
\r
65 if(pred.has(g, resource))
\r
71 public boolean supportsUnboundedQuery() {
\r
72 for(IUnaryPredicate pred : predicates)
\r
73 if(!pred.supportsUnboundedQuery())
\r
79 public void add(WriteGraph g, Resource r) {
\r
80 throw new UnsupportedOperationException();
\r
84 public void remove(WriteGraph g, Resource r) {
\r
85 throw new UnsupportedOperationException();
\r
89 public boolean supportsAddition() {
\r
94 public boolean supportsRemoval() {
\r
99 public int hashCode() {
\r
100 final int prime = 31;
\r
102 result = prime * result
\r
103 + ((predicates == null) ? 0 : predicates.hashCode());
\r
108 public boolean equals(Object obj) {
\r
113 if (getClass() != obj.getClass())
\r
115 Disjunction other = (Disjunction) obj;
\r
116 if (predicates == null) {
\r
117 if (other.predicates != null)
\r
119 } else if (!predicates.equals(other.predicates))
\r