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.predicates;
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;
26 public class Disjunction extends UnaryPredicate {
28 Collection<IUnaryPredicate> predicates;
30 public Disjunction(Collection<IUnaryPredicate> predicates) {
31 this.predicates = predicates;
34 public Disjunction(IUnaryPredicate ... predicates) {
35 this(Arrays.asList(predicates));
39 public Collection<Resource> getResources(ReadGraph g) throws DatabaseException {
40 Iterator<IUnaryPredicate> it = predicates.iterator();
43 Collection<Resource> result = it.next().getResources(g);
44 if(!result.isEmpty()) {
46 Collection<Resource> temp = it.next().getResources(g);
48 Set<Resource> merged = new HashSet<Resource>(result.size() + temp.size());
49 merged.addAll(result);
52 merged.addAll(it.next().getResources(g));
59 return Collections.emptyList();
63 public boolean has(ReadGraph g, Resource resource) throws DatabaseException {
64 for(IUnaryPredicate pred : predicates)
65 if(pred.has(g, resource))
71 public boolean supportsUnboundedQuery() {
72 for(IUnaryPredicate pred : predicates)
73 if(!pred.supportsUnboundedQuery())
79 public void add(WriteGraph g, Resource r) {
80 throw new UnsupportedOperationException();
84 public void remove(WriteGraph g, Resource r) {
85 throw new UnsupportedOperationException();
89 public boolean supportsAddition() {
94 public boolean supportsRemoval() {
99 public int hashCode() {
100 final int prime = 31;
102 result = prime * result
103 + ((predicates == null) ? 0 : predicates.hashCode());
108 public boolean equals(Object obj) {
113 if (getClass() != obj.getClass())
115 Disjunction other = (Disjunction) obj;
116 if (predicates == null) {
117 if (other.predicates != null)
119 } else if (!predicates.equals(other.predicates))