Data

public class Data
Hierarchy:
Object ⯈ Data

Data table.

Values are stored as nullable strings in rows and columns. Columns can be accessed by name (header).

Summary

Nested Classes
class Data.Row
Contains one row of values.
Constructors
Data (String... headers)
Class constructor.
Instance Methods
void addCol (String hdr)
Modify column headers to add new column.
void addCol (String hdr, Formula<?> calc)
Modify column headers to add new column.
void addCol (String hdr, String value)
Modify column headers to add new column.
Data.Row addRow ()
Create new empty data row.
Data.Row addRow (String... vs)
Create new data row and initialise with values.
void append (Data other)
Append data from another table respecting column names.
void appendMarked (Data other, String markHdr, String mark)
Append and mark data from another table respecting column names.
void clear ()
Remove all data rows.
int colCount ()
Get number of columns.
Data copy ()
Data copyCols (String[] headers)
int count ()
Get count of data rows.
Data filter (Filter filter)
int findCol (String hdr)
Find column index by column name.
Data.Row firstRow ()
Get the first data row or null if the table is empty.
<T>void fold (Formula.Var<T> acc, Formula<T> calc)
String getHeader (int index)
Get column name by column index.
Data groupBy (String hdr, String[] headers, Fold<?>... folds)
int[] indexMap (String[] hdrs)
Find column indices by column names.
Data.Row lastRow ()
Get the last data row or null if the table is empty.
Data limit (int count)
int[] optionalIndexMap (String[] hdrs)
Find column indices by column names without throwing an exception.
void print ()
void print (PrintStream out, boolean withHeaders)
void printCols (PrintStream out, boolean withHeaders, String... cols)
void printCols (String... cols)
void printHeaders (PrintStream out)
void printHeaders (PrintStream out, String[] cols)
void recalc (String hdr, Formula<?> calc)
Recalculate contents of a column using formula.
void renameHeader (String oldName, String newName)
Rename column.
void renameHeaders (String[] headers)
Rename all headers.
void replaceAll (String hdr, String oldValue, String newValue)
Find and replace values in a column.
Iterable<Data.Row> rows ()
Get data rows as unmodifiable iterable.
void sort (boolean asc, Formula<?>... calc)
void sort (boolean asc, String... hdr)
void sort (Formula<?>... calc)
void sort (String... hdr)
Show all inherited methods (11 more)
protected native Object clone ()
Inherited from Object.
boolean equals (Object)
Inherited from Object.
protected void finalize ()
Inherited from Object.
final native Class<?> getClass ()
Inherited from Object.
native int hashCode ()
Inherited from Object.
final native void notify ()
Inherited from Object.
final native void notifyAll ()
Inherited from Object.
String toString ()
Inherited from Object.
final void wait ()
Inherited from Object.
final native void wait (long)
Inherited from Object.
final void wait (long, int)
Inherited from Object.
Static Methods
static Data range (String hdr, int min, int max)
static Data range (String hdr, int min, int max, int step)
static Data range (String hdr, String fmt, double min, double max, double step)
static Data read (File file)
static Data read (File file, String[] headers)
static Data read (File file, String[] headers, String sepRegex)
static void write (Data data, File file)

Data

public Data (
	String... headers
);

Class constructor.

Parameters
headers
column headers
Throws
InvalidParameterException
duplicate header names

renameHeaders

public void renameHeaders (
	String[] headers
);

Rename all headers.

Parameters
headers
new header names in order, must match the current number of columns
Throws
InvalidParameterException
number of items in headers does not match the number of columns
InvalidParameterException
duplicate header names

renameHeader

public void renameHeader (
	String oldName,
	String newName
);

Rename column.

Parameters
oldName
old column name
newName
new column name
Throws
NoSuchElementException
no column oldName
InvalidParameterException
column newName already exists

colCount

public int colCount ();

Get number of columns.

Returns

number of columns

getHeader

public String getHeader (
	int index
);

Get column name by column index.

Parameters
index
column index
Returns

column name

Throws
ArrayIndexOutOfBoundsException
if index<0 or index>=colCount()
See also

colCount()

addRow

public Data.Row addRow ();

Create new empty data row.

Returns

new row

addRow

public Data.Row addRow (
	String... vs
);

Create new data row and initialise with values. Copying is range-safe in case the number of values in vs does not match the number of columns.

Parameters
vs
initial values in column order
Returns

new row

rows

public Iterable<Data.Row> rows ();

Get data rows as unmodifiable iterable.

Returns

data rows

firstRow

public Data.Row firstRow ();

Get the first data row or null if the table is empty.

Returns

data row

