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

org.statefulj.persistence.memory.MemoryPersisterImpl Maven / Gradle / Ivy

There is a newer version: 3.0
Show newest version
/***
 *
 * Copyright 2014 Andrew Hall
 *
 * 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 org.statefulj.persistence.memory;

import org.statefulj.common.utils.ReflectionUtils;
import org.statefulj.fsm.Persister;
import org.statefulj.fsm.StaleStateException;
import org.statefulj.fsm.model.State;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Thread safe, in memory Persister.
 *
 * @author Andrew Hall
 *
 */
public class MemoryPersisterImpl implements Persister {

	private final Map> states = new HashMap>();
	private State startState;
	private String stateFieldName;
    private volatile Field stateField;

	public MemoryPersisterImpl() {}

	public MemoryPersisterImpl(final Collection> states, final State startState) {
		setStartState(startState);
		setStates(states);
	}

	public MemoryPersisterImpl(List> states, State startState, String stateFieldName) {
		this(states, startState);
		this.stateFieldName = stateFieldName;
	}

	public MemoryPersisterImpl(T stateful, List> states, State startState) {
		this(states, startState);
		this.setCurrent(stateful, startState);
	}

	public MemoryPersisterImpl(T stateful, List> states, State startState, String stateFieldName) {
		this(states, startState, stateFieldName);
		this.setCurrent(stateful, startState);
	}

	public synchronized Collection> getStates() {
		return states.values();
	}

	public synchronized State addState(final State state) {
		return states.put(state.getName(), state);
	}

	public State removeState(final State state) {
		return removeState(state.getName());
	}

	public synchronized State removeState(final String name) {
		return states.remove(name);
	}

	@Override
	public synchronized void setStates(final Collection> states) {
		//Clear the map
		//
		this.states.clear();

		//Add new states
		//
		for(State state : states) {
			this.states.put(state.getName(), state);
		}
	}

	public State getStartState() {
		return startState;
	}

	@Override
	public void setStartState(final State startState) {
		this.startState = startState;
	}

	public String getStateFieldName() {
		return stateFieldName;
	}

	public void setStateFieldName(String stateFieldName) {
		this.stateFieldName = stateFieldName;
	}

	@Override
	public State getCurrent(T stateful) {
		try {
			String key = (String)getStateField(stateful).get(stateful);
			State state = (key != null) ? states.get(key) : null;
			return (state != null) ? state : this.startState;
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void setCurrent(T stateful, State current) {
		synchronized(stateful) {
			try {
				getStateField(stateful).set(stateful, current.getName());
			} catch(Exception e) {
				throw new RuntimeException(e);
			}
		}
	}

	/*
	 * Serialize all update of state.  Ensure that the current state is the same State that
	 * was evaluated. If not, throw an exception
	 *
	 * (non-Javadoc)
	 * @see org.fsm.Persister#setCurrent(org.fsm.model.State, org.fsm.model.State)
	 */
	@Override
	public void setCurrent(T stateful, State current, State next) throws StaleStateException {
		synchronized(stateful) {
			if (this.getCurrent(stateful).equals(current)) {
				this.setCurrent(stateful, next);
			} else {
				throw new StaleStateException();
			}
		}
	}

	private Field getStateField(final T stateful) {
        if (stateField == null)
            stateField = locateStateField(stateful);
        return
            stateField;
	}

    private synchronized Field locateStateField(final T stateful) {
        Field field = null;

        // If a state field name was provided, retrieve by name
        //
        if (this.stateFieldName != null && !this.stateFieldName.equals("")) {
			field =
				ReflectionUtils.getField(stateful.getClass(), this.stateFieldName);
        }


        // Else, fetch the field by Annotation
        //
        else {
            field = ReflectionUtils.getFirstAnnotatedField(stateful.getClass(), org.statefulj.persistence.annotations.State.class);
            if (field != null) {
                this.stateFieldName = field.getName();
            }
        }

        if (field == null) {
            throw new RuntimeException("Unable to locate a State field for stateful: " + stateful);
        }

        // Ensure that we can access the field
        //
        field.setAccessible(true);
        return field;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy