View Javadoc

1   package cz.cuni.amis.utils;
2   
3   import java.util.Calendar;
4   import java.util.Date;
5   import java.util.Timer;
6   import java.util.TimerTask;
7   
8   public class Run {
9   		
10  	public static TimerTaskExt once(long delay, Runnable runnable) {
11  		return getInstance().runOnce(delay, runnable);
12  	}
13  	
14  	public static enum TimerTaskState {
15  		SCHEDULED,
16  		RUNNING,
17  		SUCCESS,
18  		EXCEPTION,
19  		CANCELLED
20  	}
21  
22  	public static class TimerTaskExt extends TimerTask {
23  		
24  		private String name;
25  		
26  		private Runnable runnable;
27  		
28  		private TimerTaskState state = TimerTaskState.SCHEDULED;
29  		
30  		private Throwable exception;
31  			
32  		public TimerTaskExt(String name, Runnable runnable) {
33  			this.runnable = runnable;
34  		}
35  		
36  		public String getName() {
37  			return name;
38  		}
39  
40  		@Override
41  		public void run() {
42  			try {
43  				state = TimerTaskState.RUNNING;
44  				runnable.run();
45  			} catch (Exception e) {
46  				exception = e;
47  				state = TimerTaskState.EXCEPTION;
48  				return;
49  			}
50  			state = TimerTaskState.SUCCESS;
51  		}
52  		
53  		public TimerTaskState getState() {
54  			return state;
55  		}
56  		
57  		public Throwable getException() {
58  			return exception;
59  		}
60  		
61  		public boolean isState(TimerTaskState state) {
62  			return this.state == state;
63  		}
64  		
65  		public boolean isScheduled() {
66  			return state == TimerTaskState.SCHEDULED;
67  		}
68  		
69  		public boolean isRunning() {
70  			return state == TimerTaskState.RUNNING;
71  		}
72  		
73  		public boolean isSuccess() {
74  			return state == TimerTaskState.SUCCESS;
75  		}
76  		
77  		public boolean isException() {
78  			return state == TimerTaskState.EXCEPTION;
79  		}
80  		
81  		public boolean isCancelled() {
82  			return state == TimerTaskState.CANCELLED;
83  		}
84  		
85  		@Override
86  		public boolean cancel() {
87  			if (super.cancel()) {
88  				state = TimerTaskState.CANCELLED;
89  				return true;
90  			}
91  			return false;
92  		}
93  	}
94  		
95  	public static Run getInstance() {
96  		if (instance != null) return instance;
97  		synchronized(mutex) {
98  			if (instance != null) return instance;
99  			instance = new Run();
100 		}
101 		return instance;
102 	}
103 	
104 	private static Object mutex = new Object();
105 	
106 	private static Run instance = null;	
107 	
108 	// ==============
109 	// IMPLEMENTATION
110 	// ==============
111 	
112 	private Calendar calendar = Calendar.getInstance();
113 	
114 	private Timer timer = new Timer("PogamutRunDeamon", true);
115 	
116 	public Date getFutureDate(long millis) {
117 		Calendar calendar = Calendar.getInstance();
118 		calendar.setTimeInMillis(System.currentTimeMillis());
119 		calendar.add(Calendar.MILLISECOND, (int)millis);
120 		return calendar.getTime();
121 	}
122 	
123 	public TimerTaskExt runOnce(long delay, Runnable runnable) {
124 		TimerTaskExt task = new TimerTaskExt("Once", runnable);
125 		timer.schedule(task, delay);
126 		return task;
127 	}
128 
129 }