Coverage Report - net.sf.jmatchparser.util.csv.CSVReaderBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
CSVReaderBuilder
100%
12/12
N/A
1,25
CSVReaderBuilder$CSVReaderBuilderReader
100%
7/7
100%
2/2
1,25
 
 1  
 /*
 2  
  * Copyright (c) 2010 - 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;
 34  
 
 35  
 import java.io.IOException;
 36  
 import java.util.ArrayList;
 37  
 import java.util.Iterator;
 38  
 import java.util.List;
 39  
 
 40  
 /**
 41  
  * An {@link AbstractCSVWriter} where you can write records into and then call
 42  
  * the {@link #toReader()} method to build an {@link AbstractCSVReader} that can
 43  
  * read exactly these records.
 44  
  */
 45  6
 public class CSVReaderBuilder extends AbstractCSVWriter {
 46  
 
 47  6
         private final List<String[]> records = new ArrayList<String[]>();
 48  
 
 49  
         @Override
 50  
         public void write(String... record) throws IOException {
 51  23
                 records.add(record);
 52  23
         }
 53  
 
 54  
         @Override
 55  
         public void flush() throws IOException {
 56  1
         }
 57  
 
 58  
         @Override
 59  
         public void close() throws IOException {
 60  1
         }
 61  
 
 62  
         /**
 63  
          * Create a CSV reader from the records written to this builder and reset
 64  
          * it.
 65  
          * 
 66  
          * @see #reset()
 67  
          */
 68  
         public AbstractCSVReader toReader() {
 69  8
                 List<String[]> readerRecords = new ArrayList<String[]>(records);
 70  8
                 reset();
 71  8
                 return new CSVReaderBuilderReader(readerRecords.iterator());
 72  
         }
 73  
 
 74  
         /**
 75  
          * Reset this builder.
 76  
          */
 77  
         public void reset() {
 78  9
                 records.clear();
 79  9
         }
 80  
 
 81  6
         private static class CSVReaderBuilderReader extends AbstractCSVReader {
 82  
 
 83  
                 private final Iterator<String[]> it;
 84  
 
 85  8
                 public CSVReaderBuilderReader(Iterator<String[]> it) {
 86  8
                         this.it = it;
 87  8
                 }
 88  
 
 89  
                 @Override
 90  
                 public String[] read() throws IOException {
 91  32
                         if (it.hasNext())
 92  22
                                 return it.next();
 93  
                         else
 94  10
                                 return null;
 95  
                 }
 96  
 
 97  
                 @Override
 98  
                 public void close() throws IOException {
 99  5
                 }
 100  
         }
 101  
 }