Coverage Report - net.sf.jmatchparser.util.csv.fieldreader.ParseFormat
 
Classes in this File Line Coverage Branch Coverage Complexity
ParseFormat
80%
8/10
25%
1/4
1,462
ParseFormat$1
100%
2/2
N/A
1,462
ParseFormat$2
0%
0/5
N/A
1,462
ParseFormat$3
100%
5/5
N/A
1,462
ParseFormat$4
100%
5/5
N/A
1,462
ParseFormat$5
100%
2/2
N/A
1,462
ParseFormat$6
33%
2/6
N/A
1,462
 
 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.DateFormat;
 36  
 import java.text.Format;
 37  
 import java.text.NumberFormat;
 38  
 import java.text.ParseException;
 39  
 import java.text.ParsePosition;
 40  
 import java.util.Date;
 41  
 import java.util.regex.Pattern;
 42  
 
 43  
 /**
 44  
  * A format for parsing a String to an object of a possibly different type.
 45  
  * 
 46  
  * @param <T>
 47  
  *            Type of the parsed object
 48  
  */
 49  15
 public abstract class ParseFormat<T> {
 50  
 
 51  
         /**
 52  
          * Parse the given source completely. An exception is thrown if not the
 53  
          * complete source has been parsed.
 54  
          * 
 55  
          * @param source
 56  
          *            Source string
 57  
          * @return Parsed object
 58  
          */
 59  
         public abstract T parse(String source) throws ParseException;
 60  
 
 61  
         /**
 62  
          * Parse format that returns the original string.
 63  
          */
 64  1
         public static final ParseFormat<String> IDENTITY = new ParseFormat<String>() {
 65  
                 @Override
 66  
                 public String parse(String source) throws ParseException {
 67  35
                         return source;
 68  
                 }
 69  
         };
 70  
 
 71  
         /**
 72  
          * Parse format that uses a {@link Format} for parsing.
 73  
          */
 74  
         public static ParseFormat<Object> fromFormat(final Format format) {
 75  0
                 return new ParseFormat<Object>() {
 76  
                         @Override
 77  
                         public Object parse(String source) throws ParseException {
 78  0
                                 ParsePosition pp = new ParsePosition(0);
 79  0
                                 Object result = format.parseObject(source, pp);
 80  0
                                 checkParsePosition(source, pp);
 81  0
                                 return result;
 82  
                         }
 83  
                 };
 84  
         }
 85  
 
 86  
         /**
 87  
          * Parse format that uses a {@link DateFormat} for parsing.
 88  
          */
 89  
         public static ParseFormat<Date> fromDateFormat(final DateFormat format) {
 90  1
                 return new ParseFormat<Date>() {
 91  
                         @Override
 92  
                         public Date parse(String source) throws ParseException {
 93  2
                                 ParsePosition pp = new ParsePosition(0);
 94  2
                                 Date result = format.parse(source, pp);
 95  2
                                 checkParsePosition(source, pp);
 96  2
                                 return result;
 97  
                         }
 98  
                 };
 99  
         }
 100  
 
 101  
         /**
 102  
          * Parse format that uses a {@link NumberFormat} for parsing.
 103  
          */
 104  
         public static ParseFormat<Number> fromNumberFormat(final NumberFormat format) {
 105  1
                 return new ParseFormat<Number>() {
 106  
                         @Override
 107  
                         public Number parse(String source) throws ParseException {
 108  6
                                 ParsePosition pp = new ParsePosition(0);
 109  6
                                 Number result = format.parse(source, pp);
 110  6
                                 checkParsePosition(source, pp);
 111  6
                                 return result;
 112  
                         }
 113  
                 };
 114  
         }
 115  
 
 116  
         /**
 117  
          * Parse format that performs a regex replacement before parsing with
 118  
          * another format.
 119  
          */
 120  
         public static <TT> ParseFormat<TT> fromReplacement(final Pattern regex, final String replacement, final ParseFormat<TT> nextFormat) {
 121  3
                 return new ParseFormat<TT>() {
 122  
                         @Override
 123  
                         public TT parse(String source) throws ParseException {
 124  9
                                 return nextFormat.parse(regex.matcher(source).replaceAll(replacement));
 125  
                         }
 126  
                 };
 127  
         }
 128  
 
 129  
         /**
 130  
          * Parse format that parses {@link Enum} names to their values.
 131  
          */
 132  
         public static <TT extends Enum<TT>> ParseFormat<TT> forEnum(final Class<TT> enumType) {
 133  1
                 return new ParseFormat<TT>() {
 134  
                         @Override
 135  
                         public TT parse(String source) throws ParseException {
 136  
                                 try {
 137  5
                                         return Enum.valueOf(enumType, source);
 138  0
                                 } catch (IllegalArgumentException ex) {
 139  0
                                         ParseException pex = new ParseException("Unable to parse enum", 0);
 140  0
                                         pex.initCause(ex);
 141  0
                                         throw pex;
 142  
                                 }
 143  
                         }
 144  
                 };
 145  
         }
 146  
 
 147  
         private static void checkParsePosition(String source, ParsePosition pp) throws ParseException {
 148  8
                 if (pp.getIndex() != source.length())
 149  0
                         throw new ParseException("Parsing stopped before end of string", pp.getErrorIndex() == -1 ? pp.getIndex() : pp.getErrorIndex());
 150  8
         }
 151  
 }