BrightSide Workbench Full Report + Source Code
TaskConstraints.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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 
19 package org.turro.scheduler.task.constraints;
20 
21 import java.util.Date;
22 import java.util.Objects;
23 
28 public class TaskConstraints {
29 
30  private final MonthConstraint months = new MonthConstraint();
31  private final DayOfMonthConstraint daysOfMonth = new DayOfMonthConstraint();
32  private final WeekDayConstraint weekDays = new WeekDayConstraint();
33  private final HourConstraint hours = new HourConstraint();
34  private final MinuteConstraint minutes = new MinuteConstraint();
35 
37  return daysOfMonth;
38  }
39 
41  return hours;
42  }
43 
45  return minutes;
46  }
47 
49  return months;
50  }
51 
53  return weekDays;
54  }
55 
56  public boolean isValid(Date date) {
57  if(isEmpty()) {
58  return false;
59  }
60  boolean valid = months.isValid(date);
61  if(valid) valid = daysOfMonth.isValid(date);
62  if(valid) valid = weekDays.isValid(date);
63  if(valid) valid = hours.isValid(date);
64  if(valid) valid = minutes.isValid(date);
65  return valid;
66  }
67 
68  public boolean needsRefresh() {
69  if(isEmpty()) {
70  return false;
71  }
72  boolean refresh = months.needsRefresh();
73  if(!refresh) refresh = daysOfMonth.needsRefresh();
74  if(!refresh) refresh = weekDays.needsRefresh();
75  if(!refresh) refresh = hours.needsRefresh();
76  if(!refresh) refresh = minutes.needsRefresh();
77  return refresh;
78  }
79 
80  public boolean isEmpty() {
81  return months.isEmpty() && daysOfMonth.isEmpty() &&
82  weekDays.isEmpty() && hours.isEmpty() && minutes.isEmpty();
83  }
84 
85  /* Utils */
86 
87  @Override
88  public int hashCode() {
89  int hash = 5;
90  hash = 37 * hash + Objects.hashCode(this.months);
91  hash = 37 * hash + Objects.hashCode(this.daysOfMonth);
92  hash = 37 * hash + Objects.hashCode(this.weekDays);
93  hash = 37 * hash + Objects.hashCode(this.hours);
94  hash = 37 * hash + Objects.hashCode(this.minutes);
95  return hash;
96  }
97 
98  @Override
99  public boolean equals(Object obj) {
100  if (this == obj) {
101  return true;
102  }
103  if (obj == null) {
104  return false;
105  }
106  if (getClass() != obj.getClass()) {
107  return false;
108  }
109  final TaskConstraints other = (TaskConstraints) obj;
110  if (!Objects.equals(this.months, other.months)) {
111  return false;
112  }
113  if (!Objects.equals(this.daysOfMonth, other.daysOfMonth)) {
114  return false;
115  }
116  if (!Objects.equals(this.weekDays, other.weekDays)) {
117  return false;
118  }
119  if (!Objects.equals(this.hours, other.hours)) {
120  return false;
121  }
122  return Objects.equals(this.minutes, other.minutes);
123  }
124 
125 }