org.otfeed.support
Class CSVDataWriter

java.lang.Object
  extended by org.otfeed.support.CSVDataWriter
All Implemented Interfaces:
IDataWriter

public class CSVDataWriter
extends java.lang.Object
implements IDataWriter

Class that provides CSV formatting for Java POJO beans.

Simplest usage is:

 IDataWriter writer = new CSVDataWriter(OTTrade.class);
 
which creates a writer to output OTTrade properties in a CSV format. Note that only objects of one class (used in constructor) can be written. Attempts to write a different object will yield a runtime exception.

More advanced usage is:

 List propertiesList = new LinkedList();
 propertiesList.add("timestamp");
 propertiesList.add("openPrice");
 propertiesList.add("closePrice");
 propertiesList.add("volume");
 IDataWriter writer = new CSVDatWriter(OOHLC.class, propertiesList);
 
which creates a writer that outputs only listed properties of OTOHLC object.

Typically, this object will be used in conjunction with CommonListener or another class that implements an appropriate event listener using IDataWriter as the event sink.


Constructor Summary
CSVDataWriter(java.lang.Class<?> cls)
          Creates new CSVDataWriter to write objects of a given class.
CSVDataWriter(java.lang.Class<?> cls, java.util.List<java.lang.String> list)
          Creates new CSVDataWriter to write listed properties of a given class.
 
Method Summary
 void close()
          Closes the writer stream.
 java.util.Map<java.lang.Class<?>,IFormat<java.lang.Object>> getCustomPropertyFormatter()
          Allows to customize how properties are being formatted.
 java.lang.String getDelimeter()
          Delimeter, used to separate properties.
 java.io.PrintWriter getPrintWriter()
          Determines the output destination.
 boolean isHeaders()
          Determines whether CVS output strats with list of properties.
 void setCustomPropertyFormatter(java.util.Map<java.lang.Class<?>,IFormat<java.lang.Object>> val)
          Sets dictionary of custom property formatters.
 void setDelimeter(java.lang.String val)
          Sets delimeter.
 void setHeaders(java.lang.Boolean val)
          Sets headers flag.
 void setPrintWriter(java.io.PrintWriter val)
          Sets output destination.
 void writeData(java.lang.String id, java.lang.Object data)
          Writes data out, including optional id string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVDataWriter

public CSVDataWriter(java.lang.Class<?> cls,
                     java.util.List<java.lang.String> list)
Creates new CSVDataWriter to write listed properties of a given class.

Parameters:
cls - type of the objects to be written.
list - list of property names. This is useful if you want to get control over which properties are included. It allows to skip some properties, specify the exact order of properties in the CSV line, or output a single property more than once.

CSVDataWriter

public CSVDataWriter(java.lang.Class<?> cls)
Creates new CSVDataWriter to write objects of a given class. Order of the properties is not well-defined (actually depends on the JVM implementation, apparently). Therefore, it switching headers property to OFF is not recommended.

If you need full control over which properties are written out, and in what order, use CSVDataWriter(Class, List) constructor.

Parameters:
cls - type of the objects to be written.
Method Detail

getDelimeter

public java.lang.String getDelimeter()
Delimeter, used to separate properties.

Default value is ", ". Re-set this is you do not want blank character to follow comma.

Returns:
delimeter string.

setDelimeter

public void setDelimeter(java.lang.String val)
Sets delimeter.

Parameters:
val - delimeter string.

getCustomPropertyFormatter

public java.util.Map<java.lang.Class<?>,IFormat<java.lang.Object>> getCustomPropertyFormatter()
Allows to customize how properties are being formatted. One particularly useful case is properties of java.util.Date type.

Following code illustrates use of custom property format:

 CSVDataWriter writer = ...;
 writer.getCustomPropertyFormatter().put(Date.class, new DateFormat("MM/dd/yyyy"));
 

Returns:
map of custom formatters.

setCustomPropertyFormatter

public void setCustomPropertyFormatter(java.util.Map<java.lang.Class<?>,IFormat<java.lang.Object>> val)
Sets dictionary of custom property formatters.

Parameters:
val - formatters dictionary.

isHeaders

public boolean isHeaders()
Determines whether CVS output strats with list of properties. Default value is true.

Returns:
headers flag.

setHeaders

public void setHeaders(java.lang.Boolean val)
Sets headers flag.

Parameters:
val - headers flag value.

getPrintWriter

public java.io.PrintWriter getPrintWriter()
Determines the output destination. Default destination is System.out.

Returns:
output destination.

setPrintWriter

public void setPrintWriter(java.io.PrintWriter val)
Sets output destination.

Parameters:
val - output destination.

writeData

public void writeData(java.lang.String id,
                      java.lang.Object data)
Description copied from interface: IDataWriter
Writes data out, including optional id string.

Specified by:
writeData in interface IDataWriter
Parameters:
id - string, identifying the quote source.
data - quote data.

close

public void close()
Closes the writer stream.

Specified by:
close in interface IDataWriter


Copyright © 2007 Mike Kroutikov. All Rights Reserved.