1 package com.td.service;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Calendar;
\r
5 import java.util.Collections;
\r
6 import java.util.Comparator;
\r
7 import java.util.Date;
\r
8 import java.util.List;
\r
10 import com.td.db.Keyword;
\r
11 import com.td.db.KeywordDao;
\r
12 import com.td.db.Monitor;
\r
13 import com.td.db.MonitorDao;
\r
14 import com.td.db.Train;
\r
15 import com.td.db.TrainDao;
\r
16 import com.td.utility.DateUtil;
\r
17 import com.td.utility.Debug;
\r
19 public class Scheduler {
\r
20 private static Calendar cal = Calendar.getInstance();
\r
21 private List<Term>[] list = null;
\r
22 private List<Keyword> keywords = null;
\r
23 private List<Train> trains = null;
\r
24 private Comparator<Term> comp = new Comparator<Term>() {
\r
25 public int compare(Term t1, Term t2) {
\r
26 return t1.getFrom() - t2.getFrom();
\r
30 public Scheduler() {
\r
34 public void init() {
\r
35 Debug.d(this, "start init");
\r
39 Debug.d(this, "end init");
\r
42 public long calcSchedule(long now, int len) {
\r
43 Debug.d(this, "start");
\r
44 long t = nextSchedule(now, len);
\r
45 Debug.d(this, "end");
\r
49 public long nextSchedule(long now, int len) {
\r
50 long next = now + 60 * len * 1000;
\r
52 cal.setTimeInMillis(next);
\r
53 Date date = cal.getTime();
\r
54 int h = date.getHours();
\r
55 int m = date.getMinutes();
\r
56 int hm = DateUtil.getHm(h, m);
\r
57 int w = date.getDay();
\r
59 for (int i = 0; i < DateUtil.WEEK + 1; i++) {
\r
60 int n = (w + i) % DateUtil.WEEK;
\r
61 List<Term> l = list[n];
\r
62 int j = isContain(l, hm);
\r
63 if (i == 0 && j == 0) {
\r
64 //
\8e\9f\89ñ
\8bN
\93®
\8e\9e(next)
\82ª
\81A
\83X
\83P
\83W
\83\85\81[
\83\8b\93à
\82Å
\82 \82é
\r
68 Term t = l.get(j - 1);
\r
69 //
\8d¡
\93ú(next)
\82Ìfrom
\8e\9e(next
\82æ
\82è
\8cã
\82Ì
\8e\9e\8d\8f)
\r
70 return DateUtil.getTime(date, i, t.getFrom()).getTimeInMillis();
\r
73 //
\91¼
\82Ì
\97j
\93ú
\82Ì
\8fê
\8d\87\r
76 //
\8d¡
\93ú(next)
\82©
\82ç
\81Ai
\93ú
\8cã
\82Ì
\81Afrom
\8e\9e\r
77 return DateUtil.getTime(date, i, t.getFrom())
\r
85 public int isContain(List<Term> l, int hm) {
\r
89 for (int i = 0; i < l.size(); i++) {
\r
93 if (from <= hm && hm <= to) {
\r
103 public void makeSchedule() {
\r
104 if (list != null) {
\r
107 Debug.d(this, "update schedule");
\r
109 MonitorDao dao = MonitorDao.getInstance();
\r
110 List<Monitor> mList = dao.search();
\r
111 makeSchedule(mList);
\r
114 public void makeTrains() {
\r
115 if (trains != null) {
\r
118 Debug.d(this, "update trains");
\r
120 TrainDao dao = TrainDao.getInstance();
\r
121 trains = dao.search(true);
\r
124 public void makeKeywords() {
\r
125 if (keywords != null) {
\r
128 Debug.d(this, "update keywords");
\r
130 KeywordDao dao = KeywordDao.getInstance();
\r
131 keywords = dao.search();
\r
134 public List<Term>[] getList() {
\r
138 public List<Train> getTrains() {
\r
142 public List<Keyword> getKeywords() {
\r
146 public void makeSchedule(List<Monitor> mList) {
\r
149 for (Monitor monitor : mList) {
\r
150 add(list, monitor);
\r
154 public void clearSchedule() {
\r
155 list = new List[DateUtil.WEEK];
\r
156 for (int i = 0; i < list.length; i++) {
\r
157 list[i] = new ArrayList<Term>();
\r
161 public void add(List<Term>[] ll, Monitor monitor) {
\r
162 Term term = new Term(monitor.getFrom(), monitor.getTo());
\r
163 for (int i = 0; i < DateUtil.WEEK; i++) {
\r
164 if (monitor.getWeek(i)) {
\r
170 public void add(List<Term> l, Term term) {
\r
172 while (i < l.size()) {
\r
174 if (isContain(t, term)) {
\r
175 term = add(t, term);
\r
182 Collections.sort(l, comp);
\r
185 public boolean isContain(Term t1, Term t2) {
\r
186 int from1 = t1.getFrom();
\r
187 int to1 = t1.getTo();
\r
188 int from2 = t2.getFrom();
\r
189 int to2 = t2.getTo();
\r
191 if (from1 <= from2 && from2 <= to1) {
\r
194 if (from1 <= to2 && to2 <= to1) {
\r
197 if (from2 <= from1 && from1 <= to2) {
\r
200 if (from2 <= to1 && to1 <= to2) {
\r
206 public Term add(Term t1, Term t2) {
\r
207 int from1 = t1.getFrom();
\r
208 int to1 = t1.getTo();
\r
209 int from2 = t2.getFrom();
\r
210 int to2 = t2.getTo();
\r
212 int from = (from1 < from2) ? from1 : from2;
\r
213 int to = (to1 > to2) ? to1 : to2;
\r
214 return new Term(from, to);
\r