All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tools4j.groovytables.Rows.groovy Maven / Gradle / Ivy

Go to download

A groovy API which allows you to create lists of objects using a table like grammar.

There is a newer version: 1.6
Show newest version
package org.tools4j.groovytables

/**
 * User: ben
 * Date: 4/11/2016
 * Time: 5:46 PM
 */
class Rows implements Iterable{
    public final static Rows EMPTY = new Rows(Collections.EMPTY_LIST)
    private final List columnHeadings
    private final List rows

    Rows(final List rows) {
        this(Collections.EMPTY_LIST, rows)
    }

    Rows(final List columnHeadings, final List rows) {
        this.rows = rows
        this.columnHeadings = columnHeadings
    }

    @Deprecated
    /**
     * Deprecated - Use forEachRow instead
     */
    public  T execute(final Closure closure){
        return forEachRow(closure)
    }

    public  T forEachRow(final Closure closure){
        final ClosureMethod closureMethod = new ClosureMethod<>(closure)

        for(final Row row: rows){
            final ClosureCallPrecursor precursor;
            if(hasColumnHeadings()){
                precursor = closureMethod.getCallPrecursor(columnHeadings, row.values.toArray())
            } else {
                precursor = closureMethod.getCallPrecursor(row.values.toArray())
            }
            if(precursor.suitability == Suitability.NOT_SUITABLE){
                throw new IllegalArgumentException("Not Suitable!")
            } else {
                precursor.executeMethod()
            }
        }
    }

    public boolean hasColumnHeadings(){
        return !columnHeadings.isEmpty()
    }

    public boolean isEmpty(){
        return rows.isEmpty()
    }

    public static Rows with(final Closure tableContent) {
        final List rows = SimpleTableParser.createListOfRows(tableContent)
        return new Rows(rows)
    }

    public Iterator iterator(){
        return rows.iterator()
    }

    public int size() {
        return rows.size()
    }

    public Row get(final int index) {
        return rows.get(index)
    }

    List getColumnHeadings() {
        return columnHeadings
    }

    Object[] toArray() {
        return rows.toArray()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy