X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.layer0.utils%2Fsrc%2Forg%2Fsimantics%2Flayer0%2Futils%2Fpredicates%2FConjunction.java;h=ea8e0bb4f0c79a6c63a52d3eaf7fd69ab2b097c7;hb=refs%2Fchanges%2F38%2F238%2F2;hp=034ecbfaf0c85707643e1e58cdb6f461f039c2a7;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/predicates/Conjunction.java b/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/predicates/Conjunction.java index 034ecbfaf..ea8e0bb4f 100644 --- a/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/predicates/Conjunction.java +++ b/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/predicates/Conjunction.java @@ -1,117 +1,117 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.layer0.utils.predicates; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; - -import org.simantics.db.Resource; -import org.simantics.db.ReadGraph; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; - -public class Conjunction extends UnaryPredicate { - - Collection predicates; - IUnaryPredicate support; - - public Conjunction(Collection predicates) { - for(IUnaryPredicate pred : predicates) - if(pred.supportsUnboundedQuery()) { - this.support = pred; - this.predicates = new ArrayList(predicates.size()-1); - for(IUnaryPredicate pred2 : predicates) - if(pred != pred2) - this.predicates.add(pred2); - return; - } - this.predicates = predicates; - this.support = null; - } - - public Conjunction(IUnaryPredicate ... predicates) { - this(Arrays.asList(predicates)); - } - - @Override - public Collection getResources(ReadGraph g) throws DatabaseException { - Collection result = new ArrayList(); - ll: for(Resource r : support.getResources(g)) { - for(IUnaryPredicate pred : predicates) - if(!pred.has(g, r)) - continue ll; - result.add(r); - } - return result; - } - - @Override - public boolean has(ReadGraph g, Resource resource) throws DatabaseException { - for(IUnaryPredicate pred : predicates) - if(!pred.has(g, resource)) - return false; - return support == null || support.has(g, resource); - } - - @Override - public boolean supportsUnboundedQuery() { - return support != null; - } - - @Override - public void add(WriteGraph g, Resource r) { - throw new UnsupportedOperationException(); - } - - @Override - public void remove(WriteGraph g, Resource r) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean supportsAddition() { - return false; - } - - @Override - public boolean supportsRemoval() { - return false; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result - + ((predicates == null) ? 0 : predicates.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - Conjunction other = (Conjunction) obj; - if (predicates == null) { - if (other.predicates != null) - return false; - } else if (!predicates.equals(other.predicates)) - return false; - return true; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.layer0.utils.predicates; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; + +import org.simantics.db.Resource; +import org.simantics.db.ReadGraph; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; + +public class Conjunction extends UnaryPredicate { + + Collection predicates; + IUnaryPredicate support; + + public Conjunction(Collection predicates) { + for(IUnaryPredicate pred : predicates) + if(pred.supportsUnboundedQuery()) { + this.support = pred; + this.predicates = new ArrayList(predicates.size()-1); + for(IUnaryPredicate pred2 : predicates) + if(pred != pred2) + this.predicates.add(pred2); + return; + } + this.predicates = predicates; + this.support = null; + } + + public Conjunction(IUnaryPredicate ... predicates) { + this(Arrays.asList(predicates)); + } + + @Override + public Collection getResources(ReadGraph g) throws DatabaseException { + Collection result = new ArrayList(); + ll: for(Resource r : support.getResources(g)) { + for(IUnaryPredicate pred : predicates) + if(!pred.has(g, r)) + continue ll; + result.add(r); + } + return result; + } + + @Override + public boolean has(ReadGraph g, Resource resource) throws DatabaseException { + for(IUnaryPredicate pred : predicates) + if(!pred.has(g, resource)) + return false; + return support == null || support.has(g, resource); + } + + @Override + public boolean supportsUnboundedQuery() { + return support != null; + } + + @Override + public void add(WriteGraph g, Resource r) { + throw new UnsupportedOperationException(); + } + + @Override + public void remove(WriteGraph g, Resource r) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean supportsAddition() { + return false; + } + + @Override + public boolean supportsRemoval() { + return false; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((predicates == null) ? 0 : predicates.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Conjunction other = (Conjunction) obj; + if (predicates == null) { + if (other.predicates != null) + return false; + } else if (!predicates.equals(other.predicates)) + return false; + return true; + } + +}