BrightSide Workbench Full Report + Source Code
constraints/MinuteConstraint.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 MinuteConstraint extends HashSet<Integer> {
32 
33  private Integer lastCheck;
34  private boolean refresh;
35 
36  public void readXML(Element root) {
37  Element parent = root.getChild("minute-constraint");
38  for(Element el : (List<Element>) parent.getChildren("minute")) {
39  add(Integer.valueOf(el.getAttributeValue("name")));
40  }
41  }
42 
43  public void writeXML(Element root) {
44  Element el = new Element("minute-constraint");
45  root.addContent(el);
46  for(Integer i : this) {
47  Element e = new Element("minute");
48  e.setAttribute("name", i + "");
49  el.addContent(e);
50  }
51  }
52 
54  for(int i = 0; i < 59; i++) {
55  add(i);
56  }
57  return this;
58  }
59 
60  public MinuteConstraint setEvery(int every) {
61  for(int i = 0; i < 59; i += every) {
62  add(i);
63  }
64  return this;
65  }
66 
67  public boolean isValid(Date date) {
68  refresh = false;
69  Integer m = getMinute(date);
70  if(m != null && contains(m)) {
71  refresh = !m.equals(lastCheck);
72  lastCheck = m;
73  return true;
74  }
75  lastCheck = m;
76  return isEmpty();
77  }
78 
79  public boolean needsRefresh() {
80  return refresh;
81  }
82 
83  public Integer getMinute(Date date) {
84  Calendar c = Calendar.getInstance();
85  c.setTime(date);
86  return c.get(Calendar.MINUTE);
87  }
88 
89 }