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

com.espertech.esper.rowregex.RegexPartitionStateRepoScheduleStateImpl 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.rowregex;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

public class RegexPartitionStateRepoScheduleStateImpl implements RegexPartitionStateRepoScheduleState {

    private final RegexPartitionTerminationStateComparator terminationStateCompare;
    private final TreeMap schedule = new TreeMap();

    public RegexPartitionStateRepoScheduleStateImpl(RegexPartitionTerminationStateComparator terminationStateCompare) {
        this.terminationStateCompare = terminationStateCompare;
    }

    public boolean isEmpty() {
        return schedule.isEmpty();
    }

    public boolean putOrAdd(long matchBeginTime, RegexNFAStateEntry state) {
        Object value = schedule.get(matchBeginTime);
        if (value == null) {
            schedule.put(matchBeginTime, state);
            return true;
        }

        if (value instanceof RegexNFAStateEntry)
        {
            RegexNFAStateEntry valueEntry = (RegexNFAStateEntry) value;
            List list = new ArrayList();
            list.add(valueEntry);
            list.add(state);
            schedule.put(matchBeginTime, list);
        }
        else
        {
            List list = (List) value;
            list.add(state);
        }

        return false;
    }

    public Object get(long matchBeginTime) {
        return schedule.get(matchBeginTime);
    }

    public long firstKey() {
        return schedule.firstKey();
    }

    public void removeAddRemoved(long matchBeginTime, List foundStates) {
        Object found = schedule.remove(matchBeginTime);
        if (found == null) {
            return;
        }
        if (found instanceof RegexNFAStateEntry) {
            foundStates.add((RegexNFAStateEntry) found);
        }
        else {
            foundStates.addAll((List) found);
        }
    }

    public boolean containsKey(long matchBeginTime) {
        return schedule.containsKey(matchBeginTime);
    }

    public boolean findRemoveAddToList(long matchBeginTime, RegexNFAStateEntry state, List foundStates) {
        Object entry = schedule.get(matchBeginTime);
        if (entry == null) {
            return false;
        }
        if (entry instanceof RegexNFAStateEntry) {
            RegexNFAStateEntry single = (RegexNFAStateEntry) entry;
            if (terminationStateCompare.compareTerminationStateToEndState(state, single)) {
                schedule.remove(matchBeginTime);
                foundStates.add(single);
                return true;
            }
            return false;
        }

        List entries = (List) entry;
        Iterator it = entries.iterator();
        boolean removed = false;
        for (;it.hasNext();) {
            RegexNFAStateEntry endState = it.next();
            if (terminationStateCompare.compareTerminationStateToEndState(state, endState)) {
                it.remove();
                foundStates.add(endState);
                removed = true;
            }
        }
        if (entries.isEmpty()) {
            schedule.remove(matchBeginTime);
        }
        return removed;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy