io.dyn.core.fsm.StateMachine.java Source code

Java tutorial

Introduction

Here is the source code for io.dyn.core.fsm.StateMachine.java

Source

/*
 * Copyright (c) 2011-2012 by the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.dyn.core.fsm;

import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.atomic.AtomicReference;

import io.dyn.core.EventedBase;
import io.dyn.core.Lifecycle;
import org.springframework.util.Assert;

/**
 * @author Jon Brisbin <jon@jbrisbin.com>
 */
public class StateMachine extends EventedBase<StateMachine> implements Lifecycle<StateMachine> {

    private final Object statesMutex = new Object();
    private final List<String> states;
    private final ListIterator<String> cursor;
    private final AtomicReference<String> currentState = new AtomicReference<>();

    public StateMachine(List<String> states) {
        Assert.notNull(states, "States cannot be null");
        this.states = states;
        this.cursor = states.listIterator();
    }

    @Override
    public StateMachine start() {
        event("start");
        next();
        return this;
    }

    @Override
    public StateMachine stop() {
        event("stop");
        return this;
    }

    public List<String> states() {
        return Collections.unmodifiableList(this.states);
    }

    public String currentState() {
        return currentState.get();
    }

    public StateMachine previous() {
        if (cursor.hasPrevious()) {
            String prev;
            synchronized (statesMutex) {
                prev = cursor.previous();
            }
            currentState.set(prev);
            event(prev);
        }
        return this;
    }

    public StateMachine current() {
        return event(currentState.get());
    }

    public StateMachine next() {
        if (cursor.hasNext()) {
            String next;
            synchronized (statesMutex) {
                next = cursor.next();
            }
            currentState.set(next);
            return event(next);
        } else {
            return end();
        }
    }

    public StateMachine state(String state) {
        synchronized (statesMutex) {
            cursor.set(state);
        }
        currentState.set(state);
        return event(state);
    }

    public StateMachine end() {
        return event("end");
    }

    @Override
    public StateMachine recycle() {
        synchronized (statesMutex) {
            while (cursor.previousIndex() > -1) {
                cursor.previous();
            }
        }
        return super.recycle();
    }

}