1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| /***
* WARNING : Don't forget to call notifyFatherIExist() in a static block to display this algo in the mainView comboBox
*/
public abstract class SensorAlgoChanger implements Serializable {
private static ArrayList<Class> listSons = new ArrayList<>();
private final double MIN =-273.15;
private final double MAX = 4000;
private final static Random RANDOM = new Random();
public static int i;
public abstract double doTemperature();
public double getMin()
{
return MIN;
}
public double getMax(){
return MAX;
}
public Random getRandom(){
return RANDOM;
}
public static void addSon(Class classAlgo)
{
listSons.add(classAlgo);
}
public static ArrayList<String> getSons() {
ArrayList<String> listReturn = new ArrayList<>();
for (Class c : listSons) {
int start = c.getName().indexOf(".") + 1;
int end = c.getName().length();
listReturn.add(c.getName().substring(start, end));
}
return listReturn;
}
public static void notifyFatherIExist(Class classAlgo)
{
addSon(classAlgo);
}
}
Et chaque fils hérite de cette classe mère de la façon suivante :
public class AlgoSmallFluctuation extends SensorAlgoChanger {
private double currentTemperature;
private boolean isFirstTemperature;
private double delta;
static
{
System.out.println("salute");
notifyFatherIExist(AlgoSmallFluctuation.class);
}
public AlgoSmallFluctuation(double coef, double firstTemperature)
{
delta = coef;
currentTemperature = firstTemperature;
this.isFirstTemperature = true;
}
@Override
public double doTemperature() {
if (isFirstTemperature){
isFirstTemperature = false;
return currentTemperature;
}
currentTemperature = currentTemperature + (getRandom().nextDouble()*(delta + delta) - delta);
if (currentTemperature >= getMax())
return getMax();
if (currentTemperature <= getMin())
return getMin();
//System.out.println(previousTemperature);
return currentTemperature;
}
} |