BrightSide Workbench Full Report + Source Code
TaskSettings.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.settings;
20 
21 import java.util.Date;
22 import java.util.Objects;
23 import org.turro.elephant.context.IConstructor;
24 import org.turro.reflection.Reflections;
25 import org.turro.scheduler.task.AbstractTask;
26 import org.turro.scheduler.task.constraints.TaskConstraints;
27 import org.turro.util.Comparison;
28 
33 public class TaskSettings implements Comparable<TaskSettings> {
34 
35  private boolean active;
36  private String implementation, description, data;
37  private Date startDate, endDate;
38  private TaskConstraints constraints = new TaskConstraints();
39 
40  public boolean isActive() {
41  return active;
42  }
43 
44  public void setActive(boolean active) {
45  this.active = active;
46  }
47 
48  public String getImplementation() {
49  return implementation;
50  }
51 
52  public void setImplementation(String implementation) {
53  this.implementation = implementation;
54  }
55 
56  public String getDescription() {
57  return description;
58  }
59 
60  public void setDescription(String description) {
61  this.description = description;
62  }
63 
64  public String getData() {
65  return data;
66  }
67 
68  public void setData(String data) {
69  this.data = data;
70  }
71 
72  public Date getStartDate() {
73  return startDate;
74  }
75 
76  public void setStartDate(Date startDate) {
77  this.startDate = startDate;
78  }
79 
80  public Date getEndDate() {
81  return endDate;
82  }
83 
84  public void setEndDate(Date endDate) {
85  this.endDate = endDate;
86  }
87 
89  return constraints;
90  }
91 
92  public void setConstraints(TaskConstraints constraints) {
93  this.constraints = constraints;
94  }
95 
96  public boolean shouldRun(Date now) {
97  return (startDate == null || now.after(startDate)) &&
98  (endDate == null || now.before(endDate)) &&
99  getConstraints().isValid(now);
100  }
101 
102  /* Factory */
103 
104  public static TaskSettings from(AbstractTask task) {
105  TaskSettings settings = new TaskSettings();
106  settings.setImplementation(task.getClass().getName());
107  return settings;
108  }
109 
110  public AbstractTask build(IConstructor constructor) {
111  AbstractTask task = (AbstractTask) Reflections.of(getImplementation()).instance();
112  task.setConstructor(constructor);
113  task.setSettings(this);
114  return task;
115  }
116 
117  /* Comparable */
118 
119  @Override
120  public int compareTo(TaskSettings o) {
121  return Comparison.ascendant()
122  .compare(getImplementation(), o.getImplementation())
123  .compare(getDescription(), o.getDescription())
124  .compare(getData(), o.getData())
125  .compare(getConstraints().hashCode(), o.getConstraints().hashCode())
126  .get();
127  }
128 
129  /* Utils */
130 
131  @Override
132  public int hashCode() {
133  int hash = 3;
134  hash = 23 * hash + (this.active ? 1 : 0);
135  hash = 23 * hash + Objects.hashCode(this.implementation);
136  hash = 23 * hash + Objects.hashCode(this.description);
137  hash = 23 * hash + Objects.hashCode(this.data);
138  hash = 23 * hash + Objects.hashCode(this.startDate);
139  hash = 23 * hash + Objects.hashCode(this.endDate);
140  hash = 23 * hash + Objects.hashCode(this.constraints);
141  return hash;
142  }
143 
144  @Override
145  public boolean equals(Object obj) {
146  if (this == obj) {
147  return true;
148  }
149  if (obj == null) {
150  return false;
151  }
152  if (getClass() != obj.getClass()) {
153  return false;
154  }
155  final TaskSettings other = (TaskSettings) obj;
156  if (this.active != other.active) {
157  return false;
158  }
159  if (!Objects.equals(this.implementation, other.implementation)) {
160  return false;
161  }
162  if (!Objects.equals(this.description, other.description)) {
163  return false;
164  }
165  if (!Objects.equals(this.data, other.data)) {
166  return false;
167  }
168  if (!Objects.equals(this.startDate, other.startDate)) {
169  return false;
170  }
171  if (!Objects.equals(this.endDate, other.endDate)) {
172  return false;
173  }
174  return Objects.equals(this.constraints, other.constraints);
175  }
176 
177 }
static TaskSettings from(AbstractTask task)
AbstractTask build(IConstructor constructor)
void setConstraints(TaskConstraints constraints)
void setImplementation(String implementation)