forked from stateless4j/stateless4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f4c524
commit 221dfee
Showing
3 changed files
with
142 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
} |