View Javadoc
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 "ValidationRuleBuilder.java".  Description: 
10  "RuleBuilder that determines which kind of rule shall be built" 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2004.  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.Collections;
29  import java.util.List;
30  
31  import ca.uhn.hl7v2.Version;
32  import ca.uhn.hl7v2.validation.Rule;
33  import ca.uhn.hl7v2.validation.impl.RuleBinding;
34  
35  /**
36   * Base class every rule builder starts with. All rules being built are bound to the HL7 versions
37   * that are specified here.
38   * 
39   * @author Christian Ohr
40   */
41  @SuppressWarnings("serial")
42  public class ValidationRuleBuilder extends RuleTypeBuilder<ValidationRuleBuilder, Rule<?>> {
43  
44  	protected ValidationRuleBuilder() {
45  		super();
46  	}
47  
48  	private ValidationRuleBuilder(List<RuleBinding<? extends Rule<?>>> rules, Version... versions) {
49  		super(rules, versions);
50  	}
51  
52  	public final List<RuleBinding<? extends Rule<?>>> initialize() {
53  		if (getRules().isEmpty()) configure();
54  		return Collections.unmodifiableList(getRules());
55  	}
56  
57  	/**
58  	 * Extend this method to add validation rules to the builder
59  	 */
60  	protected void configure() {
61  	}
62  
63  	public final ValidationRuleBuilder forVersion(
64  			Version... version) {
65  		return new ValidationRuleBuilder(getRules(), version);
66  	}
67  
68  	public final ValidationRuleBuilder forVersion(
69  			String... version) {
70  		Version[] versions = new Version[version.length];
71  		for (int i = 0; i < versions.length; i++) {
72  			versions[i] = Version.versionOf(version[i]);
73  		}
74  		return new ValidationRuleBuilder(getRules(), versions);
75  	}
76  
77  	public final VersionExpressionBuilder forVersion() {
78  		return new VersionExpressionBuilder();
79  	}
80  
81  	public final ValidationRuleBuilder forAllVersions() {
82  		return forVersion().all();
83  	}
84  
85  	/**
86  	 * Helper builder when the versions are not given explicitly but in form of an expression.
87  	 */
88  	public class VersionExpressionBuilder {
89  
90  		public ValidationRuleBuilder all() {
91  			return new ValidationRuleBuilder(getRules(), Version.values());
92  		}
93  
94  		public ValidationRuleBuilder asOf(String version) {
95  			return asOf(Version.versionOf(version));
96  		}
97  
98  		public ValidationRuleBuilder asOf(Version version) {
99  			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             Versionml#Version">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 }