Skip to content

Commit

Permalink
Add compilable examples
Browse files Browse the repository at this point in the history
  • Loading branch information
frankkusters committed Oct 8, 2014
1 parent 8f4c524 commit 221dfee
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 37 deletions.
58 changes: 21 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,32 @@
[![Build Status](https://travis-ci.org/oxo42/stateless4j.svg?branch=master)](https://travis-ci.org/oxo42/stateless4j)

## Introduction ##
Create **state machines** and lightweight state machine-based workflows **directly in java code**:
Create **state machines** and lightweight state machine-based workflows **directly in java code**.

```java
Action callStartTimer = new Action() {
@Override
public void doIt() {
startCallTimer();
}
};
Action callStopTimer = new Action() {
@Override
public void doIt() {
stopCallTimer();
}
};

enum State {
Ringing, Connected, OnHold, OffHook
}

enum Trigger {
CallDialed, CallConnected, PlacedOnHold, LeftMessage, HungUp
}

StateMachine<State, Trigger> phoneCall = new StateMachine<State, Trigger>(State.OffHook);

phoneCall.configure(State.OffHook)
.permit(Trigger.CallDialed, State.Ringing);

phoneCall.configure(State.Ringing)
.permit(Trigger.HungUp, State.OffHook)
.permit(Trigger.CallConnected, State.Connected);

phoneCall.configure(State.Connected)
.onEntry(callStartTimer)
.onExit(callStopTimer)
.permit(Trigger.LeftMessage, State.OffHook)
.permit(Trigger.HungUp, State.OffHook)
.permit(Trigger.PlacedOnHold, State.OnHold);
StateMachineConfig<State, Trigger> phoneCallConfig = new StateMachineConfig<>();

phoneCallConfig.configure(State.OffHook)
.permit(Trigger.CallDialed, State.Ringing);

phoneCallConfig.configure(State.Ringing)
.permit(Trigger.HungUp, State.OffHook)
.permit(Trigger.CallConnected, State.Connected);

// this example uses Java 8 method references
// a Java 7 example is provided in /examples
phoneCallConfig.configure(State.Connected)
.onEntry(this::startCallTimer)
.onExit(this::stopCallTimer)
.permit(Trigger.LeftMessage, State.OffHook)
.permit(Trigger.HungUp, State.OffHook)
.permit(Trigger.PlacedOnHold, State.OnHold);

// ...

StateMachine<State, Trigger> phoneCall =
new StateMachine<>(State.OffHook, phoneCallConfig);

phoneCall.fire(Trigger.CallDialed);
assertEquals(State.Ringing, phoneCall.getState());
```
Expand Down
67 changes: 67 additions & 0 deletions examples/PhoneCallJava7.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package example;

import com.github.oxo42.stateless4j.StateMachine;
import com.github.oxo42.stateless4j.StateMachineConfig;
import com.github.oxo42.stateless4j.delegates.Action;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class PhoneCallJava7 {

@Test
public void testPhoneRings() throws Exception {
Action callStartTimer = new Action() {
@Override
public void doIt() {
startCallTimer();
}
};
Action callStopTimer = new Action() {
@Override
public void doIt() {
stopCallTimer();
}
};

StateMachineConfig<State, Trigger> phoneCallConfig = new StateMachineConfig<>();

phoneCallConfig.configure(State.OffHook)
.permit(Trigger.CallDialed, State.Ringing);

phoneCallConfig.configure(State.Ringing)
.permit(Trigger.HungUp, State.OffHook)
.permit(Trigger.CallConnected, State.Connected);

phoneCallConfig.configure(State.Connected)
.onEntry(callStartTimer)
.onExit(callStopTimer)
.permit(Trigger.LeftMessage, State.OffHook)
.permit(Trigger.HungUp, State.OffHook)
.permit(Trigger.PlacedOnHold, State.OnHold);

// ...

StateMachine<State, Trigger> phoneCall = new StateMachine<>(State.OffHook, phoneCallConfig);

phoneCall.fire(Trigger.CallDialed);
assertEquals(State.Ringing, phoneCall.getState());
}

private void stopCallTimer() {
// ...
}

private void startCallTimer() {
// ...
}

private enum State {
Ringing, Connected, OnHold, OffHook

}
private enum Trigger {
CallDialed, CallConnected, PlacedOnHold, LeftMessage, HungUp

}
}
54 changes: 54 additions & 0 deletions examples/PhoneCallJava8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package example;

import com.github.oxo42.stateless4j.StateMachine;
import com.github.oxo42.stateless4j.StateMachineConfig;
import com.github.oxo42.stateless4j.delegates.Action;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class PhoneCallJava8 {

@Test
public void testPhoneRings() throws Exception {
StateMachineConfig<State, Trigger> phoneCallConfig = new StateMachineConfig<>();

phoneCallConfig.configure(State.OffHook)
.permit(Trigger.CallDialed, State.Ringing);

phoneCallConfig.configure(State.Ringing)
.permit(Trigger.HungUp, State.OffHook)
.permit(Trigger.CallConnected, State.Connected);

phoneCallConfig.configure(State.Connected)
.onEntry(this::startCallTimer)
.onExit(this::stopCallTimer)
.permit(Trigger.LeftMessage, State.OffHook)
.permit(Trigger.HungUp, State.OffHook)
.permit(Trigger.PlacedOnHold, State.OnHold);

// ...

StateMachine<State, Trigger> phoneCall = new StateMachine<>(State.OffHook, phoneCallConfig);

phoneCall.fire(Trigger.CallDialed);
assertEquals(State.Ringing, phoneCall.getState());
}

private void stopCallTimer() {
// ...
}

private void startCallTimer() {
// ...
}

private enum State {
Ringing, Connected, OnHold, OffHook

}
private enum Trigger {
CallDialed, CallConnected, PlacedOnHold, LeftMessage, HungUp

}
}

0 comments on commit 221dfee

Please sign in to comment.