View Javadoc

1   package com.flexiblewebsolutions.xdriveunit;
2   
3   import java.util.Random;
4   
5   /***
6    * Sets a delay for any action to be performed.
7    * 
8    * @author Donavon Buss
9    */
10  public class DelayTimer {
11  
12  	private boolean _RandomDelay = false;
13  
14  	private int _RandomMax = 6;
15  
16  	private int _RandomMin = 1;
17  
18  	private long _Delay = 0;
19  
20  	private Random _RandomNum = null;
21  
22  	/***
23  	 * Sets the delay time to use a random value
24  	 * @param pVal True value set random delay mode
25  	 */
26  	public void setRandomDelay(boolean pVal) {
27  		setRandomDelay(pVal, 1, 6);
28  	}
29  
30  	/***
31  	 * Sets the delay time to use a random value within a range
32  	 * @param pVal True value set random delay mode 
33  	 * @param pMin Minimum value for delay
34  	 * @param pMax Maximum value for delay
35  	 */
36  	public void setRandomDelay(boolean pVal, int pMin, int pMax) {
37  		_RandomDelay = pVal;
38  		_RandomMin = pMin;
39  		_RandomMax = pMax;
40  		_Delay = -1;
41  		_RandomNum = new Random(System.currentTimeMillis());
42  	}
43  
44  	/***
45  	 * Returns whether the delay is random
46  	 * @return true if random delay
47  	 */
48  	public boolean getRandomDelay() {
49  		return (_RandomDelay);
50  	}
51  
52  	public DelayTimer() {
53  		super();
54  		// use default delay time of 0 seconds
55  	}
56  
57  	/***
58  	 * Suspends operation of thread for a specified amount of time.
59  	 *
60  	 */
61  	public void clickDelay() {
62  
63  		if (_Delay == 0) {
64  			return;
65  		}
66  
67  		long delayMillis = 0;
68  		// generate random delay time
69  		if (_RandomDelay) {
70  			int seconds = _RandomNum.nextInt(_RandomMax) + _RandomMin;
71  			delayMillis = seconds * 1000;
72  		} else {
73  			delayMillis = _Delay;
74  		}
75  
76  		// wait the specified delay
77  		try {
78  			Thread.sleep(delayMillis);
79  		} catch (InterruptedException e) {
80  			e.printStackTrace();
81  		}
82  	}
83  
84  	/***
85  	 * The number of milliseconds set as the delay
86  	 * @return millisecond delay
87  	 */
88  	public long getDelay() {
89  		return _Delay;
90  	}
91  
92  	/***
93  	 * Set number of seconds to delay for non-random mode
94  	 * @param Current seconds setting
95  	 */
96  	public void setDelaySeconds(int pSeconds) {
97  		setRandomDelay(false);
98  		_Delay = 1000 * pSeconds;
99  	}
100 
101 	/***
102 	 * Returns max possible value for random generation
103 	 * @return Maximum possible seconds
104 	 */
105 	public int getRandomMax() {
106 		return _RandomMax;
107 	}
108 
109 	/***
110 	 * Sets the max possible value for random generation
111 	 * @param Maximum possible seconds.
112 	 */
113 	public void setRandomMax(int randomMax) {
114 		_RandomMax = randomMax;
115 	}
116 
117 	/***
118 	 * Returns min possible value for random generation
119 	 * @return Minimum possible seconds
120 	 */
121 	public int getRandomMin() {
122 		return _RandomMin;
123 	}
124 
125 	/***
126 	 * Sets the minimum possible value for random generation
127 	 * @param Minimum possible seconds
128 	 */
129 	public void setRandomMin(int randomMin) {
130 		_RandomMin = randomMin;
131 	}
132 }