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

de.scravy.machina.GuardedStateList Maven / Gradle / Ivy

Go to download

A finite state machine library that features true stateless state machines.

There is a newer version: 1.3.1
Show newest version
// Generated by delombok at Mon May 25 16:22:43 CEST 2015
package de.scravy.machina;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import de.scravy.pair.Pair;

/**
 * INTERNAL
 */
final class GuardedStateList implements Iterable> {
  private final List> list;
  private final S defaultState;

  public GuardedStateList(final List, S>> guardedStates, final S defaultState) {
    final List> list = new ArrayList<>(guardedStates.size());
    for (final Pair, S> guardedState : guardedStates) {
      list.add(new GuardedState(guardedState.getFirst(), guardedState.getSecond()));
    }
    this.list = Collections.unmodifiableList(list);
    this.defaultState = defaultState;
  }

  public S getStateFor(final E event, final C context) {
    if (context != null) {
      for (final GuardedState guardedState : this.list) {
        if (guardedState.getGuard().evaluate(event, context)) {
          return guardedState.getState();
        }
      }
    }
    return this.defaultState;
  }

  @Override
  public Iterator> iterator() {
    return this.list.iterator();
  }

  @SuppressWarnings("all")
  @javax.annotation.Generated("lombok")
  public S getDefaultState() {
    return this.defaultState;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy