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

fuzzycsv.Count.groovy Maven / Gradle / Ivy

Go to download

A groovy/java tabular Data (from CSV,SQL,JSON) processing library that supports fuzzy column matching,tranformations/merging/querying etc

There is a newer version: 1.9.1-groovy4
Show newest version
package fuzzycsv

import groovy.transform.CompileStatic

@CompileStatic
class Count extends AbstractAggregator {

            List columns
    private boolean    unique = false

    Count() {}

    Count(List columns, List data) {
        this.setData(data)
        this.columns = columns
    }

    @Override
    Object getValue() {
        def data = getData(columns)
        def unique = unique ? data.unique() : data
        if (columns)
            return unique.count { List r -> r.any { c -> c != null } }
        return unique.size()
    }

    Count unique() {
        unique = true
        return this
    }

    Count all() {
        unique = false
        return this
    }


    static Count count() {
        return new Count(columnName: "count()")
    }

    static Count plnCount(String name) {
        return new Count(columnName: name)
    }

    static Count count(String name, Object... columnsForCounting) {
        return new Count(columnName: name, columns: columnsForCounting as List)
    }

    static Count countUnique() {
        return new Count(columnName: "count()").unique()
    }

    static Count plnCountUnique(String name) {
        return new Count(columnName: name).unique()
    }

    static Count countUnique(String name, Object... columnsForCounting) {
        return new Count(columnName: name, columns: columnsForCounting as List).unique()
    }

    @Override
    String getColumnName() {
        if (!super.columnName) {
            if (unique)
                return "countunique(${columns*.toString().join(',')})"
            else
                return "count(${columns*.toString().join(',')})"
        }
        else {
            return super.getColumnName()
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy