Coverage Report - ca.uhn.hl7v2.validation.builder.PredicatePrimitiveTypeRule
 
Classes in this File Line Coverage Branch Coverage Complexity
PredicatePrimitiveTypeRule
70%
14/20
N/A
1.615
PredicatePrimitiveTypeRule$1
N/A
N/A
1.615
PredicatePrimitiveTypeRule$Trimmer
100%
8/8
75%
3/4
1.615
PredicatePrimitiveTypeRule$Trimmer$1
100%
2/2
N/A
1.615
PredicatePrimitiveTypeRule$Trimmer$2
100%
2/2
N/A
1.615
PredicatePrimitiveTypeRule$Trimmer$3
100%
2/2
N/A
1.615
PredicatePrimitiveTypeRule$Trimmer$4
100%
2/2
100%
2/2
1.615
 
 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 "PredicatePrimitiveTypeRule.java".  Description: 
 10  
 "PrimitiveTypeRule that validates using predicates" 
 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.regex.Pattern;
 29  
 
 30  
 import ca.uhn.hl7v2.validation.ValidationException;
 31  
 import ca.uhn.hl7v2.validation.impl.AbstractPrimitiveTypeRule;
 32  
 
 33  
 /**
 34  
  * Abstract base class for PrimitiveTypeRules that validate using predicates
 35  
  * 
 36  
  * @author Christian Ohr
 37  
  */
 38  119428
 @SuppressWarnings("serial")
 39  112226
 public class PredicatePrimitiveTypeRule extends AbstractPrimitiveTypeRule implements
 40  
                 PredicateRuleSupport<String> {
 41  
  
 42  5
     private static final Pattern LEADING_WHITESPACE = Pattern.compile("^\\s+");
 43  5
     private static final Pattern TRAILING_WHITESPACE = Pattern.compile("\\s+$");
 44  
 
 45  
     
 46  45
     public enum Trimmer {
 47  
 
 48  5
         LEFT {
 49  111986
             @Override public String trim(String source) { return trimPattern(LEADING_WHITESPACE, source); }            
 50  
         },
 51  5
         RIGHT {
 52  240
             @Override public String trim(String source) { return trimPattern(TRAILING_WHITESPACE, source); }            
 53  
         },                
 54  5
         NONE {
 55  126805
             @Override public String trim(String source) { return source; }            
 56  
         },
 57  5
         ALL {
 58  70
             @Override public String trim(String source) { return source == null ? null : source.trim(); }                        
 59  
         };
 60  
 
 61  
         /**
 62  
          * Trims a string
 63  
          * @param source the string to be trimmed
 64  
          * @return the trimmed string
 65  
          */
 66  
         public abstract String trim(String source);
 67  
         
 68  
         protected String trimPattern(Pattern pattern, String source) {
 69  112226
             if (source == null || pattern == null) 
 70  2430
                 return source;
 71  109796
             return pattern.matcher(source).replaceAll("");
 72  
         }
 73  
     }
 74  
 
 75  
         private Predicate predicate;
 76  
         private Trimmer trimmer;
 77  
 
 78  
     /**
 79  
      * Creates a new primitive rule without trimming
 80  
      * @param predicate predicate to be used
 81  
      */
 82  
         public PredicatePrimitiveTypeRule(Predicate predicate) {
 83  48745
                 this(predicate, Trimmer.NONE);
 84  48745
         }
 85  
 
 86  
     /**
 87  
      * Creates a new primitive rule
 88  
      * @param predicate predicate to be used
 89  
      * @param trimmer trimming behavior
 90  
      */
 91  65590
         public PredicatePrimitiveTypeRule(Predicate predicate, Trimmer trimmer) {
 92  65590
                 this.predicate = predicate;
 93  65590
                 this.trimmer = trimmer;
 94  65590
         setDescription("Primitive value '%s' requires to be " + predicate.getDescription());
 95  65590
         }
 96  
 
 97  
         public Predicate getPredicate() {
 98  119568
                 return predicate;
 99  
         }
 100  
 
 101  
         public String correct(String value) {
 102  239101
             return trimmer.trim(value);
 103  
         }
 104  
 
 105  
         public boolean test(String value) {
 106  
                 try {
 107  0
                         return getPredicate().evaluate(value);
 108  0
                 } catch (ValidationException e) {
 109  0
                         return false;
 110  
                 }
 111  
         }
 112  
 
 113  
         public ValidationException[] apply(String value) {
 114  
                 try {
 115  119568
                         return result(getPredicate().evaluate(correct(value)), value);
 116  0
                 } catch (ValidationException e) {
 117  0
                         return failed(e);
 118  
                 }
 119  
         }
 120  
 
 121  
         @Override
 122  
         public String toString() {
 123  0
                 return getDescription();
 124  
         }
 125  
 
 126  
 }