View Javadoc

1   package net.sourceforge.basher;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   /**
7    * Container class for holding task configuration options and manipulating certain aspects of it.
8    *
9    * @author Johan Lindquist
10   */
11  public class TaskConfiguration
12  {
13      /**
14       * Instance weight as a number between 0 and 100 - 0 indicating the task would not run, 100 that it always runs.
15       * Defaults to 100 (always runs).
16       */
17      private int _weight = 100;
18  
19      /**
20       * instance mirror of weight as the "real" float.  This is to avoid stomping on the same number.
21       */
22      private double _doubleWeight = _weight;
23  
24      /**
25       * Instance backup of the weight - kept for reset
26       */
27      private int _originalWeight = _weight;
28  
29      /**
30       * instance inertia
31       */
32      private double _inertia = 1.0d;
33  
34      /**
35       * maximum invocation - if non-zero, this task should execute at most _maxInvocations times
36       */
37      private int _maxInvocations = 0;
38  
39      /**
40       * Defines the time (internal Basher time) at which point the task will start running.  Defaults to 0
41       * (start initially).
42       */
43      private long _runFrom = 0;
44  
45      /**
46       * Defines the time (internal Basher time) at which point the task will stop running.  Defaults to
47       * <code>-1</code> (run forever).
48       */
49      private long _stopAfter = Long.MAX_VALUE;
50  
51      /**
52       * Defines the maximum amount of time allowed for a task to run.  If the task takes longer than the specified
53       * time, the invocation will be considered a failure.  Defaults to 0 (no max time)
54       */
55      private int _maxTime = 0;
56  
57      /**
58       * Defines the registered task name
59       */
60      private String _taskName;
61  
62      /**
63       * Defines the phases for which the task should be available in.  By default, a task is always available in the
64       * <code>RUN</code> phase.
65       */
66      private List<Phase> _applicablePhases = new ArrayList<Phase>();
67  
68      public TaskConfiguration()
69      {
70          _applicablePhases.add( Phase.RUN );
71      }
72  
73      /**
74       * Retrieves the weight of this <code>AbstractTask</code>.  This could be used by certain schedulers to allow for
75       * more fine grained control of task scheduling
76       *
77       * @return A value between 1 and 100 (higher signals a wish to run more frequently)
78       */
79      public Integer getWeight()
80      {
81          return _weight;
82      }
83  
84      /**
85       * Retrieves the inertia of this <code>TaskConfiguration</code>.
86       * See {@link net.sourceforge.basher.TaskConfiguration#setInertia(Double)}.
87       *
88       * @return The rate of change for this instance.
89       */
90      public Double getInertia()
91      {
92          return _inertia;
93      }
94  
95      /**
96       * Sets the inertia of this <code>AbstractTask</code>.  This is used to control the change of
97       * the weight assocaited with this task, calculated for each invocation of the task (if it is
98       * not 1.0).
99       *
100      * @param inertia The inertia to give this instance.  This should be a value between 0.1 and 2.0.
101      */
102     public void setInertia( final Double inertia )
103     {
104         _inertia = inertia;
105     }
106 
107     /**
108      * Resets the weight calculattions of this <code>TaskConfiguration</code> instance.
109      */
110     public void reset()
111     {
112         _weight = _originalWeight;
113         _doubleWeight = _weight;
114     }
115 
116     /**
117      * Sets the weight for the task.  This <i>could</i> be used by a scheduler to control the invocations of the
118      * <code>Task</code>.
119      *
120      * @param weight The weight of the task.  Should be a value between 1 and 100.
121      */
122     public void setWeight( final Integer weight )
123     {
124         _weight = weight;
125         // Mirror the weight in the float calculations too ...
126         _doubleWeight = _weight;
127 
128         // And back these settings up ...
129         _originalWeight = weight;
130         _doubleWeight = weight;
131 
132     }
133 
134     /**
135      * Sets the maximum number of times the task should be invoked.  See
136      * {@link net.sourceforge.basher.TaskConfiguration#getMaxInvocations()} for more information.
137      *
138      * @param maxInvocations The number of invocations to do.  A value of 0 (zero) or less indicates that the task
139      *                       should always be invoked.
140      */
141     public void setMaxInvocations( final Integer maxInvocations )
142     {
143         _maxInvocations = maxInvocations;
144     }
145 
146     /**
147      * Retrieves the maximum number of times the task should be invoked.  The number returned <i>could</i> be used
148      * by a scheduler to control the invocation of the <code>Task</code>.
149      *
150      * @return The maximum number of invocations
151      */
152     public Integer getMaxInvocations()
153     {
154         return _maxInvocations;
155     }
156 
157 
158     /**
159      * Recalculates the weight of this <code>Task</code>.  This will take the inertia and multiply it with the current
160      * weight and cast the result to an integer.
161      */
162     public void reCalculateWeight()
163     {
164         // Only do anything if
165         if ( Double.compare( _inertia, 1.0D ) != 0 )
166         {
167             // This ensure we dont stand stompin' on the same number
168             // if the inertia is small ...
169             _doubleWeight = _doubleWeight * _inertia;
170             _weight = ( int ) _doubleWeight;
171         }
172     }
173 
174     /**
175      * Retrieves the default time from which this task should run.
176      *
177      * @return The time from which this task should run.  0 indicates run from start, otherwise specifies the seconds from
178      *         start of the run phase.
179      */
180     public Long getRunFrom()
181     {
182         return _runFrom;
183     }
184 
185     /**
186      * Retrieves the default time from which this task should stop running.
187      *
188      * @return The time from which this task should stop running.  Always returns {@link java.lang.Long#MAX_VALUE}
189      *         (run forever) unless overridden by sub-class.
190      */
191     public Long getStopAfter()
192     {
193         return _stopAfter;
194     }
195 
196     /**
197      * Sets the time from which this task should run.
198      *
199      * @param runFrom The time from which this task should run.  0 indicates run from start, otherwise specifies the seconds from
200      *                start of the run phase.
201      */
202     public void setRunFrom( final Long runFrom )
203     {
204         _runFrom = runFrom;
205     }
206 
207     /**
208      * Sets the default time from which this task should stop running.
209      *
210      * @param stopAfter The time from which this task should stop running.  Always returns {@link java.lang.Long#MAX_VALUE}
211      *                  (run forever) unless overridden by sub-class.
212      */
213     public void setStopAfter( final Long stopAfter )
214     {
215         _stopAfter = stopAfter;
216     }
217 
218 
219     /**
220      * Clears the list of applicable phases.
221      */
222     public void clearApplicablePhases()
223     {
224         _applicablePhases.clear();
225     }
226 
227     /**
228      * Adds the specified phase to the set of applicable phases.  This method will ensure there are no duplicate entries.
229      *
230      * @param phase The phases to add
231      */
232     public void addApplicablePhase( Phase phase )
233     {
234         if ( phase == null )
235         {
236             throw new NullPointerException( "phase" );
237         }
238         if ( !_applicablePhases.contains( phase ) )
239         {
240             _applicablePhases.add( phase );
241         }
242     }
243 
244     /**
245      * Retrieves the <code>Phase</code>s in which this <code>Task</code> is run.  This, by default, returns only
246      * <code>Phase.RUN</code>.
247      *
248      * @return List of phases this <code>Task</code> should run in - only RUN phase is included by default
249      */
250     public List<Phase> getApplicablePhases()
251     {
252         return _applicablePhases;
253     }
254 
255     /**
256      * Returns the default max time for <code>Task</code>s.
257      *
258      * @return Always returns -1, signifying no maximum time
259      */
260     public Integer getMaxTime()
261     {
262         return _maxTime;
263     }
264 
265     public void setMaxTime( final Integer maxTime )
266     {
267         _maxTime = maxTime;
268     }
269 
270     /**
271      * Retrieves the name that the <code>Task</code> instance should be known as.
272      *
273      * @return The name of the task instance.
274      */
275     public String getTaskName()
276     {
277         return _taskName;
278     }
279 
280     /**
281      * Sets the name that the <code>Task</code> instance should be known as.
282      *
283      * @param taskName The name of the instance.
284      */
285     public void setTaskName( final String taskName )
286     {
287         _taskName = taskName;
288     }
289 
290     /**
291      * @param taskConfiguration
292      */
293     public void initialize( final TaskConfiguration taskConfiguration )
294     {
295         _applicablePhases = new ArrayList<Phase>( taskConfiguration.getApplicablePhases() );
296         _inertia = taskConfiguration.getInertia();
297         _maxInvocations = taskConfiguration.getMaxInvocations();
298         _maxTime = taskConfiguration.getMaxTime();
299         _runFrom = taskConfiguration.getRunFrom();
300         _stopAfter = taskConfiguration.getStopAfter();
301         _taskName = taskConfiguration.getTaskName();
302         _weight = taskConfiguration.getWeight();
303     }
304 
305 
306     @Override
307     public boolean equals( final Object o )
308     {
309         if ( this == o )
310         {
311             return true;
312         }
313         if ( !( o instanceof TaskConfiguration ) )
314         {
315             return false;
316         }
317 
318         final TaskConfiguration that = ( TaskConfiguration ) o;
319 
320         if ( Double.compare( that._doubleWeight, _doubleWeight ) != 0 )
321         {
322             return false;
323         }
324         if ( Double.compare( that._inertia, _inertia ) != 0 )
325         {
326             return false;
327         }
328         if ( _maxInvocations != that._maxInvocations )
329         {
330             return false;
331         }
332         if ( _maxTime != that._maxTime )
333         {
334             return false;
335         }
336         if ( _originalWeight != that._originalWeight )
337         {
338             return false;
339         }
340         if ( _runFrom != that._runFrom )
341         {
342             return false;
343         }
344         if ( _stopAfter != that._stopAfter )
345         {
346             return false;
347         }
348         if ( _weight != that._weight )
349         {
350             return false;
351         }
352         if ( _taskName != null ? !_taskName.equals( that._taskName ) : that._taskName != null )
353         {
354             return false;
355         }
356 
357         return true;
358     }
359 
360     @Override
361     public int hashCode()
362     {
363         int result;
364         long temp;
365         result = _weight;
366         temp = _doubleWeight != +0.0d ? Double.doubleToLongBits( _doubleWeight ) : 0L;
367         result = 31 * result + ( int ) ( temp ^ ( temp >>> 32 ) );
368         result = 31 * result + _originalWeight;
369         temp = _inertia != +0.0d ? Double.doubleToLongBits( _inertia ) : 0L;
370         result = 31 * result + ( int ) ( temp ^ ( temp >>> 32 ) );
371         result = 31 * result + _maxInvocations;
372         result = 31 * result + ( int ) ( _runFrom ^ ( _runFrom >>> 32 ) );
373         result = 31 * result + ( int ) ( _stopAfter ^ ( _stopAfter >>> 32 ) );
374         result = 31 * result + _maxTime;
375         result = 31 * result + ( _taskName != null ? _taskName.hashCode() : 0 );
376         result = 31 * result + ( _applicablePhases != null ? _applicablePhases.hashCode() : 0 );
377         return result;
378     }
379 
380     @Override
381     public String toString()
382     {
383         return "TaskConfiguration{" +
384                 "_weight=" + _weight +
385                 ", _doubleWeight=" + _doubleWeight +
386                 ", _originalWeight=" + _originalWeight +
387                 ", _inertia=" + _inertia +
388                 ", _maxInvocations=" + _maxInvocations +
389                 ", _runFrom=" + _runFrom +
390                 ", _stopAfter=" + _stopAfter +
391                 ", _maxTime=" + _maxTime +
392                 ", _taskName='" + _taskName + '\'' +
393                 '}';
394     }
395 }