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