BrightSide Workbench Full Report + Source Code
constraints/HourConstraint.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.scheduler.constraints;
19 
20 import java.util.Calendar;
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.List;
24 import org.jdom.Element;
25 
30 @Deprecated
31 public class HourConstraint extends HashSet<Integer> {
32 
33  private Integer lastCheck;
34  private boolean refresh;
35 
36  public void readXML(Element root) {
37  Element parent = root.getChild("hour-constraint");
38  for(Element el : (List<Element>) parent.getChildren("hour")) {
39  add(Integer.valueOf(el.getAttributeValue("name")));
40  }
41  }
42 
43  public void writeXML(Element root) {
44  Element el = new Element("hour-constraint");
45  root.addContent(el);
46  for(Integer i : this) {
47  Element e = new Element("hour");
48  e.setAttribute("name", i + "");
49  el.addContent(e);
50  }
51  }
52 
54  for(int i = 0; i < 24; i++) {
55  add(i);
56  }
57  return this;
58  }
59 
61  for(int i = 0; i < 12; i++) {
62  add(i);
63  }
64  return this;
65  }
66 
68  for(int i = 12; i < 24; i++) {
69  add(i);
70  }
71  return this;
72  }
73 
74  public boolean isValid(Date date) {
75  refresh = false;
76  Integer h = getHour(date);
77  if(h != null && contains(h)) {
78  refresh = !h.equals(lastCheck);
79  lastCheck = h;
80  return true;
81  }
82  lastCheck = h;
83  return isEmpty();
84  }
85 
86  public boolean needsRefresh() {
87  return refresh;
88  }
89 
90  public Integer getHour(Date date) {
91  Calendar c = Calendar.getInstance();
92  c.setTime(date);
93  return c.get(Calendar.HOUR_OF_DAY);
94  }
95 
96 }