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 "ValidationRuleBuilder.java".  Description: 
010"RuleBuilder that determines which kind of rule shall be built" 
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0132004.  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.Collections;
029import java.util.List;
030
031import ca.uhn.hl7v2.Version;
032import ca.uhn.hl7v2.validation.Rule;
033import ca.uhn.hl7v2.validation.impl.RuleBinding;
034
035/**
036 * Base class every rule builder starts with. All rules being built are bound to the HL7 versions
037 * that are specified here.
038 * 
039 * @author Christian Ohr
040 */
041@SuppressWarnings("serial")
042public class ValidationRuleBuilder extends RuleTypeBuilder<ValidationRuleBuilder, Rule<?>> {
043
044        protected ValidationRuleBuilder() {
045                super();
046        }
047
048        private ValidationRuleBuilder(List<RuleBinding<? extends Rule<?>>> rules, Version... versions) {
049                super(rules, versions);
050        }
051
052        public final List<RuleBinding<? extends Rule<?>>> initialize() {
053                if (getRules().isEmpty()) configure();
054                return Collections.unmodifiableList(getRules());
055        }
056
057        /**
058         * Extend this method to add validation rules to the builder
059         */
060        protected void configure() {
061        }
062
063        public final ValidationRuleBuilder forVersion(
064                        Version... version) {
065                return new ValidationRuleBuilder(getRules(), version);
066        }
067
068        public final ValidationRuleBuilder forVersion(
069                        String... version) {
070                Version[] versions = new Version[version.length];
071                for (int i = 0; i < versions.length; i++) {
072                        versions[i] = Version.versionOf(version[i]);
073                }
074                return new ValidationRuleBuilder(getRules(), versions);
075        }
076
077        public final VersionExpressionBuilder forVersion() {
078                return new VersionExpressionBuilder();
079        }
080
081        public final ValidationRuleBuilder forAllVersions() {
082                return forVersion().all();
083        }
084
085        /**
086         * Helper builder when the versions are not given explicitly but in form of an expression.
087         */
088        public class VersionExpressionBuilder {
089
090                public ValidationRuleBuilder all() {
091                        return new ValidationRuleBuilder(getRules(), Version.values());
092                }
093
094                public ValidationRuleBuilder asOf(String version) {
095                        return asOf(Version.versionOf(version));
096                }
097
098                public ValidationRuleBuilder asOf(Version version) {
099                        return new ValidationRuleBuilder(getRules(), Version.asOf(version));
100                }
101
102                public ValidationRuleBuilder before(String version) {
103                        return before(Version.versionOf(version));
104                }
105
106                public ValidationRuleBuilder before(Version version) {
107                        return new ValidationRuleBuilder(getRules(), Version.before(version));
108                }
109
110        public ValidationRuleBuilder except(String... versions) {
111            Version[] v = new Version[versions.length];
112            for (int i = 0; i < versions.length; i++) {
113                v[i] = Version.versionOf(versions[i]);
114            }
115            return except(v);
116        }
117
118        public ValidationRuleBuilder except(Version... versions) {
119            return new ValidationRuleBuilder(getRules(), Version.except(versions));
120        }
121
122        }
123
124}