PIbd-23_Minhasapov_R.H._Exc.../TracktorSerde.java

70 lines
2.6 KiB
Java

import java.awt.*;
public class TracktorSerde { // Tracktor Serialization/Deserialization
private static final char _separatorForObject = ':';
public static DrawningTracktor deserialize(String info) {
String[] strings = info.split(Character.toString(_separatorForObject));
int speed = Integer.parseInt(strings[0]);
float weight = Float.parseFloat(strings[1]);
Color bodyColor = new Color(Integer.parseInt(strings[2]));
IDrawningRollers rollers = switch (strings[3]) {
case "DrawningRollers" -> new DrawningRollers(Integer.parseInt(strings[4]), bodyColor);
case "DrawningCrossRollers" -> new DrawningCrossRollers(Integer.parseInt(strings[4]), bodyColor);
case "DrawningSquaredRollers" -> new DrawningSquaredRollers(Integer.parseInt(strings[4]), bodyColor);
default -> null;
};
if (strings.length == 5) {
EntityTracktor entity = new EntityTracktor(speed, weight, bodyColor);
return new DrawningTracktor(entity, rollers);
}
if (strings.length == 8) {
Color dopColor = new Color(Integer.parseInt(strings[5]));
boolean bucket = Boolean.parseBoolean(strings[6]);
boolean supports = Boolean.parseBoolean(strings[7]);
EntityTrackedVehicle entity = new EntityTrackedVehicle(speed, weight, bodyColor, dopColor, bucket, supports);
return new DrawningTrackedVehicle(entity, rollers);
}
return null;
}
public static String serialize(DrawningTracktor drawingTracktor) {
EntityTracktor tracktor = drawingTracktor.getTracktor();
String result = String.format(
"%d%c%s%c%d%c%s%c%d",
tracktor.getSpeed(),
_separatorForObject,
tracktor.getWeight(),
_separatorForObject,
tracktor.getBodyColor().getRGB(),
_separatorForObject,
drawingTracktor.getRollers().getClass().getSimpleName(),
_separatorForObject,
drawingTracktor.getRollers().getRollersCount()
);
if (!(tracktor instanceof EntityTrackedVehicle trackedVehicle)) {
return result;
}
return String.format(
"%s%c%d%c%b%c%b",
result,
_separatorForObject,
trackedVehicle.getDopColor().getRGB(),
_separatorForObject,
trackedVehicle.getBucket(),
_separatorForObject,
trackedVehicle.getSupports()
);
}
}