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

main.java.pro.verron.officestamper.experimental.ExcelCollector Maven / Gradle / Ivy

Go to download

Office-stamper is a Java template engine for docx documents, forked from org.wickedsource.docx-stamper

There is a newer version: 2.6.0
Show newest version
package pro.verron.officestamper.experimental;

import java.util.ArrayList;
import java.util.List;

/**
 * The ExcelCollector class is used to collect objects of a specific type from an Excel file.
 * It extends the ExcelVisitor class and overrides the 'before' method to add the objects of the given type to a list.
 *
 * @param  the type of objects to collect
 */
public class ExcelCollector
        extends ExcelVisitor {

    private final Class type;
    private final List list = new ArrayList<>();

    /**
     * Constructs a new ExcelCollector object with the given type.
     *
     * @param type the class representing the type of objects to collect
     */
    public ExcelCollector(Class type) {
        this.type = type;
    }

    /**
     * Collects objects of a specific type from an Excel file.
     *
     * @param     the type of objects to collect
     * @param object the Excel file or object to collect from
     * @param type   the class representing the type of objects to collect
     *
     * @return a List containing the collected objects
     */
    public static  List collect(
            Object object,
            Class type
    ) {
        var collector = new ExcelCollector<>(type);
        collector.visit(object);
        return collector.collect();
    }

    /**
     * Returns a List containing the collected objects.
     *
     * @return a List containing the collected objects
     */
    public List collect() {
        return list;
    }

    /**
     * This method is called before visiting an object in the ExcelCollector class.
     * It checks if the object is an instance of the specified type and adds it to a list if it is.
     *
     * @param object the object being visited
     */
    @Override
    protected void before(Object object) {
        if (type.isInstance(object))
            list.add(type.cast(object));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy