View Javadoc

1   package org.kuali.rice.krms.framework.engine;
2   
3   /**
4    * <p>Enumeration for simple collection operators used by {@link CollectionOfComparablesTermBasedProposition}.  The
5    * operators encapsulate logic for how to collate results and when to short circuit as a collection is being
6    * processed.  Correct usage is best summarized by this code block:</p>
7    * <pre>
8    * for (Comparable<T> item : comparableItems) {
9    *     collatedResult = collectionOper.reduce(compare(item, compareValue), collatedResult);
10   *     if (collectionOper.shortCircuit(collatedResult)) break;
11   * }
12   * </pre>
13   * 
14   * @author Kuali Rice Team (rice.collab@kuali.org)
15   *
16   */
17  public enum CollectionOperator {
18  
19  	ONE_OR_MORE(false) {
20  		@Override
21  		public boolean reduce(boolean elementResult, boolean collatedResult) {
22  			return elementResult || collatedResult;
23  		}
24  		
25  		@Override
26  		public boolean shortCircuit(boolean collatedResult) {
27  			return collatedResult;
28  		}
29  	},
30  	
31  	ALL(true) {
32  		@Override
33  		public boolean reduce(boolean elementResult, boolean collatedResult) {
34  			return elementResult && collatedResult;
35  		}
36  
37  		@Override
38  		public boolean shortCircuit(boolean collatedResult) {
39  			return !collatedResult;
40  		}
41  	},
42  	
43  	NONE(true) {
44  		@Override
45  		public boolean reduce(boolean elementResult, boolean collatedResult) {
46  			return !elementResult && collatedResult;
47  		}
48  
49  		@Override
50  		public boolean shortCircuit(boolean collatedResult) {
51  			return !collatedResult;
52  		}
53  	};
54  	
55  	private final boolean initialCollationResult;
56  	
57  	private CollectionOperator(boolean initialCollationResult) {
58  		this.initialCollationResult = initialCollationResult;
59  	}
60  	
61  	/**
62  	 * This method takes the collated result thus far and the result for the next element,
63  	 * and produces the next collated result.
64  	 * 
65  	 * @return the new collated result
66  	 */
67  	public abstract boolean reduce(boolean elementResult, boolean collatedResult);
68  	
69  	/**
70  	 * This method lets the engine know if it can short circuit its iteration through the list based on the 
71  	 * collated result.  The condition when short circuiting can be done varies with the operator.
72  	 * 
73  	 * @param collatedResult
74  	 * @return true if short circuiting can be done to optimize processing
75  	 */
76  	public abstract boolean shortCircuit(boolean collatedResult);
77  	
78  	/**
79  	 * when the result for the first item in the collection is calculated, there isn't yet a collated result 
80  	 * to use in the {@link #reduce(boolean, boolean)} method.  Different operators require different
81  	 * initial values to function correctly, so this property holds the correct initial collated value for the 
82  	 * given operator instance.
83  	 */
84  	public boolean getInitialCollatedResult() {
85  		return initialCollationResult;
86  	}
87  	
88  }