|
|
@@ -12,7 +12,6 @@ import java.util.HashSet;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
import java.util.SortedSet;
|
|
|
|
import java.util.SortedSet;
|
|
|
|
import java.util.TreeSet;
|
|
|
|
import java.util.TreeSet;
|
|
|
|
import java.util.Vector;
|
|
|
|
import java.util.Vector;
|
|
|
@@ -49,8 +48,6 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|
|
|
private static final String CAR_ID = "carId";
|
|
|
|
private static final String CAR_ID = "carId";
|
|
|
|
public static final String LOCO_ID = "locoId";
|
|
|
|
public static final String LOCO_ID = "locoId";
|
|
|
|
private static final String TRACE = "trace";
|
|
|
|
private static final String TRACE = "trace";
|
|
|
|
private static final HashMap<Id, Train> trains = new HashMap<>();
|
|
|
|
|
|
|
|
public static final String ID = "id";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String NAME = "name";
|
|
|
|
private static final String NAME = "name";
|
|
|
|
private String name = null;
|
|
|
|
private String name = null;
|
|
|
@@ -118,17 +115,6 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Train(Locomotive loco) {
|
|
|
|
|
|
|
|
this(loco,null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Train(Locomotive loco, Id id) {
|
|
|
|
|
|
|
|
if (isNull(id)) id = new Id();
|
|
|
|
|
|
|
|
this.id = id;
|
|
|
|
|
|
|
|
add(loco);
|
|
|
|
|
|
|
|
trains.put(id, this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Object action(HashMap<String, String> params, Plan plan) throws IOException {
|
|
|
|
public static Object action(HashMap<String, String> params, Plan plan) throws IOException {
|
|
|
|
String action = params.get(ACTION);
|
|
|
|
String action = params.get(ACTION);
|
|
|
|
if (isNull(action)) return t("No action passed to Train.action!");
|
|
|
|
if (isNull(action)) return t("No action passed to Train.action!");
|
|
|
@@ -142,7 +128,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|
|
|
return t("No train id passed!");
|
|
|
|
return t("No train id passed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Id id = Id.from(params);
|
|
|
|
Id id = Id.from(params);
|
|
|
|
Train train = trains.get(id);
|
|
|
|
Train train = BaseClass.get(id);
|
|
|
|
if (isNull(train)) return(t("No train with id {}!",id));
|
|
|
|
if (isNull(train)) return(t("No train with id {}!",id));
|
|
|
|
switch (action) {
|
|
|
|
switch (action) {
|
|
|
|
case ACTION_ADD:
|
|
|
|
case ACTION_ADD:
|
|
|
@@ -213,10 +199,12 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|
|
|
return properties();
|
|
|
|
return properties();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void add(Car car) {
|
|
|
|
public Train add(Car car) {
|
|
|
|
if (isNull(car)) return;
|
|
|
|
if (isSet(car)) {
|
|
|
|
cars.add(car);
|
|
|
|
cars.add(car);
|
|
|
|
car.train(this);
|
|
|
|
car.train(this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String automatic() {
|
|
|
|
public String automatic() {
|
|
|
@@ -327,7 +315,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|
|
|
if (isNull(locoId)) return t("Need loco id to create new train!");
|
|
|
|
if (isNull(locoId)) return t("Need loco id to create new train!");
|
|
|
|
Locomotive loco = BaseClass.get(new Id(locoId));
|
|
|
|
Locomotive loco = BaseClass.get(new Id(locoId));
|
|
|
|
if (isNull(loco)) return t("unknown locomotive: {}",params.get(ID));
|
|
|
|
if (isNull(loco)) return t("unknown locomotive: {}",params.get(ID));
|
|
|
|
Train train = new Train(loco);
|
|
|
|
Train train = new Train().add(loco);
|
|
|
|
train.parent(plan);
|
|
|
|
train.parent(plan);
|
|
|
|
if (params.containsKey(NAME)) train.name(params.get(NAME));
|
|
|
|
if (params.containsKey(NAME)) train.name(params.get(NAME));
|
|
|
|
train.register();
|
|
|
|
train.register();
|
|
|
@@ -386,10 +374,6 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|
|
|
return properties();
|
|
|
|
return properties();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Train get(Id id) {
|
|
|
|
|
|
|
|
return trains.get(id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Train heading(Direction dir) {
|
|
|
|
public Train heading(Direction dir) {
|
|
|
|
LOG.debug("{}.heading({})",this,dir);
|
|
|
|
LOG.debug("{}.heading({})",this,dir);
|
|
|
|
direction = dir;
|
|
|
|
direction = dir;
|
|
|
@@ -448,8 +432,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|
|
|
while (isSet(line)) {
|
|
|
|
while (isSet(line)) {
|
|
|
|
JSONObject json = new JSONObject(line);
|
|
|
|
JSONObject json = new JSONObject(line);
|
|
|
|
|
|
|
|
|
|
|
|
Train train = new Train(null,Id.from(json));
|
|
|
|
new Train().load(json).parent(plan);
|
|
|
|
train.load(json).parent(plan);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
line = file.readLine();
|
|
|
|
line = file.readLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -537,7 +520,13 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String name() {
|
|
|
|
public String name() {
|
|
|
|
return (isSet(name) ? name : cars.stream().filter(car -> isSet(car.name())).findFirst().get().name());
|
|
|
|
if (isSet(name)) return name;
|
|
|
|
|
|
|
|
if (cars.isEmpty()) return t("emtpy train");
|
|
|
|
|
|
|
|
for (Car car : cars) {
|
|
|
|
|
|
|
|
String name = car.name();
|
|
|
|
|
|
|
|
if (isSet(name)) return name;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return t("empty train");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Train name(String newName) {
|
|
|
|
private Train name(String newName) {
|
|
|
@@ -694,10 +683,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|
|
|
|
|
|
|
|
|
|
|
public static void saveAll(String filename) throws IOException {
|
|
|
|
public static void saveAll(String filename) throws IOException {
|
|
|
|
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
|
|
|
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
|
|
|
|
for (Entry<Id, Train> entry:trains.entrySet()) {
|
|
|
|
for (Train train:BaseClass.listElements(Train.class)) file.write(train.json()+"\n");
|
|
|
|
Train train = entry.getValue();
|
|
|
|
|
|
|
|
file.write(train.json()+"\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|