BrightSide Workbench Full Report + Source Code
Phase.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.phase;
20 
21 import java.util.Date;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
24 
29 public abstract class Phase {
30 
32  protected boolean autoChanging = false, changed = false;
33 
35  this.current = current;
36  }
37 
39  return current;
40  }
41 
42  public boolean isAutoChanging() {
43  return autoChanging;
44  }
45 
46  public boolean isChanged() {
47  return changed;
48  }
49 
50  public synchronized void checkStatus(Date now) {
51  PhaseDefinition newPhase = doCheckPhase(now);
52  if(current == null || (!newPhase.equals(current) && canChangeTo(newPhase))) {
53  autoChanging = true;
54  try {
55  onChanging(newPhase);
56  newPhase = doChangeTo(newPhase);
57  onChanged(newPhase);
58  current = newPhase;
59  } finally {
60  autoChanging = false;
61  }
62  }
63  }
64 
65  public synchronized void changeTo(PhaseDefinition newPhase) {
67  onChanging(newPhase);
68  current = doChangeTo(newPhase);
69  changed = !current.equals(tmp);
71  }
72 
73  protected abstract PhaseDefinition doCheckPhase(Date now);
74  protected abstract PhaseDefinition doChangeTo(PhaseDefinition newPhase);
75 
76  public boolean canChangeTo(PhaseDefinition newPhase) {
77  return !current.equals(newPhase);
78  }
79 
80  public String getName() {
81  return current.toString();
82  }
83 
84  protected void onChanging(PhaseDefinition newStatus) {}
85  protected void onChanged(PhaseDefinition newStatus) {}
86 
87  protected SortedSet<PhaseDefinition> allPhases() {
88  return PhaseDefinitions.instance();
89  }
90 
91  public SortedSet<PhaseDefinition> getPossiblePhases() {
92  SortedSet<PhaseDefinition> possible = new TreeSet<>();
93  for(PhaseDefinition phase : allPhases()) {
94  if(canChangeTo(phase)) {
95  possible.add(phase);
96  }
97  }
98  return possible;
99  }
100 
101 }
static PhaseDefinitions instance()
void onChanged(PhaseDefinition newStatus)
Definition: Phase.java:85
boolean isAutoChanging()
Definition: Phase.java:42
PhaseDefinition getCurrent()
Definition: Phase.java:38
Phase(PhaseDefinition current)
Definition: Phase.java:34
SortedSet< PhaseDefinition > getPossiblePhases()
Definition: Phase.java:91
synchronized void changeTo(PhaseDefinition newPhase)
Definition: Phase.java:65
abstract PhaseDefinition doCheckPhase(Date now)
boolean canChangeTo(PhaseDefinition newPhase)
Definition: Phase.java:76
String getName()
Definition: Phase.java:80
PhaseDefinition current
Definition: Phase.java:31
boolean isChanged()
Definition: Phase.java:46
synchronized void checkStatus(Date now)
Definition: Phase.java:50
boolean autoChanging
Definition: Phase.java:32
SortedSet< PhaseDefinition > allPhases()
Definition: Phase.java:87
abstract PhaseDefinition doChangeTo(PhaseDefinition newPhase)
void onChanging(PhaseDefinition newStatus)
Definition: Phase.java:84