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