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

net.sf.jxls.parser.Aggregator Maven / Gradle / Ivy

Go to download

jXLS is a small and easy-to-use Java library for generating Excel files using XLS templates

There is a newer version: 1.0.6
Show newest version
package net.sf.jxls.parser;

/**
 * @author Curtis Stanford
 * @author Leonid Vysochyn
 */
public abstract class Aggregator {
    public static Aggregator getInstance(String function) {
        if (function.equalsIgnoreCase("sum")) return new Sum();
        else if (function.equalsIgnoreCase("avg")) return new Average();
        else if (function.equalsIgnoreCase("min")) return new Minimum();
        else if (function.equalsIgnoreCase("max")) return new Maximum();
        else if (function.equalsIgnoreCase("count")) return new Count();
        else return null;
    }

    public void add(Object o) {
    }

    public Object getResult() {
        return null;
    }

    static class Sum extends Aggregator {
        private double total = 0.0;

        public void add(Object o) {
            if (o instanceof Number) total += ((Number) o).doubleValue();
        }

        public Object getResult() {
            return new Double(total);
        }
    }

    static class Average extends Aggregator {
        private double total = 0.0;
        private double count = 0.0;

        public void add(Object o) {
            if (o instanceof Number) {
                total += ((Number) o).doubleValue();
                count++;
            }
        }

        public Object getResult() {
            if (count == 0.0) return null;
            else return new Double(total / count);
        }
    }

    static class Minimum extends Aggregator {
        private Object min = null;

        public void add(Object o) {
            if (o != null) {
                if (min == null) min = o;
                else if (o instanceof Comparable) {
                    if (((Comparable) o).compareTo(min) < 0) min = o;
                }
            }
        }

        public Object getResult() {
            return min;
        }
    }

    static class Maximum extends Aggregator {
        private Object max = null;

        public void add(Object o) {
            if (o != null) {
                if (max == null) max = o;
                else if (o instanceof Comparable) {
                    if (((Comparable) o).compareTo(max) > 0) max = o;
                }
            }
        }

        public Object getResult() {
            return max;
        }
    }

    static class Count extends Aggregator {
        private int count = 0;

        public void add(Object o) {
            if (o != null) count++;
        }

        public Object getResult() {
            return new Integer(count);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy