SynchronizedTrafficLightCtrlTestStatemachine.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package traffic.light.trafficlightctrltest;
  2. import traffic.light.ITimer;
  3. import traffic.light.ITimerCallback;
  4. import traffic.light.trafficlightctrltest.TrafficLightCtrlTestStatemachine.State;
  5. /**
  6. * Runnable wrapper of TrafficLightCtrlTestStatemachine. This wrapper provides a thread-safe
  7. * instance of the state machine.
  8. *
  9. * Please report bugs and issues...
  10. */
  11. public class SynchronizedTrafficLightCtrlTestStatemachine implements ITrafficLightCtrlTestStatemachine {
  12. /**
  13. * The core state machine is simply wrapped and the event processing will be
  14. * delegated to that state machine instance. This instance will be created
  15. * implicitly.
  16. */
  17. protected TrafficLightCtrlTestStatemachine statemachine = new TrafficLightCtrlTestStatemachine();
  18. /**
  19. * Interface object for SCInterface
  20. */
  21. protected class SynchronizedSCInterface implements SCInterface {
  22. public void setSCInterfaceOperationCallback(SCInterfaceOperationCallback operationCallback) {
  23. synchronized(statemachine) {
  24. statemachine.getSCInterface().setSCInterfaceOperationCallback(operationCallback);
  25. }
  26. }
  27. public boolean isRaisedDisplayRed() {
  28. synchronized(statemachine) {
  29. return statemachine.getSCInterface().isRaisedDisplayRed();
  30. }
  31. }
  32. public boolean isRaisedDisplayGreen() {
  33. synchronized(statemachine) {
  34. return statemachine.getSCInterface().isRaisedDisplayGreen();
  35. }
  36. }
  37. public boolean isRaisedDisplayYellow() {
  38. synchronized(statemachine) {
  39. return statemachine.getSCInterface().isRaisedDisplayYellow();
  40. }
  41. }
  42. public boolean isRaisedDisplayNone() {
  43. synchronized(statemachine) {
  44. return statemachine.getSCInterface().isRaisedDisplayNone();
  45. }
  46. }
  47. };
  48. protected SCInterface sCInterface;
  49. public SynchronizedTrafficLightCtrlTestStatemachine() {
  50. sCInterface = new SynchronizedSCInterface();
  51. }
  52. public synchronized SCInterface getSCInterface() {
  53. return sCInterface;
  54. }
  55. /*================ TIME EVENT HANDLING ================
  56. /** An external timer instance is required. */
  57. protected ITimer externalTimer;
  58. /** Internally we use a timer proxy that queues time events together with other input events. */
  59. protected ITimer timerProxy = new ITimer() {
  60. /** Simply delegate to external timer with a modified callback. */
  61. @Override
  62. public void setTimer(ITimerCallback callback, int eventID, long time,
  63. boolean isPeriodic) {
  64. externalTimer.setTimer(SynchronizedTrafficLightCtrlTestStatemachine.this, eventID, time, isPeriodic);
  65. }
  66. @Override
  67. public void unsetTimer(ITimerCallback callback, int eventID) {
  68. externalTimer.unsetTimer(SynchronizedTrafficLightCtrlTestStatemachine.this, eventID);
  69. }
  70. };
  71. /**
  72. * Set the {@link ITimer} for the state machine. It must be set externally
  73. * on a timed state machine before a run cycle can be correct executed.
  74. *
  75. * @param timer
  76. */
  77. public void setTimer(ITimer timer) {
  78. synchronized(statemachine) {
  79. this.externalTimer = timer;
  80. /* the wrapped state machine uses timer proxy as timer */
  81. statemachine.setTimer(timerProxy);
  82. }
  83. }
  84. /**
  85. * Returns the currently used timer.
  86. *
  87. * @return {@link ITimer}
  88. */
  89. public ITimer getTimer() {
  90. return externalTimer;
  91. }
  92. public void timeElapsed(int eventID) {
  93. synchronized (statemachine) {
  94. statemachine.timeElapsed(eventID);
  95. }
  96. }
  97. /**
  98. * init() will be delegated thread-safely to the wrapped state machine.
  99. */
  100. public void init() {
  101. synchronized(statemachine) {
  102. statemachine.init();
  103. }
  104. }
  105. /**
  106. * enter() will be delegated thread-safely to the wrapped state machine.
  107. */
  108. public void enter() {
  109. synchronized(statemachine) {
  110. statemachine.enter();
  111. }
  112. }
  113. /**
  114. * exit() will be delegated thread-safely to the wrapped state machine.
  115. */
  116. public void exit() {
  117. synchronized(statemachine) {
  118. statemachine.exit();
  119. }
  120. }
  121. /**
  122. * isActive() will be delegated thread-safely to the wrapped state machine.
  123. */
  124. public boolean isActive() {
  125. synchronized(statemachine) {
  126. return statemachine.isActive();
  127. }
  128. }
  129. /**
  130. * isFinal() will be delegated thread-safely to the wrapped state machine.
  131. */
  132. public boolean isFinal() {
  133. synchronized(statemachine) {
  134. return statemachine.isFinal();
  135. }
  136. }
  137. /**
  138. * isStateActive() will be delegated thread-safely to the wrapped state machine.
  139. */
  140. public boolean isStateActive(State state) {
  141. synchronized(statemachine) {
  142. return statemachine.isStateActive(state);
  143. }
  144. }
  145. /**
  146. * runCycle() will be delegated thread-safely to the wrapped state machine.
  147. */
  148. @Override
  149. public void runCycle() {
  150. synchronized (statemachine) {
  151. statemachine.runCycle();
  152. }
  153. }
  154. }