| 1 | /* __copyright_begin__ |
| 2 | Copyright 2011 Dan Hagberg |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | __copyright_end__ */ |
| 16 | /** |
| 17 | * |
| 18 | */ |
| 19 | package net.digitaltsunami.tmeter.action; |
| 20 | |
| 21 | import net.digitaltsunami.tmeter.Timer; |
| 22 | |
| 23 | /** |
| 24 | * Defines an interface for possibly more complex handling of timer instances. |
| 25 | * <p> |
| 26 | * Provides for a chain of actions to be defined that will execute upon the |
| 27 | * timer being stopped. Each action should be non-destructive in that it should |
| 28 | * not change the attributes of the {@link Timer}. |
| 29 | * |
| 30 | * @author dhagberg |
| 31 | * |
| 32 | */ |
| 33 | public abstract class TimerAction { |
| 34 | protected TimerAction nextAction; |
| 35 | |
| 36 | /** |
| 37 | * Add the provided action to the chain of actions to perform. |
| 38 | * <p> |
| 39 | * No guarantee as to the order in which the actions will be executed. |
| 40 | * |
| 41 | * @param action |
| 42 | * @return provided action so that they may be chained without creating a |
| 43 | * local instance. |
| 44 | */ |
| 45 | public TimerAction addAction(TimerAction action) { |
| 46 | if (nextAction != null) { |
| 47 | // If there is already a next action, insert the new action between |
| 48 | // the two. |
| 49 | action.nextAction = nextAction; |
| 50 | } |
| 51 | nextAction = action; |
| 52 | return action; |
| 53 | } |
| 54 | |
| 55 | public final void timerComplete(Timer timer) { |
| 56 | processTimer(timer); |
| 57 | if (nextAction != null) { |
| 58 | nextAction.timerComplete(timer); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Perform any applicable processing specific to the actions. This method |
| 64 | * will be invoked when a completed timer is submitted for processing. |
| 65 | * <p> |
| 66 | * Concrete classes must override this method in order to view the submitted |
| 67 | * timers. |
| 68 | * |
| 69 | * @param timer |
| 70 | * an instance of a completed {@link Timer}. |
| 71 | */ |
| 72 | protected abstract void processTimer(Timer timer); |
| 73 | |
| 74 | /** |
| 75 | * Perform any processing specific to the action for reset. This method will |
| 76 | * be invoked to reset the state of the action and may indicate that a |
| 77 | * new timing run is starting. |
| 78 | * <p> |
| 79 | * Concrete classes should override this method if they accumulate timer |
| 80 | * data that is specific to a given run. The default is to do nothing. |
| 81 | */ |
| 82 | protected void reset() { |
| 83 | // No action |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Drives the reset processing for all {@link TimerAction} instances. |
| 88 | */ |
| 89 | public final void resetState() { |
| 90 | reset(); |
| 91 | if (nextAction != null) { |
| 92 | nextAction.reset(); |
| 93 | } |
| 94 | } |
| 95 | } |