Changeset 5198


Ignore:
Timestamp:
Dec 15, 2009, 1:56:17 PM (13 years ago)
Author:
Nicklas Nordborg
Message:

References #1444: Implement generic BFS writers and readers/parsers

Improved error handling and error messages. Added DataWriterFactory? and two generic implementations.

Location:
trunk/src/core/net/sf/basedb/util/bfs
Files:
4 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/bfs/AnnotationWriter.java

    r5194 r5198  
    2222package net.sf.basedb.util.bfs;
    2323
     24import java.io.OutputStream;
     25import java.io.OutputStreamWriter;
    2426import java.io.Writer;
     27import java.nio.charset.Charset;
    2528import java.util.HashSet;
    2629import java.util.Set;
     
    3942{
    4043
     44  /**
     45    Utility method for creating an annotation writer when you have an
     46    output stream.
     47    @param out The output stream the annotation writer should print to
     48    @param filename Optional, the name of the file the output stream
     49      is printing to
     50  */
     51  public static AnnotationWriter create(OutputStream out, String filename)
     52  {
     53    AnnotationWriter writer = new AnnotationWriter(
     54        new OutputStreamWriter(out, Charset.forName("UTF-8")));
     55    writer.setFilename(filename);
     56    return writer;
     57  }
     58 
     59  private String filename;
    4160  private boolean hasPrintedHeaders;
    4261  private int columnCount = -1;
     
    5372 
    5473  /**
     74    Get the file name that this writer is printing to.
     75    @return The file name or null if not known
     76  */
     77  public String getFilename()
     78  {
     79    return filename;
     80  }
     81 
     82  /**
     83    Set the file name that this writer is printing to.
     84  */
     85  public void setFilename(String filename)
     86  {
     87    this.filename = filename;
     88  }
     89
     90 
     91  /**
    5592    Checks if headers has already been printed or not.
    5693  */
     
    87124    if (hasPrintedHeaders())
    88125    {
    89       throw new IllegalStateException("Headers has already been printed");
    90     }
    91     if (headers == null) throw new NullPointerException("headers");
     126      throw new IllegalStateException("Headers has already been printed to file: " +
     127          getFilename());
     128    }
     129    if (headers == null)
     130    {
     131      throw new NullPointerException("'headers' in file: " + getFilename());
     132    }
    92133   
    93134    Set<String> used = new HashSet<String>();
     
    96137    {
    97138      String header = headers[i];
    98       if (header == null) throw new NullPointerException("headers[" + i + "]");
     139      if (header == null)
     140      {
     141        throw new NullPointerException("'headers[" + i + "]' in file: " + getFilename());
     142      }
    99143      if (used.contains(header))
    100144      {
    101         throw new IllegalArgumentException("Duplicate header at index " + i + ": " + header);
     145        throw new IllegalArgumentException("Duplicate header at index " + i +
     146          " in file " + getFilename() + ": " + header);
    102147      }
    103148      used.add(header);
     
    127172    if (!hasPrintedHeaders())
    128173    {
    129       throw new IllegalStateException("Must print headers before printing data");
     174      throw new IllegalStateException("Must print headers before printing data to file: " +
     175        getFilename());
    130176    }
    131177    if (id <= 0)
    132178    {
    133       throw new IllegalArgumentException("The id must be a positive integer");
     179      throw new IllegalArgumentException("The id must be a positive integer in file: " +
     180        getFilename());
    134181    }
    135182    if (usedIds.contains(id))
    136183    {
    137       throw new IllegalArgumentException("Id value must be unique: " + id);
    138     }
    139     if (data == null) throw new NullPointerException("data");
     184      throw new IllegalArgumentException("Id value must be unique in file " +
     185        getFilename() + ": " + id);
     186    }
     187    if (data == null) throw new NullPointerException("'data' in file: " + getFilename());
    140188    if (data.length != columnCount)
    141189    {
    142190      throw new IllegalArgumentException("Data array has " + data.length + " columns; " +
    143           "; expected " + columnCount);
     191          "; expected " + columnCount + " in file: " + getFilename());
    144192    }
    145193    usedIds.add(id);
  • trunk/src/core/net/sf/basedb/util/bfs/DataWriter.java

    r5194 r5198  
    4343{
    4444
     45  private String filename;
    4546  private boolean lockedColumns;
    4647  private int columnCount = -1;
     
    5859  }
    5960
     61  /**
     62    Get the file name that this writer is printing to.
     63    @return The file name or null if not known
     64  */
     65  public String getFilename()
     66  {
     67    return filename;
     68  }
     69
     70  /**
     71    Set the file name that this writer is printing to.
     72  */
     73  public void setFilename(String filename)
     74  {
     75    this.filename = filename;
     76  }
     77 
    6078  /**
    6179    Get the number of data columns. This information is only
     
    82100    if (lockedColumns)
    83101    {
    84       throw new IllegalStateException("Can't change the number of columns since data has already been written");
     102      throw new IllegalStateException("Can't change the number of columns since data " +
     103        "has already been written in file: " + getFilename());
    85104    }
    86105    this.columnCount = columns;
     
    111130  {
    112131
    113     if (data == null) throw new NullPointerException("data");
     132    if (data == null) throw new NullPointerException("'data' in file: " + getFilename());
    114133    if (columnCount <= 0) columnCount = data.length;
    115134    if (data.length != columnCount)
    116135    {
    117136      throw new IllegalArgumentException("Data array has " + data.length + " columns; " +
    118           "; expected " + columnCount);
     137          "; expected " + columnCount + " in file: " + getFilename());
    119138    }
    120139    lockedColumns = true;
  • trunk/src/core/net/sf/basedb/util/bfs/MetadataWriter.java

    r5194 r5198  
    2222package net.sf.basedb.util.bfs;
    2323
     24import java.io.OutputStream;
     25import java.io.OutputStreamWriter;
    2426import java.io.PrintWriter;
    2527import java.io.Writer;
     28import java.nio.charset.Charset;
    2629import java.util.HashSet;
    2730import java.util.Set;
     
    5558  public static final String BOF_MARKER = "BFSformat";
    5659
     60  /**
     61    Utility method for creating a metadata writer when you have an
     62    output stream.
     63    @param out The output stream the metadata writer should print to
     64    @param filename Optional, the name of the file the output stream
     65      is printing to
     66  */
     67  public static MetadataWriter create(OutputStream out, String filename)
     68  {
     69    MetadataWriter writer = new MetadataWriter(
     70        new OutputStreamWriter(out, Charset.forName("UTF-8")));
     71    writer.setFilename(filename);
     72    return writer;
     73  }
     74 
    5775  private final EncoderDecoder encoder;
     76  private String filename;
    5877  private String subtype;
    5978  private Set<String> usedSections;
     
    7695    this.usedKeys = new HashSet<String>();
    7796    this.encoder = new TabCrLfEncoderDecoder(true);
     97  }
     98 
     99  /**
     100    Get the file name that this writer is printing to.
     101    @return The file name or null if not known
     102  */
     103  public String getFilename()
     104  {
     105    return filename;
     106  }
     107 
     108  /**
     109    Set the file name that this writer is printing to.
     110  */
     111  public void setFilename(String filename)
     112  {
     113    this.filename = filename;
    78114  }
    79115 
Note: See TracChangeset for help on using the changeset viewer.