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

com.espertech.esper.epl.enummethod.eval.EnumEvalOrderByAscDescEvents Maven / Gradle / Ivy

There is a newer version: 7.1.0
Show newest version
/*
 * *************************************************************************************
 *  Copyright (C) 2006-2015 EsperTech, Inc. All rights reserved.                       *
 *  http://www.espertech.com/esper                                                     *
 *  http://www.espertech.com                                                           *
 *  ---------------------------------------------------------------------------------- *
 *  The software in this package is published under the terms of the GPL license       *
 *  a copy of which has been included with this distribution in the license.txt file.  *
 * *************************************************************************************
 */

package com.espertech.esper.epl.enummethod.eval;

import com.espertech.esper.client.EventBean;
import com.espertech.esper.epl.expression.core.ExprEvaluator;
import com.espertech.esper.epl.expression.core.ExprEvaluatorContext;

import java.util.*;

public class EnumEvalOrderByAscDescEvents extends EnumEvalBase implements EnumEval {

    private final boolean descending;

    public EnumEvalOrderByAscDescEvents(ExprEvaluator innerExpression, int streamCountIncoming, boolean descending) {
        super(innerExpression, streamCountIncoming);
        this.descending = descending;
    }

    public Object evaluateEnumMethod(EventBean[] eventsLambda, Collection target, boolean isNewData, ExprEvaluatorContext context) {
        TreeMap sort = new TreeMap();
        boolean hasColl = false;

        Collection beans = (Collection) target;
        for (EventBean next : beans) {
            eventsLambda[streamNumLambda] = next;

            Comparable comparable = (Comparable) innerExpression.evaluate(eventsLambda, isNewData, context);
            Object entry = sort.get(comparable);

            if (entry == null) {
                sort.put(comparable, next);
                continue;
            }

            if (entry instanceof Collection) {
                ((Collection) entry).add(next);
                continue;
            }

            Deque coll = new ArrayDeque();
            coll.add(entry);
            coll.add(next);
            sort.put(comparable, coll);
            hasColl = true;
        }

        Map sorted;
        if (descending) {
            sorted = sort.descendingMap();
        }
        else {
            sorted = sort;
        }

        if (!hasColl) {
            return sorted.values();
        }

        Deque coll = new ArrayDeque();
        for (Map.Entry entry : sorted.entrySet()) {
            if (entry.getValue() instanceof Collection) {
                coll.addAll((Collection) entry.getValue());
            }
            else {
                coll.add(entry.getValue());
            }
        }
        return coll;
    }
}