Coverage Report - net.sf.jmatchparser.util.csv.fieldreader.EnumFieldReader
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumFieldReader
100%
9/9
100%
2/2
1,333
 
 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.HashMap;
 36  
 import java.util.Map;
 37  
 
 38  
 import net.sf.jmatchparser.util.csv.AbstractCSVReader;
 39  
 import net.sf.jmatchparser.util.csv.fieldreader.FieldSource.EmptyAction;
 40  
 
 41  
 /**
 42  
  * A {@link FieldReader} that uses an {@link Enum} that implements
 43  
  * {@link FieldEnum} as its field type.
 44  
  */
 45  
 public class EnumFieldReader<K extends FieldEnum<V>, V> extends FieldReader<K, V> {
 46  
 
 47  
         private final K[] enumValues;
 48  
 
 49  
         /**
 50  
          * Create a new {@link EnumFieldReader}
 51  
          * 
 52  
          * @param enumValues
 53  
          *            Values of the enum to be used, usually obtained from
 54  
          *            <code>Enum.values()</code>
 55  
          * @param reader
 56  
          *            CSV reader to be used
 57  
          */
 58  
         public EnumFieldReader(K[] enumValues, AbstractCSVReader reader) {
 59  4
                 super(buildDefinitions(enumValues), reader);
 60  4
                 this.enumValues = enumValues;
 61  4
         }
 62  
 
 63  
         private static <K extends FieldEnum<V>, V> Map<K, FieldDefinition<? extends V>> buildDefinitions(K[] enumValues) {
 64  4
                 Map<K, FieldDefinition<? extends V>> result = new HashMap<K, FieldDefinition<? extends V>>();
 65  22
                 for (K enumValue : enumValues) {
 66  18
                         result.put(enumValue, enumValue.getDefinition());
 67  
                 }
 68  4
                 return result;
 69  
         }
 70  
 
 71  
         /**
 72  
          * Compute the field sources from the index of the enum value inside the
 73  
          * declaration.
 74  
          * 
 75  
          * To be used when header parsing is disabled.
 76  
          * 
 77  
          * @param emptyAction
 78  
          *            {@link EmptyAction} to use
 79  
          */
 80  
         public void computeFieldSourcesFromIndex(EmptyAction emptyAction) {
 81  1
                 computeFieldSourcesFromIndex(emptyAction, enumValues);
 82  1
         }
 83  
 }