Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PrimitiveRuleBuilder |
|
| 1.1;1.1 |
1 | /** | |
2 | The contents of this file are subject to the Mozilla Public License Version 1.1 | |
3 | (the "License"); you may not use this file except in compliance with the License. | |
4 | You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
5 | Software distributed under the License is distributed on an "AS IS" basis, | |
6 | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the | |
7 | specific language governing rights and limitations under the License. | |
8 | ||
9 | The Original Code is "PrimitiveRuleBuilder.java". Description: | |
10 | "Rule Builder for PrimitiveTypeRules." | |
11 | ||
12 | The Initial Developer of the Original Code is University Health Network. Copyright (C) | |
13 | 2012. All Rights Reserved. | |
14 | ||
15 | Contributor(s): ______________________________________. | |
16 | ||
17 | Alternatively, the contents of this file may be used under the terms of the | |
18 | GNU General Public License (the "GPL"), in which case the provisions of the GPL are | |
19 | applicable instead of those above. If you wish to allow use of your version of this | |
20 | file only under the terms of the GPL and not to allow others to use your version | |
21 | of this file under the MPL, indicate your decision by deleting the provisions above | |
22 | and replace them with the notice and other provisions required by the GPL License. | |
23 | If you do not delete the provisions above, a recipient may use your version of | |
24 | this file under either the MPL or the GPL. | |
25 | */ | |
26 | package ca.uhn.hl7v2.validation.builder; | |
27 | ||
28 | import java.util.ArrayList; | |
29 | import java.util.Collection; | |
30 | import java.util.List; | |
31 | import java.util.Set; | |
32 | ||
33 | import ca.uhn.hl7v2.Version; | |
34 | import ca.uhn.hl7v2.validation.PrimitiveTypeRule; | |
35 | import ca.uhn.hl7v2.validation.Rule; | |
36 | import ca.uhn.hl7v2.validation.builder.PredicatePrimitiveTypeRule.Trimmer; | |
37 | import ca.uhn.hl7v2.validation.impl.PrimitiveTypeRuleBinding; | |
38 | import ca.uhn.hl7v2.validation.impl.RuleBinding; | |
39 | ||
40 | /** | |
41 | * Rule builder for {@link PrimitiveTypeRule}s | |
42 | * | |
43 | * @author Christian Ohr | |
44 | */ | |
45 | 119740 | @SuppressWarnings("serial") |
46 | public class PrimitiveRuleBuilder extends RuleTypeBuilder<PrimitiveRuleBuilder, PrimitiveTypeRule> { | |
47 | ||
48 | private Set<String> types; | |
49 | ||
50 | protected PrimitiveRuleBuilder(List<RuleBinding<? extends Rule<?>>> rules, Set<Version> versions, | |
51 | Set<String> types) { | |
52 | 65600 | super(rules, versions); |
53 | 65600 | this.types = types; |
54 | 65600 | } |
55 | ||
56 | /** | |
57 | * Builds a {@link PrimitiveTypeRule} that evaluates the specified {@link Predicate} against the | |
58 | * primitive value. | |
59 | * | |
60 | * @param predicate predicate to evaluate the primitive value against | |
61 | * | |
62 | * @return this instance to build more rules | |
63 | */ | |
64 | public PrimitiveRuleBuilder is(Predicate predicate) { | |
65 | 48745 | return test(prepareRule(new PredicatePrimitiveTypeRule(predicate))); |
66 | } | |
67 | ||
68 | /** | |
69 | * Builds a {@link PrimitiveTypeRule} that evaluates the specified {@link Predicate} against the | |
70 | * primitive value with leading whitespaces trimmed. | |
71 | * | |
72 | * @param predicate predicate to evaluate the primitive value against | |
73 | * | |
74 | * @return this instance to build more rules | |
75 | */ | |
76 | public PrimitiveRuleBuilder leftTrim(Predicate predicate) { | |
77 | 11125 | return test(prepareRule(new PredicatePrimitiveTypeRule(predicate, Trimmer.LEFT))); |
78 | } | |
79 | ||
80 | /** | |
81 | * Builds a {@link PrimitiveTypeRule} that always evaluates to <code>true</code> and trims | |
82 | * leading whitespaces. | |
83 | * | |
84 | * @return this instance to build more rules | |
85 | */ | |
86 | public PrimitiveRuleBuilder leftTrim() { | |
87 | 5705 | return leftTrim(always(true)); |
88 | } | |
89 | ||
90 | /** | |
91 | * Builds a {@link PrimitiveTypeRule} that always evaluates to <code>true</code> and trims | |
92 | * trailing whitespaces. | |
93 | * | |
94 | * @return this instance to build more rules | |
95 | */ | |
96 | public PrimitiveRuleBuilder rightTrim() { | |
97 | 5705 | return rightTrim(always(true)); |
98 | } | |
99 | ||
100 | /** | |
101 | * Builds a {@link PrimitiveTypeRule} that evaluates the specified {@link Predicate} against the | |
102 | * primitive value with trailing whitespaces trimmed. | |
103 | * | |
104 | * @param predicate predicate to evaluate the primitive value against | |
105 | * | |
106 | * @return this instance to build more rules | |
107 | */ | |
108 | public PrimitiveRuleBuilder rightTrim(Predicate predicate) { | |
109 | 5710 | return test(prepareRule(new PredicatePrimitiveTypeRule(predicate, Trimmer.RIGHT))); |
110 | } | |
111 | ||
112 | /** | |
113 | * Builds a {@link PrimitiveTypeRule} that always evaluates to <code>true</code> and trims | |
114 | * leading and trailing whitespaces. | |
115 | * | |
116 | * @return this instance to build more rules | |
117 | */ | |
118 | public PrimitiveRuleBuilder allTrim() { | |
119 | 5 | return allTrim(always(true)); |
120 | } | |
121 | ||
122 | /** | |
123 | * Builds a {@link PrimitiveTypeRule} that evaluates the specified {@link Predicate} against the | |
124 | * primitive value with leading and trailing whitespaces trimmed. | |
125 | * | |
126 | * @param predicate predicate to evaluate the primitive value against | |
127 | * | |
128 | * @return this instance to build more rules | |
129 | */ | |
130 | public PrimitiveRuleBuilder allTrim(Predicate predicate) { | |
131 | 10 | return test(prepareRule(new PredicatePrimitiveTypeRule(predicate, Trimmer.ALL))); |
132 | } | |
133 | ||
134 | @Override | |
135 | protected Collection<RuleBinding<PrimitiveTypeRule>> getRuleBindings(PrimitiveTypeRule rule, | |
136 | String version) { | |
137 | 119740 | List<RuleBinding<PrimitiveTypeRule>> bindings = new ArrayList<RuleBinding<PrimitiveTypeRule>>(); |
138 | 119740 | for (String type : types) { |
139 | 163345 | bindings.add(new PrimitiveTypeRuleBinding(version, type, rule)); |
140 | 163345 | } |
141 | 119740 | return activate(bindings); |
142 | } | |
143 | ||
144 | // for tests only | |
145 | Set<String> getTypes() { | |
146 | 10 | return types; |
147 | } | |
148 | ||
149 | ||
150 | } |