Path: blob/master/sourcetools/com.ibm.uma/com/ibm/uma/om/PredicateList.java
6004 views
/*******************************************************************************1* Copyright (c) 2001, 2017 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.uma.om;2223import java.util.Vector;2425import com.ibm.uma.UMA;26import com.ibm.uma.UMAException;272829public class PredicateList {30Vector<Predicate> predicates = new Vector<Predicate>();31String containingFile;32boolean cachedResult;33boolean resultHasBeenCached = false;3435public PredicateList(String containingFile) {36this.containingFile = containingFile;37}3839public String getContainingFile() {40return containingFile;41}4243public boolean evaluate() throws UMAException {44if ( resultHasBeenCached ) {45return cachedResult;46}47// if the there are no predicates return true;48if ( predicates.size() < 1 ) {49cachedResult = true;50resultHasBeenCached = true;51return cachedResult;52}5354// the last true result in the list wins.55// if the last true result is for an exclude-if then return false56// if the last true result is for an include-if then return true57// if no predicates evaluated to true then58// - return false if all predicates were include-if59// - return true if all predicates were exclude-if60// - return false otherwise61boolean includeIfPresent = false;62for ( int i=predicates.size()-1; i>=0; i-- ) {63Predicate predicate = predicates.elementAt(i);64if ( evalutePredicate( predicate.getPredicate() ) ) {65switch ( predicate.getType() ) {66case Predicate.EXCLUDE_IF: {67cachedResult = false;68resultHasBeenCached = true;69return cachedResult;70}71case Predicate.INCLUDE_IF: {72cachedResult = true;73resultHasBeenCached = true;74return cachedResult;75}76}77} else if ( predicate.getType() == Predicate.INCLUDE_IF ) {78includeIfPresent = true;79}80}8182// an include-if was present, but no condition was satisfied83if ( includeIfPresent ) {84cachedResult = false;85resultHasBeenCached = true;86return cachedResult;87}8889// no include-if was present90cachedResult = true;91resultHasBeenCached = true;92return cachedResult;93}9495boolean evaluateSinglePredicate(String predicate) throws UMAException {96return UMA.getUma().getSinglePredicateEvaluator().evaluateSinglePredicate(predicate);97}9899boolean evalutePredicate(String predicate) throws UMAException {100String[] preds = predicate.split("\\s");101boolean result = true;102boolean predicateFound = false;103boolean andOp = false;104boolean orOp = false;105boolean xorOp = false;106boolean notOp = false;107for ( String pred : preds ) {108if ( pred.equalsIgnoreCase("and") ) {109if ( orOp || xorOp || notOp || !predicateFound ) {110throw new UMAException("Error: badly formed predicate [" + predicate + "] in " + containingFile );111}112andOp = true;113continue;114} else if ( pred.equalsIgnoreCase("or") ) {115if ( andOp || xorOp || notOp || !predicateFound ) {116throw new UMAException("Error: badly formed predicate [" + predicate + "] in " + containingFile );117}118orOp = true;119continue;120} else if ( pred.equalsIgnoreCase("xor") ) {121if ( andOp || orOp || notOp || !predicateFound ) {122throw new UMAException("Error: badly formed predicate [" + predicate + "] in " + containingFile );123}124xorOp = true;125continue;126}else if ( pred.equalsIgnoreCase("not") ) {127notOp = true;128continue;129} else {130predicateFound = true;131}132if ( andOp ) {133result = result &&134( notOp ? !evaluateSinglePredicate(pred) :135evaluateSinglePredicate(pred));136} else if ( orOp ) {137result = result ||138( notOp ? !evaluateSinglePredicate(pred) :139evaluateSinglePredicate(pred) );140} else if ( xorOp ) {141result = result ^142( notOp ? !evaluateSinglePredicate(pred) :143evaluateSinglePredicate(pred) );144} else {145result = notOp ? !evaluateSinglePredicate(pred) :146evaluateSinglePredicate(pred);147}148notOp = false;149orOp = false;150andOp = false;151xorOp = false;152}153return result;154}155156public void add(Predicate predicate) {157predicates.add(predicate);158}159}160161162