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.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
18 import org.simantics.db.Resource;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
23 public class Conjunction extends UnaryPredicate {
25 Collection<IUnaryPredicate> predicates;
26 IUnaryPredicate support;
28 public Conjunction(Collection<IUnaryPredicate> predicates) {
29 for(IUnaryPredicate pred : predicates)
30 if(pred.supportsUnboundedQuery()) {
32 this.predicates = new ArrayList<IUnaryPredicate>(predicates.size()-1);
33 for(IUnaryPredicate pred2 : predicates)
35 this.predicates.add(pred2);
38 this.predicates = predicates;
42 public Conjunction(IUnaryPredicate ... predicates) {
43 this(Arrays.asList(predicates));
47 public Collection<Resource> getResources(ReadGraph g) throws DatabaseException {
48 Collection<Resource> result = new ArrayList<Resource>();
49 ll: for(Resource r : support.getResources(g)) {
50 for(IUnaryPredicate pred : predicates)
59 public boolean has(ReadGraph g, Resource resource) throws DatabaseException {
60 for(IUnaryPredicate pred : predicates)
61 if(!pred.has(g, resource))
63 return support == null || support.has(g, resource);
67 public boolean supportsUnboundedQuery() {
68 return support != null;
72 public void add(WriteGraph g, Resource r) {
73 throw new UnsupportedOperationException();
77 public void remove(WriteGraph g, Resource r) {
78 throw new UnsupportedOperationException();
82 public boolean supportsAddition() {
87 public boolean supportsRemoval() {
92 public int hashCode() {
95 result = prime * result
96 + ((predicates == null) ? 0 : predicates.hashCode());
101 public boolean equals(Object obj) {
106 if (getClass() != obj.getClass())
108 Conjunction other = (Conjunction) obj;
109 if (predicates == null) {
110 if (other.predicates != null)
112 } else if (!predicates.equals(other.predicates))