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

net.intelie.pipes.GroupBy Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
package net.intelie.pipes;

import net.intelie.pipes.types.ClauseInfo;
import net.intelie.pipes.types.RowFields;
import net.intelie.pipes.types.RowType;
import net.intelie.pipes.types.Type;
import net.intelie.pipes.util.Preconditions;

import java.io.Serializable;
import java.util.Iterator;

public interface GroupBy extends Evaluable, HasType, Serializable {
    GroupBy NONE = new NoneGroupBy();

    int size();

    boolean isEmpty();

    GroupBy ensureNoExpiry() throws PipeException;

    GroupBy ensureExpiry() throws PipeException;

    void validate(ValidationContext context) throws PipeException;

    ClauseInfo info();

     State newState(Init initializer);

    void evalUnsafe(Scope parent, Object obj, UnsafeRow row);

    void evalUnsafe(Scope parent, Object obj, UnsafeRow row, int index);

    interface Init {
        T init(Row key);
    }

    interface State extends Iterable, Serializable {
        int size();

        T get(Scope parent, Object obj);

        void offerTimestamp(long timestamp);

        T emptyState();
    }

    class NoneGroupBy implements GroupBy {
        private static final long serialVersionUID = 1L;
        private static final ArrayRow EMPTY_ROW = new ArrayRow();

        @Override
        public boolean isEmpty() {
            return true;
        }

        @Override
        public int size() {
            return 0;
        }

        @Override
        public GroupBy ensureNoExpiry() throws PipeException {
            return this;
        }

        @Override
        public GroupBy ensureExpiry() throws PipeException {
            return this;
        }

        @Override
        public void validate(ValidationContext context) throws PipeException {
        }

        @Override
        public void evalUnsafe(Scope parent, Object obj, UnsafeRow row) {
        }

        @Override
        public void evalUnsafe(Scope parent, Object obj, UnsafeRow row, int index) {
        }

        @Override
        public ClauseInfo info() {
            return new ClauseInfo();
        }

        @Override
        public Row eval(Scope parent, Object obj) {
            return EMPTY_ROW;
        }

        @Override
        public PropertyVisitor visit(Scope parent, PropertyVisitor visitor) {
            return visitor;
        }

        @Override
        public Type type() {
            return new RowType(new RowFields());
        }

        @Override
        public String toString() {
            return "";
        }

        @Override
        public  State newState(final Init initializer) {
            return new State() {
                private static final long serialVersionUID = 1L;

                private T state = initializer.init(EMPTY_ROW);

                @Override
                public int size() {
                    return 1;
                }

                @Override
                public void offerTimestamp(long timestamp) {
                }

                @Override
                public T get(Scope parent, Object obj) {
                    return state;
                }

                @Override
                public T emptyState() {
                    return state;
                }

                @Override
                public Iterator iterator() {
                    return new Iterator() {
                        private boolean consumed = false;

                        @Override
                        public boolean hasNext() {
                            return !consumed;
                        }

                        @Override
                        public T next() {
                            Preconditions.checkState(!consumed);
                            consumed = true;
                            return state;
                        }

                        @Override
                        public void remove() {
                            Preconditions.checkState(consumed);
                            state = initializer.init(EMPTY_ROW);
                        }
                    };
                }
            };
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy