Coverage Report - net.sf.jmatchparser.util.csv.fieldreader.FieldReaderException
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldReaderException
0%
0/10
0%
0/2
1,111
FieldReaderException$FieldReaderExceptionInfo
0%
0/8
N/A
1,111
 
 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.util.Map;
 36  
 
 37  
 /**
 38  
  * Thrown when a field has a value that cannot be parsed.
 39  
  */
 40  
 public class FieldReaderException extends Exception {
 41  
 
 42  
         private final FieldReaderErrorCode errorCode;
 43  
         private final FieldReaderExceptionInfo<?, ?> exceptionInfo;
 44  
         private final boolean rawField;
 45  
 
 46  
         /**
 47  
          * Create a new field reader exception.
 48  
          * 
 49  
          * @param errorCode
 50  
          *            Error code
 51  
          * @param rawField
 52  
          *            Whether the field is still raw (like in the CSV layout) or
 53  
          *            already formatted (in the layout the {@link FieldReader}
 54  
          *            creates)
 55  
          * @param exceptionInfo
 56  
          *            Information about the field and its value
 57  
          */
 58  
         public FieldReaderException(FieldReaderErrorCode errorCode, boolean rawField, FieldReaderExceptionInfo<?, ?> exceptionInfo) {
 59  0
                 this(errorCode, rawField, exceptionInfo, null);
 60  0
         }
 61  
 
 62  
         /**
 63  
          * Create a new field reader exception.
 64  
          * 
 65  
          * @param errorCode
 66  
          *            Error code
 67  
          * @param rawField
 68  
          *            Whether the field is still raw (like in the CSV layout) or
 69  
          *            already formatted (in the layout the {@link FieldReader}
 70  
          *            creates)
 71  
          * @param exceptionInfo
 72  
          *            Information about the field and its value
 73  
          */
 74  
         public FieldReaderException(FieldReaderErrorCode errorCode, boolean rawField, FieldReaderExceptionInfo<?, ?> exceptionInfo, Throwable cause) {
 75  0
                 super((rawField ? "Raw " : "") + "Field " + exceptionInfo.getField() + ", Value: " + exceptionInfo.getValue() + ", Error: "
 76  
                                 + errorCode.getErrorCode() + " (" + errorCode.getErrorDescription() + "), Record: "
 77  
                                 + exceptionInfo.getOriginalRecord().toString(), cause);
 78  0
                 this.errorCode = errorCode;
 79  0
                 this.rawField = rawField;
 80  0
                 this.exceptionInfo = exceptionInfo;
 81  0
         }
 82  
 
 83  
         /**
 84  
          * Return the error code of this exception.
 85  
          */
 86  
         public FieldReaderErrorCode getErrorCode() {
 87  0
                 return errorCode;
 88  
         }
 89  
 
 90  
         /**
 91  
          * Return whether this exception occurred when the fields still were "raw"
 92  
          * (i. e. not formatted).
 93  
          */
 94  
         public boolean isRawField() {
 95  0
                 return rawField;
 96  
         }
 97  
 
 98  
         /**
 99  
          * Return the exception info of this exception.
 100  
          */
 101  
         public FieldReaderExceptionInfo<?, ?> getExceptionInfo() {
 102  0
                 return exceptionInfo;
 103  
         }
 104  
 
 105  
         /**
 106  
          * Additional information about a field reader exception. Placed into a
 107  
          * nested class since {@link Exception}s may not have generic arguments.
 108  
          * 
 109  
          * @param <K>
 110  
          *            Type of the fields of the field reader
 111  
          * @param <V>
 112  
          *            Type of the values of the field reader
 113  
          */
 114  
         public static class FieldReaderExceptionInfo<K, V> {
 115  
                 private final K field;
 116  
                 private final V value;
 117  
                 private final Map<K, V> originalRecord;
 118  
 
 119  
                 /**
 120  
                  * Create a new FieldReaderExceptionInfo instance.
 121  
                  * 
 122  
                  * @param field
 123  
                  *            Field that caused the exception
 124  
                  * @param value
 125  
                  *            Value of that field
 126  
                  * @param originalRecord
 127  
                  *            All fields as available at that state of parsing
 128  
                  */
 129  0
                 public FieldReaderExceptionInfo(K field, V value, Map<K, V> originalRecord) {
 130  0
                         this.field = field;
 131  0
                         this.value = value;
 132  0
                         this.originalRecord = originalRecord;
 133  0
                 }
 134  
 
 135  
                 /**
 136  
                  * Return the field.
 137  
                  */
 138  
                 public K getField() {
 139  0
                         return field;
 140  
                 }
 141  
 
 142  
                 /**
 143  
                  * Return the value.
 144  
                  */
 145  
                 public V getValue() {
 146  0
                         return value;
 147  
                 }
 148  
 
 149  
                 /**
 150  
                  * Return all the other fields available.
 151  
                  */
 152  
                 public Map<K, V> getOriginalRecord() {
 153  0
                         return originalRecord;
 154  
                 }
 155  
         }
 156  
 }