Coverage Report - ca.uhn.hl7v2.validation.impl.ValidationContextImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ValidationContextImpl
100%
28/28
88%
16/18
1.8
 
 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 "ValidationContextImpl.java".  Description: 
 10  
 "A default implementation of ValidationContext." 
 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.impl;
 27  
 
 28  
 import java.io.Serializable;
 29  
 import java.util.ArrayList;
 30  
 import java.util.Collection;
 31  
 import java.util.List;
 32  
 
 33  
 import ca.uhn.hl7v2.model.Primitive;
 34  
 import ca.uhn.hl7v2.validation.EncodingRule;
 35  
 import ca.uhn.hl7v2.validation.MessageRule;
 36  
 import ca.uhn.hl7v2.validation.PrimitiveTypeRule;
 37  
 import ca.uhn.hl7v2.validation.Rule;
 38  
 import ca.uhn.hl7v2.validation.ValidationContext;
 39  
 import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
 40  
 
 41  
 /**
 42  
  * A default implementation of <code>ValidationContext</code>.
 43  
  * 
 44  
  * @author Bryan Tripp
 45  
  * @author Christian Ohr
 46  
  */
 47  
 @SuppressWarnings("serial")
 48  
 public class ValidationContextImpl implements ValidationContext, Serializable {
 49  
 
 50  
         private List<RuleBinding<PrimitiveTypeRule>> myPrimitiveRuleBindings;
 51  
         private List<RuleBinding<MessageRule>> myMessageRuleBindings;
 52  
         private List<RuleBinding<EncodingRule>> myEncodingRuleBindings;
 53  
 
 54  512
         public ValidationContextImpl() {
 55  512
                 myPrimitiveRuleBindings = new ArrayList<RuleBinding<PrimitiveTypeRule>>();
 56  512
                 myMessageRuleBindings = new ArrayList<RuleBinding<MessageRule>>();
 57  512
                 myEncodingRuleBindings = new ArrayList<RuleBinding<EncodingRule>>();
 58  512
         }
 59  
         
 60  
         ValidationContextImpl(ValidationRuleBuilder builder) {
 61  507
                 this();
 62  507
                 for (RuleBinding<? extends Rule<?>> ruleBinding : builder.initialize()) {
 63  11132
                         if (ruleBinding instanceof MessageRuleBinding)
 64  922
                                 myMessageRuleBindings.add((MessageRuleBinding)ruleBinding);
 65  10210
                         else if (ruleBinding instanceof EncodingRuleBinding)
 66  4
                                 myEncodingRuleBindings.add((EncodingRuleBinding)ruleBinding);
 67  10206
                         else if (ruleBinding instanceof PrimitiveTypeRuleBinding)
 68  10206
                                 myPrimitiveRuleBindings.add((PrimitiveTypeRuleBinding)ruleBinding);
 69  11132
                 }
 70  507
         }
 71  
         
 72  
 
 73  
         /**
 74  
          * @see ValidationContext#getPrimitiveRules(String, String, Primitive)
 75  
          * @param theType ignored
 76  
          */
 77  
         public Collection<PrimitiveTypeRule> getPrimitiveRules(String theVersion, String theTypeName, Primitive theType) {
 78  16745
                 return getRules(myPrimitiveRuleBindings, theVersion, theTypeName);
 79  
         }
 80  
 
 81  
         /**
 82  
          * @return a List of <code>RuleBinding</code>s for
 83  
          *         <code>PrimitiveTypeRule</code>s.
 84  
          */
 85  
         public List<RuleBinding<PrimitiveTypeRule>> getPrimitiveRuleBindings() {
 86  2
                 return myPrimitiveRuleBindings;
 87  
         }
 88  
                 
 89  
 
 90  
         /**
 91  
          * @see ValidationContext#getMessageRules(java.lang.String, java.lang.String, java.lang.String)
 92  
          */
 93  
         public Collection<MessageRule> getMessageRules(String theVersion, String theMessageType, String theTriggerEvent) {
 94  1002
                 return getRules(myMessageRuleBindings, theVersion, theMessageType + "^" + theTriggerEvent);
 95  
         }
 96  
 
 97  
         /**
 98  
          * @return a List of <code>RuleBinding</code>s for <code>MessageRule</code>s.
 99  
          */
 100  
         public List<RuleBinding<MessageRule>> getMessageRuleBindings() {
 101  3
                 return myMessageRuleBindings;
 102  
         }
 103  
 
 104  
         /**
 105  
          * @see ca.uhn.hl7v2.validation.ValidationContext#getEncodingRules(java.lang.String,
 106  
          *      java.lang.String)
 107  
          */
 108  
         public Collection<EncodingRule> getEncodingRules(String theVersion, String theEncoding) {
 109  1005
                 return getRules(myEncodingRuleBindings, theVersion, theEncoding);
 110  
         }
 111  
 
 112  
         /**
 113  
          * @return a List of <code>RuleBinding</code>s for <code>EncodingRule</code>s.
 114  
          */
 115  
         public List<RuleBinding<EncodingRule>> getEncodingRuleBindings() {
 116  2
                 return myEncodingRuleBindings;
 117  
         }
 118  
         
 119  
         private <T extends Rule<?>> Collection<T> getRules(List<RuleBinding<T>> bindings, String version, String scope) {
 120  18752
                 List<T> active = new ArrayList<T>(bindings.size());
 121  18752
                 for (RuleBinding<T> binding : bindings) {
 122  349782
                         if (applies(binding, version, scope))
 123  17307
                                 active.add(binding.getRule());
 124  349781
                 }
 125  18751
                 return active;
 126  
         }
 127  
 
 128  
         private boolean applies(RuleBinding<?> binding, String version, String scope) {
 129  349782
                 return (binding.getActive() && binding.appliesToVersion(version) && binding.appliesToScope(scope));
 130  
         }
 131  
 
 132  
 
 133  
 }