lastRow

public Data.Row lastRow ();

Get the last data row or null if the table is empty.

Returns

data row

clear

public void clear ();

Remove all data rows.

findCol

public int findCol (
	String hdr
);

Find column index by column name. Lookup is relatively fast as it uses a hash map.

Parameters
hdr
column header
Returns

column index

Throws
NoSuchElementException
column not found

indexMap

public int[] indexMap (
	String[] hdrs
);

Find column indices by column names. Throws an exception if any of the names is not found.

Parameters
hdrs
column headers, cannot be null
Returns

array of column indices in the order of hdrs

Throws
NoSuchElementException
column not found
NullPointerException
if hdrs is null

optionalIndexMap

public int[] optionalIndexMap (
	String[] hdrs
);

Find column indices by column names without throwing an exception. If a column name does not exist, -1 index is returned.

Parameters
hdrs
column headers, cannot be null
Returns

array of column indices in the order of hdrs

Throws
NullPointerException
if hdrs is null

count

public int count ();

Get count of data rows.

Returns

row count

addCol

public void addCol (
	String hdr,
	Formula<?> calc
);

Modify column headers to add new column. Values of the new column can be calculated using calc or initalised to null if formula is not provided.

Parameters
hdr
new column name
calc
formula to calculate initial values, can be null
See also

recalc(String, Formula), addCol(String, String)

addCol

public void addCol (
	String hdr,
	String value
);

Modify column headers to add new column. Values of the new column are initialised to the given constant.

Parameters
hdr
new column name
value
initial value, can be null
See also

addCol(String, Formula)

addCol

public void addCol (
	String hdr
);

Modify column headers to add new column. Values of the new column are initialised to null.

Parameters
hdr
new column name
See also

addCol(String, Formula), addCol(String, String)

recalc

public void recalc (
	String hdr,
	Formula<?> calc
);

Recalculate contents of a column using formula. If formula is not provided, column values are set to null.

Parameters
hdr
column name
calc
formula to calculate values, can be null
See also

addCol(String, Formula)

replaceAll

public void replaceAll (
	String hdr,
	String oldValue,
	String newValue
);

Find and replace values in a column. Uses case-sensitive string comparison for non-null values.

Parameters
hdr
column to search values
oldValue
old value, can be null
newValue
replacement value, can be null

fold

public <T>void fold (
	Formula.Var<T> acc,
	Formula<T> calc
);

append

public void append (
	Data other
);

Append data from another table respecting column names.

Parameters
other
source table
Throws
NullPointerException
source table is null

appendMarked

public void appendMarked (
	Data other,
	String markHdr,
	String mark
);

Append and mark data from another table respecting column names. Appended rows are marked with a specified value.

Typical use for this method is to keep track of data sources when combining data from multiple tables. For example:

 Data data = Data.read(new File("data1.csv"));
 data.addCol("source", "data1");
 data.appendMarked(Data.read(new File("data2.csv")), "source", "data2");
 data.appendMarked(Data.read(new File("data3.csv")), "source", "data3");
Parameters
other
source table
markHdr
which column in the destination table to mark
mark
marking value, can be null
Throws
NullPointerException
source table is null
NoSuchElementException
column markHdr does not exist
See also

append(Data), addCol(String, String)

sort

public void sort (
	boolean asc,
	Formula<?>... calc
);

sort

public void sort (
	boolean asc,
	String... hdr
);

sort

public void sort (
	Formula<?>... calc
);

sort

public void sort (
	String... hdr
);

printHeaders

public void printHeaders (
	PrintStream out
);

printHeaders

public void printHeaders (
	PrintStream out,
	String[] cols
);

print

public void print (
	PrintStream out,
	boolean withHeaders
);

printCols

public void printCols (
	PrintStream out,
	boolean withHeaders,
	String... cols
);

print

public void print ();

printCols

public void printCols (
	String... cols
);

filter

public Data filter (
	Filter filter
);

limit

public Data limit (
	int count
);

copy

public Data copy ();

copyCols

public Data copyCols (
	String[] headers
);

groupBy

public Data groupBy (
	String hdr,
	String[] headers,
	Fold<?>... folds
);

read

public static Data read (
	File file,
	String[] headers,
	String sepRegex
);

read

public static Data read (
	File file,
	String[] headers
);

read

public static Data read (
	File file
);

write

public static void write (
	Data data,
	File file
);

range

public static Data range (
	String hdr,
	int min,
	int max,
	int step
);

range

public static Data range (
	String hdr,
	int min,
	int max
);

range

public static Data range (
	String hdr,
	String fmt,
	double min,
	double max,
	double step
);