View Javadoc

1   package org.kuali.student.r2.common.criteria;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import org.kuali.rice.core.api.criteria.Predicate;
8   import org.kuali.rice.core.api.mo.ModelBuilder;
9   import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
10  
11  public class LookupCustomizer<T> {
12      
13      private List<PredicateTransform> additionalTransforms;
14      private List<PredicateTransform> predicateTransforms;
15      private final Transform<T, T> resultTransform;
16  
17      private LookupCustomizer(Builder<T> builder) {
18          this.additionalTransforms = builder.getAdditionalTransforms();
19          this.predicateTransforms = builder.getPredicateTransforms();
20          this.resultTransform = builder.getResultTransform() != null ? builder.getResultTransform() : IndentityTransform.<T, T>getInstance();
21      }
22      
23      public Predicate applyAdditionalTransforms(Predicate predicate, Criteria criteria) {
24          for (PredicateTransform transform : additionalTransforms){
25              predicate = transform.apply(predicate, criteria);
26          }
27          return predicate;
28      }
29  
30      public Predicate applyPredicateTransforms(Predicate predicate, Criteria criteria) {
31          for (PredicateTransform transform : predicateTransforms){
32              predicate = transform.apply(predicate, criteria);
33          }
34          return predicate;
35      }
36  
37      public T applyResultTransforms(Object o) {
38          return null;
39      }
40      
41      public List<PredicateTransform> getAdditionalTransforms() {
42          return additionalTransforms;
43      }
44  
45      public void setAdditionalTransforms(List<PredicateTransform> additionalTransforms) {
46          this.additionalTransforms = additionalTransforms;
47      }
48  
49      public List<PredicateTransform> getPredicateTransforms() {
50          return predicateTransforms;
51      }
52  
53      public void setPredicateTransforms(List<PredicateTransform> predicateTransforms) {
54          this.predicateTransforms = predicateTransforms;
55      }
56  
57      public Transform<T, T> getResultTransform() {
58          return resultTransform;
59      }
60      
61      public static final class Builder<T> implements ModelBuilder, Serializable {
62  
63          private List<PredicateTransform> additionalTransforms;
64          private List<PredicateTransform> predicateTransforms;
65          private Transform<T, T> resultTransform;
66  
67          private Builder() {
68  
69          }
70  
71          public static <T> Builder<T> create() {
72              return new Builder<T>();
73          }
74  
75          public List<PredicateTransform> getAdditionalTransforms() {
76              return additionalTransforms;
77          }
78  
79          public void setAdditionalTransforms(List<PredicateTransform> additionalTransforms) {
80              this.additionalTransforms = additionalTransforms;
81          }
82  
83          public List<PredicateTransform> getPredicateTransforms() {
84              return predicateTransforms;
85          }
86  
87          public void setPredicateTransforms(List<PredicateTransform> predicateTransforms) {
88              this.predicateTransforms = predicateTransforms;
89          }
90  
91          public Transform<T, T> getResultTransform() {
92              return resultTransform;
93          }
94  
95          public void setResultTransform(final Transform<T, T> resultTransform) {
96              this.resultTransform = resultTransform;
97          }
98  
99          @Override
100         public LookupCustomizer<T> build() {
101             if (additionalTransforms == null){
102                 additionalTransforms = new ArrayList<LookupCustomizer.PredicateTransform>();
103             }
104             if (predicateTransforms == null){
105                 predicateTransforms = new ArrayList<LookupCustomizer.PredicateTransform>();
106             }
107             return new LookupCustomizer<T>(this);
108         }
109     }
110 
111     public interface PredicateTransform {
112         Predicate apply(Predicate input, Criteria criteria);
113     }
114     
115     public interface Transform<P, R> {
116         R apply(P input);
117     }
118 
119     /**
120      * f: x -> x.  This function just returns the passed in parameter.
121      *
122      * @param <I> the type the function acts on.
123      */
124     private static final class IndentityTransform<I> implements Transform<I, I> {
125 
126         @SuppressWarnings("unchecked")
127         private static final Transform INSTANCE = new IndentityTransform();
128 
129         @SuppressWarnings("unchecked")
130         public static <P, R> Transform<P, R> getInstance() {
131             return INSTANCE;
132         }
133 
134         @Override
135         public I apply(final I input) {
136             return input;
137         }
138     }
139 
140 }