View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.common.state.example;
22  
23  import goldengate.common.exception.IllegalFiniteStateException;
24  import goldengate.common.logging.GgSlf4JLoggerFactory;
25  import goldengate.common.state.MachineState;
26  import goldengate.common.state.example.ExampleEnumState.ExampleTransition;
27  
28  import java.util.EnumSet;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  import org.jboss.netty.logging.InternalLoggerFactory;
32  
33  /**
34   * Example of usage for MachineState based on ExampleEnumState enum class
35   * 
36   * @author Frederic Bregier
37   *
38   */
39  public class ExampleUsageMachineState {
40      /**
41       * An example of usage.
42       * @param args
43       */
44      @SuppressWarnings({
45              "unchecked", "rawtypes" })
46      public static void main(String []args) {
47          InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(null));
48  
49          // Example
50          
51          // First create a HashMap and fill it directly
52          ConcurrentHashMap<ExampleEnumState, EnumSet<ExampleEnumState>> stateMap =
53              new ConcurrentHashMap<ExampleEnumState, EnumSet<ExampleEnumState>>();
54          stateMap.put(ExampleTransition.tRUNNING.elt.state, 
55                  (EnumSet<ExampleEnumState>) ExampleTransition.tRUNNING.elt.set);
56          // Second create the MachineState with the right Map
57          MachineState<ExampleEnumState> machineState1 = 
58              new MachineState(ExampleEnumState.PAUSED, stateMap);
59          // Third, if not already done, fill the Map with the transitions
60          for (ExampleTransition trans: ExampleTransition.values()) {
61              machineState1.addNewAssociation(trans.elt);
62          }
63  
64          // Or First create the MachineSate but empty
65          MachineState<ExampleEnumState> machineState2 = 
66              new MachineState(ExampleEnumState.PAUSED);
67          // Second fill the associations with transitions since none exist yet
68          for (ExampleTransition trans: ExampleTransition.values()) {
69              machineState2.addNewAssociation(trans.elt);
70          }
71          
72          System.out.println("Machine1 states...");
73          changeState(machineState1, ExampleEnumState.CONFIGURING);
74          changeState(machineState1, ExampleEnumState.PAUSED);
75          changeState(machineState1, ExampleEnumState.RUNNING);
76          changeState(machineState1, ExampleEnumState.ENDED);
77          changeState(machineState1, ExampleEnumState.PAUSED);
78          changeState(machineState1, ExampleEnumState.RESET);
79          changeState(machineState1, ExampleEnumState.PAUSED);
80  
81          System.out.println("Machine2 states...");
82          changeState(machineState2, ExampleEnumState.CONFIGURING);
83          changeState(machineState2, ExampleEnumState.PAUSED);
84          changeState(machineState2, ExampleEnumState.RUNNING);
85          changeState(machineState2, ExampleEnumState.ENDED);
86          changeState(machineState2, ExampleEnumState.PAUSED);
87          changeState(machineState2, ExampleEnumState.RESET);
88          changeState(machineState2, ExampleEnumState.PAUSED);
89      }
90      static private void changeState(MachineState<ExampleEnumState> mach, 
91              ExampleEnumState desired) {
92          try {
93              printState(mach);
94              mach.setCurrent(desired);
95              printState(mach);
96          } catch (IllegalFiniteStateException e) {
97              printWrongState(mach, desired);
98          }
99      }
100     static private void printState(MachineState<ExampleEnumState> mach) {
101         System.out.println("State is "+mach.getCurrent());
102     }
103     static private void printWrongState(MachineState<ExampleEnumState> mach, ExampleEnumState desired) {
104         System.out.println("Cannot go from State "+mach.getCurrent()+" to State "+desired);
105     }
106 
107 }