BrightSide Workbench Full Report + Source Code
constraints/MonthConstraint.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.EnumSet;
23 import java.util.HashSet;
24 import java.util.List;
25 import org.jdom.Element;
26 
31 @Deprecated
32 public class MonthConstraint extends HashSet<Month> {
33 
34  private Month lastCheck;
35  private boolean refresh;
36 
37  public void readXML(Element root) {
38  Element parent = root.getChild("month-constraint");
39  for(Element el : (List<Element>) parent.getChildren("month")) {
40  add(Month.valueOf(el.getAttributeValue("name")));
41  }
42  }
43 
44  public void writeXML(Element root) {
45  Element el = new Element("month-constraint");
46  root.addContent(el);
47  for(Month m : this) {
48  Element e = new Element("month");
49  e.setAttribute("name", m.toString());
50  el.addContent(e);
51  }
52  }
53 
55  addAll(EnumSet.allOf(Month.class));
56  return this;
57  }
58 
59  public boolean isValid(Date date) {
60  refresh = false;
61  Month m = getMonth(date);
62  if(m != null && contains(m)) {
63  refresh = !m.equals(lastCheck);
64  lastCheck = m;
65  return true;
66  }
67  lastCheck = m;
68  return isEmpty();
69  }
70 
71  public boolean needsRefresh() {
72  return refresh;
73  }
74 
75 
76  public Month getMonth(Date date) {
77  Calendar c = Calendar.getInstance();
78  c.setTime(date);
79  switch(c.get(Calendar.MONTH)) {
80  case Calendar.JANUARY:
81  return Month.JANUARY;
82  case Calendar.FEBRUARY:
83  return Month.FEBRUARY;
84  case Calendar.MARCH:
85  return Month.MARCH;
86  case Calendar.APRIL:
87  return Month.APRIL;
88  case Calendar.MAY:
89  return Month.MAY;
90  case Calendar.JUNE:
91  return Month.JUNE;
92  case Calendar.JULY:
93  return Month.JULY;
94  case Calendar.AUGUST:
95  return Month.AUGUST;
96  case Calendar.SEPTEMBER:
97  return Month.SEPTEMBER;
98  case Calendar.OCTOBER:
99  return Month.OCTOBER;
100  case Calendar.NOVEMBER:
101  return Month.NOVEMBER;
102  case Calendar.DECEMBER:
103  return Month.DECEMBER;
104  }
105  return null;
106  }
107 
108 }