001/**
002The contents of this file are subject to the Mozilla Public License Version 1.1 
003(the "License"); you may not use this file except in compliance with the License. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "PrimitiveRuleBuilder.java".  Description: 
010"Rule Builder for PrimitiveTypeRules."
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0132012.  All Rights Reserved. 
014
015Contributor(s): ______________________________________. 
016
017Alternatively, the contents of this file may be used under the terms of the 
018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
019applicable instead of those above.  If you wish to allow use of your version of this 
020file only under the terms of the GPL and not to allow others to use your version 
021of this file under the MPL, indicate your decision by deleting  the provisions above 
022and replace  them with the notice and other provisions required by the GPL License.  
023If you do not delete the provisions above, a recipient may use your version of 
024this file under either the MPL or the GPL. 
025 */
026package ca.uhn.hl7v2.validation.builder;
027
028import java.util.ArrayList;
029import java.util.Collection;
030import java.util.List;
031import java.util.Set;
032
033import ca.uhn.hl7v2.Version;
034import ca.uhn.hl7v2.validation.PrimitiveTypeRule;
035import ca.uhn.hl7v2.validation.Rule;
036import ca.uhn.hl7v2.validation.builder.PredicatePrimitiveTypeRule.Trimmer;
037import ca.uhn.hl7v2.validation.impl.PrimitiveTypeRuleBinding;
038import ca.uhn.hl7v2.validation.impl.RuleBinding;
039
040/**
041 * Rule builder for {@link PrimitiveTypeRule}s
042 * 
043 * @author Christian Ohr
044 */
045@SuppressWarnings("serial")
046public class PrimitiveRuleBuilder extends RuleTypeBuilder<PrimitiveRuleBuilder, PrimitiveTypeRule>  {
047
048        private Set<String> types;
049
050        protected PrimitiveRuleBuilder(List<RuleBinding<? extends Rule<?>>> rules, Set<Version> versions,
051                        Set<String> types) {
052                super(rules, versions);
053                this.types = types;
054        }
055
056        /**
057         * Builds a {@link PrimitiveTypeRule} that evaluates the specified {@link Predicate} against the
058         * primitive value.
059         * 
060         * @param predicate predicate to evaluate the primitive value against
061         * 
062         * @return this instance to build more rules
063         */
064        public PrimitiveRuleBuilder is(Predicate predicate) {
065                return test(prepareRule(new PredicatePrimitiveTypeRule(predicate)));
066        }
067
068        /**
069         * Builds a {@link PrimitiveTypeRule} that evaluates the specified {@link Predicate} against the
070         * primitive value with leading whitespaces trimmed.
071         * 
072         * @param predicate predicate to evaluate the primitive value against
073         * 
074         * @return this instance to build more rules
075         */
076        public PrimitiveRuleBuilder leftTrim(Predicate predicate) {
077                return test(prepareRule(new PredicatePrimitiveTypeRule(predicate, Trimmer.LEFT)));
078        }
079
080    /**
081     * Builds a {@link PrimitiveTypeRule} that always evaluates to <code>true</code> and trims
082     * leading whitespaces.
083     * 
084     * @return this instance to build more rules
085     */
086    public PrimitiveRuleBuilder leftTrim() {
087        return leftTrim(always(true));
088    }
089    
090        /**
091         * Builds a {@link PrimitiveTypeRule} that always evaluates to <code>true</code> and trims
092         * trailing whitespaces.
093         * 
094         * @return this instance to build more rules
095         */
096        public PrimitiveRuleBuilder rightTrim() {
097                return rightTrim(always(true));
098        }
099        
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        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        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        return test(prepareRule(new PredicatePrimitiveTypeRule(predicate, Trimmer.ALL)));
132    }    
133
134        @Override
135        protected Collection<RuleBinding<PrimitiveTypeRule>> getRuleBindings(PrimitiveTypeRule rule,
136                        String version) {
137                List<RuleBinding<PrimitiveTypeRule>> bindings = new ArrayList<RuleBinding<PrimitiveTypeRule>>();
138                for (String type : types) {
139                        bindings.add(new PrimitiveTypeRuleBinding(version, type, rule));
140                }
141                return activate(bindings);
142        }
143
144        // for tests only
145        Set<String> getTypes() {
146                return types;
147        }
148
149        
150}