Repository navigation
Expand file tree
/
Copy pathTestClock.java
More file actions
136 lines (126 loc) · 4.17 KB
/
Copy pathTestClock.java
File metadata and controls
136 lines (126 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.utils.concurrency;
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;
/**
* A test clock which is also a TimerTask. The task calls a Scheduler's poll method
*
*/
public class TestClock extends TimerTask {
private int _minute = 0;
private int _hour = 0;
private int _day = 0;
private int _week = 0;
private int _month = 0;
private int _year = 0;
private Calendar _cal = null;
private final int _minutesPerHour;
private final int _hoursPerDay;
private final int _daysPerWeek;
private final int _daysPerMonth;
private final int _weeksPerMonth;
private final int _monthsPerYear;
private final Scheduler _scheduler;
public TestClock(Scheduler scheduler, int minutesPerHour, int hoursPerDay, int daysPerWeek, int daysPerMonth, int weeksPerMonth, int monthsPerYear) {
_minutesPerHour = minutesPerHour;
_hoursPerDay = hoursPerDay;
_daysPerWeek = daysPerWeek;
_daysPerMonth = daysPerMonth;
_weeksPerMonth = weeksPerMonth;
_monthsPerYear = monthsPerYear;
_cal = Calendar.getInstance();
_year = _cal.get(Calendar.YEAR);
_month = _cal.get(Calendar.MONTH);
_day = _cal.get(Calendar.DAY_OF_MONTH);
_week = _cal.get(Calendar.WEEK_OF_MONTH);
_hour = _cal.get(Calendar.HOUR_OF_DAY);
_minute = _cal.get(Calendar.MINUTE);
_scheduler = scheduler;
}
public int getMinute() {
return _minute;
}
public int getHour() {
return _hour;
}
public int getDay() {
return _day;
}
public int getWeek() {
return _week;
}
public int getMonth() {
return _month;
}
public int getYear() {
return _year;
}
public int getMinutesPerHour() {
return _minutesPerHour;
}
public int getHoursPerDay() {
return _hoursPerDay;
}
public int getDaysPerMonth() {
return _daysPerMonth;
}
public int getDaysPerWeek() {
return _daysPerWeek;
}
public int getWeeksPerMonth() {
return _weeksPerMonth;
}
public int getMonthsPerYear() {
return _monthsPerYear;
}
@Override
public void run() {
synchronized(this) {
_minute++;
if ((_minute > 0) && ((_minute % _minutesPerHour) == 0)) {
_minute = 0;
_hour++;
}
if ((_hour > 0) && ((_hour % _hoursPerDay) == 0)) {
_hour = 0;
_day++;
}
if ((_day > 0) && ((_day % _daysPerWeek) == 0)) {
_week++;
}
if ((_day > 0) && ((_day % _daysPerMonth) == 0)) {
_day = 0;
_week = 0;
_month++;
}
if ((_month > 0) && ((_month % _monthsPerYear) == 0)) {
_month = 0;
_year++;
}
if (_scheduler != null) {
// XXX: Creating new date is hugely inefficient for every minute.
// Later the time in the database will be changed to currentTimeInMillis.
// Then we can use System.getCurrentTimeInMillis() which is damn cheap.
_cal.set(_year, _month, _day, _hour, _minute);
Date currentTimestamp = _cal.getTime();
_scheduler.poll(currentTimestamp);
}
}
}
}