View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.framework.engine;
17  
18  /**
19   * <p>Enumeration for simple collection operators used by {@link CollectionOfComparablesTermBasedProposition}.  The
20   * operators encapsulate logic for how to collate results and when to short circuit as a collection is being
21   * processed.  Correct usage is best summarized by this code block:</p>
22   * <pre>
23   * for (Comparable<T> item : comparableItems) {
24   *     collatedResult = collectionOper.reduce(compare(item, compareValue), collatedResult);
25   *     if (collectionOper.shortCircuit(collatedResult)) break;
26   * }
27   * </pre>
28   * 
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   *
31   */
32  public enum CollectionOperator {
33  
34  	ONE_OR_MORE(false) {
35  		@Override
36  		public boolean reduce(boolean elementResult, boolean collatedResult) {
37  			return elementResult || collatedResult;
38  		}
39  		
40  		@Override
41  		public boolean shortCircuit(boolean collatedResult) {
42  			return collatedResult;
43  		}
44  	},
45  	
46  	ALL(true) {
47  		@Override
48  		public boolean reduce(boolean elementResult, boolean collatedResult) {
49  			return elementResult && collatedResult;
50  		}
51  
52  		@Override
53  		public boolean shortCircuit(boolean collatedResult) {
54  			return !collatedResult;
55  		}
56  	},
57  	
58  	NONE(true) {
59  		@Override
60  		public boolean reduce(boolean elementResult, boolean collatedResult) {
61  			return !elementResult && collatedResult;
62  		}
63  
64  		@Override
65  		public boolean shortCircuit(boolean collatedResult) {
66  			return !collatedResult;
67  		}
68  	};
69  	
70  	private final boolean initialCollationResult;
71  	
72  	private CollectionOperator(boolean initialCollationResult) {
73  		this.initialCollationResult = initialCollationResult;
74  	}
75  	
76  	/**
77  	 * This method takes the collated result thus far and the result for the next element,
78  	 * and produces the next collated result.
79  	 * 
80  	 * @return the new collated result
81  	 */
82  	public abstract boolean reduce(boolean elementResult, boolean collatedResult);
83  	
84  	/**
85  	 * This method lets the engine know if it can short circuit its iteration through the list based on the 
86  	 * collated result.  The condition when short circuiting can be done varies with the operator.
87  	 * 
88  	 * @param collatedResult
89  	 * @return true if short circuiting can be done to optimize processing
90  	 */
91  	public abstract boolean shortCircuit(boolean collatedResult);
92  	
93  	/**
94  	 * when the result for the first item in the collection is calculated, there isn't yet a collated result 
95  	 * to use in the {@link #reduce(boolean, boolean)} method.  Different operators require different
96  	 * initial values to function correctly, so this property holds the correct initial collated value for the 
97  	 * given operator instance.
98  	 */
99  	public boolean getInitialCollatedResult() {
100 		return initialCollationResult;
101 	}
102 	
103 }