Coverage Report - net.sf.jmatchparser.util.csv.AbstractCSVWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractCSVWriter
100%
9/9
100%
4/4
1,5
 
 1  
 /*
 2  
  * Copyright (c) 2006 - 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.Map;
 37  
 
 38  
 /**
 39  
  * Abstract base class to read data from several CSV-like file formats.
 40  
  */
 41  15
 public abstract class AbstractCSVWriter {
 42  
 
 43  
         /**
 44  
          * Flush this CSV writer.
 45  
          */
 46  
         public abstract void flush() throws IOException;
 47  
 
 48  
         /**
 49  
          * Close this CSV writer.
 50  
          */
 51  
         public abstract void close() throws IOException;
 52  
 
 53  
         /**
 54  
          * Write a record (line) into this CSV file.
 55  
          */
 56  
         public abstract void write(String... record) throws IOException;
 57  
 
 58  
         /**
 59  
          * Write an "indirect" record, consisting of a {@link Map} of headers to
 60  
          * values, and a header list. Values that do not exist in the map are
 61  
          * assumed to be empty.
 62  
          * 
 63  
          * @param recordMap
 64  
          *            map to map headers to values
 65  
          * @param headers
 66  
          *            headers
 67  
          */
 68  
         public final <T> void write(Map<T, String> recordMap, T[] headers) throws IOException {
 69  1
                 String[] record = new String[headers.length];
 70  4
                 for (int i = 0; i < headers.length; i++) {
 71  3
                         String value = recordMap.get(headers[i]);
 72  3
                         if (value == null)
 73  2
                                 value = "";
 74  3
                         record[i] = value;
 75  
                 }
 76  1
                 write(record);
 77  1
         }
 78  
 }