Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LookupCustomizer |
|
| 1.1538461538461537;1.154 | ||||
LookupCustomizer$1 |
|
| 1.1538461538461537;1.154 | ||||
LookupCustomizer$Builder |
|
| 1.1538461538461537;1.154 | ||||
LookupCustomizer$IndentityTransform |
|
| 1.1538461538461537;1.154 | ||||
LookupCustomizer$Transform |
|
| 1.1538461538461537;1.154 |
1 | /** | |
2 | * Copyright 2005-2011 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.core.api.criteria; | |
17 | ||
18 | import org.kuali.rice.core.api.mo.ModelBuilder; | |
19 | ||
20 | import java.io.Serializable; | |
21 | ||
22 | /** | |
23 | * This class works withe the lookup framework to customize a query. It currently can do the following things: | |
24 | * | |
25 | * <ul> | |
26 | * <li>transform/remove predicates</li> | |
27 | * <li>transform/remove query results</li> | |
28 | * </ul> | |
29 | * | |
30 | * <p> | |
31 | * The predicate transform will applied to the predicates in the incoming query yielding a | |
32 | * a predicate. If the predicate does not need to be transformed then the function can return the | |
33 | * incoming argument. If the predicate should be removed the transform should return null. | |
34 | * This is a way to remove or change a predicate before the query is executed. | |
35 | * This is a good way to add new predicates to a query or to limit the result of the query before | |
36 | * it is executed. Transforms also allow a predicate referencing a property path that does not exist | |
37 | * on database mapped object to be changed to something that is valid. | |
38 | * </p> | |
39 | * | |
40 | * <p> | |
41 | * The result transform will be applied to the results of the query after the query is executed. | |
42 | * If the result does not need to be transformed then the function can return the | |
43 | * incoming argument. This is a way to remove or change a result after the query is executed. | |
44 | * </p> | |
45 | * | |
46 | * <p>transformers should not have to deal with null items</p> | |
47 | */ | |
48 | 0 | public class LookupCustomizer<T> { |
49 | ||
50 | //FIXME: add wilcards to make predicate transform more flexible ie. EqualsPredicate to AndPredicate | |
51 | private final Transform<Predicate,Predicate> predicateTransform; | |
52 | private final Transform<T, T> resultTransform; | |
53 | ||
54 | 0 | private LookupCustomizer(Builder<T> builder) { |
55 | 0 | this.predicateTransform = builder.getPredicateTransform() != null ? builder.getPredicateTransform() : IndentityTransform.<Predicate, Predicate>getInstance(); |
56 | 0 | this.resultTransform = builder.getResultTransform() != null ? builder.getResultTransform() : IndentityTransform.<T, T>getInstance(); |
57 | 0 | } |
58 | ||
59 | public Transform<Predicate, Predicate> getPredicateTransform() { | |
60 | 0 | return predicateTransform; |
61 | } | |
62 | ||
63 | public Transform<T, T> getResultTransform() { | |
64 | 0 | return resultTransform; |
65 | } | |
66 | ||
67 | 0 | public static final class Builder<T> implements ModelBuilder, Serializable { |
68 | ||
69 | private Transform<Predicate, Predicate> predicateTransform; | |
70 | private Transform<T, T> resultTransform; | |
71 | ||
72 | 0 | private Builder() { |
73 | ||
74 | 0 | } |
75 | ||
76 | public static <T> Builder<T> create() { | |
77 | 0 | return new Builder<T>(); |
78 | } | |
79 | ||
80 | public Transform<Predicate, Predicate> getPredicateTransform() { | |
81 | 0 | return predicateTransform; |
82 | } | |
83 | ||
84 | public void setPredicateTransform(final Transform<Predicate, Predicate> predicateTransform) { | |
85 | 0 | this.predicateTransform = predicateTransform; |
86 | 0 | } |
87 | ||
88 | public Transform<T, T> getResultTransform() { | |
89 | 0 | return resultTransform; |
90 | } | |
91 | ||
92 | public void setResultTransform(final Transform<T, T> resultTransform) { | |
93 | 0 | this.resultTransform = resultTransform; |
94 | 0 | } |
95 | ||
96 | @Override | |
97 | public LookupCustomizer<T> build() { | |
98 | 0 | return new LookupCustomizer<T>(this); |
99 | } | |
100 | } | |
101 | public interface Transform<P, R> { | |
102 | R apply(P input); | |
103 | } | |
104 | ||
105 | /** | |
106 | * f: x -> x. This function just returns the passed in parameter. | |
107 | * | |
108 | * @param <I> the type the function acts on. | |
109 | */ | |
110 | 0 | private static final class IndentityTransform<I> implements Transform<I, I> { |
111 | ||
112 | @SuppressWarnings("unchecked") | |
113 | 0 | private static final Transform INSTANCE = new IndentityTransform(); |
114 | ||
115 | @SuppressWarnings("unchecked") | |
116 | public static <P, R> Transform<P, R> getInstance() { | |
117 | 0 | return INSTANCE; |
118 | } | |
119 | ||
120 | @Override | |
121 | public I apply(final I input) { | |
122 | 0 | return input; |
123 | } | |
124 | } | |
125 | } |