Coverage Report - net.sf.jmatchparser.util.csv.fieldreader.FieldDefinition
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldDefinition
75%
12/16
50%
2/4
2,75
FieldDefinition$MandatoryState
100%
4/4
N/A
2,75
 
 1  
 /*
 2  
  * Copyright (c) 2008 - 2011 Michael Schierl
 3  
  * 
 4  
  * All rights reserved.
 5  
  * 
 6  
  * Redistribution and use in source and binary forms, with or without
 7  
  * modification, are permitted provided that the following conditions
 8  
  * are met:
 9  
  * 
 10  
  * - Redistributions of source code must retain the above copyright notice,
 11  
  *   this list of conditions and the following disclaimer.
 12  
  *   
 13  
  * - Redistributions in binary form must reproduce the above copyright
 14  
  *   notice, this list of conditions and the following disclaimer in the
 15  
  *   documentation and/or other materials provided with the distribution.
 16  
  *   
 17  
  * - Neither name of the copyright holders nor the names of its
 18  
  *   contributors may be used to endorse or promote products derived from
 19  
  *   this software without specific prior written permission.
 20  
  *   
 21  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS
 22  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 23  
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 24  
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 25  
  * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 26  
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 27  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 28  
  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 29  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 30  
  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 31  
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32  
  */
 33  
 package net.sf.jmatchparser.util.csv.fieldreader;
 34  
 
 35  
 import java.text.ParseException;
 36  
 import java.util.Map;
 37  
 import java.util.regex.Pattern;
 38  
 
 39  
 import net.sf.jmatchparser.util.PatternCache;
 40  
 import net.sf.jmatchparser.util.csv.fieldreader.FieldReaderException.FieldReaderExceptionInfo;
 41  
 
 42  
 /**
 43  
  * A definition of a field of the {@link FieldReader}, as seen from the
 44  
  * perspective of the "consumer" that uses the parsed data.
 45  
  * 
 46  
  * @param <T>
 47  
  *            Type of the parsed object
 48  
  */
 49  
 public class FieldDefinition<T> {
 50  
 
 51  
         private final Pattern mask;
 52  
         private final int maxLength;
 53  
         private final ParseFormat<T> format;
 54  
         private final MandatoryState mandatory;
 55  
 
 56  
         /**
 57  
          * Create a new field definition.
 58  
          * 
 59  
          * @param mask
 60  
          *            Regular expression the values have to match before being
 61  
          *            formatted.
 62  
          * @param maxLength
 63  
          *            Maximum length of the values before being formatted
 64  
          * @param mandatory
 65  
          *            mandatory state of this field (see {@link MandatoryState})
 66  
          * @param format
 67  
          *            Format to be applied to the value
 68  
          */
 69  16
         public FieldDefinition(Pattern mask, int maxLength, MandatoryState mandatory, ParseFormat<T> format) {
 70  16
                 this.mask = mask;
 71  16
                 this.maxLength = maxLength;
 72  16
                 this.mandatory = mandatory;
 73  16
                 this.format = format;
 74  16
         }
 75  
 
 76  
         /**
 77  
          * Create a new field definition.
 78  
          * 
 79  
          * @param mask
 80  
          *            Regular expression the values have to match before being
 81  
          *            formatted.
 82  
          * @param maxLength
 83  
          *            Maximum length of the values before being formatted
 84  
          * @param mandatory
 85  
          *            mandatory state of this field (see {@link MandatoryState})
 86  
          * @param format
 87  
          *            Format to be applied to the value
 88  
          */
 89  
         public FieldDefinition(String mask, int maxLength, MandatoryState mandatory, ParseFormat<T> format) {
 90  16
                 this(PatternCache.compile(mask), maxLength, mandatory, format);
 91  16
         }
 92  
 
 93  
         MandatoryState getMandatory() {
 94  26
                 return mandatory;
 95  
         }
 96  
 
 97  
         T parse(String value, String fieldName, Map<String, String> rawRecord) throws FieldReaderException {
 98  39
                 if (maxLength < value.length()) {
 99  0
                         throw new FieldReaderException(FieldReaderErrorCodes.VALUE_TOO_LONG, true, new FieldReaderExceptionInfo<String, String>(fieldName, value, rawRecord));
 100  
                 }
 101  39
                 if (!mask.matcher(value).matches()) {
 102  0
                         throw new FieldReaderException(FieldReaderErrorCodes.INVALID_FORMAT, true, new FieldReaderExceptionInfo<String, String>(fieldName, value, rawRecord));
 103  
                 }
 104  
                 try {
 105  39
                         return format.parse(value);
 106  0
                 } catch (ParseException ex) {
 107  0
                         throw new FieldReaderException(FieldReaderErrorCodes.INVALID_FORMAT, true, new FieldReaderExceptionInfo<String, String>(fieldName, value, rawRecord), ex);
 108  
                 }
 109  
         }
 110  
 
 111  
         /**
 112  
          * Mandatory state of a field.
 113  
          */
 114  4
         public enum MandatoryState {
 115  
 
 116  
                 /**
 117  
                  * This field is optional. It can be filled or not.
 118  
                  */
 119  1
                 OPTIONAL,
 120  
 
 121  
                 /**
 122  
                  * This field is mandatory. It has to be filled for all records.
 123  
                  */
 124  1
                 MANDATORY,
 125  
 
 126  
                 /**
 127  
                  * This field is only mandatory if a new record should be created, not
 128  
                  * if a record should be updated.
 129  
                  */
 130  1
                 MANDATORY_IF_NEW
 131  
         }
 132  
 }