serialization
This commit is contained in:
parent
1b7531633d
commit
1d780ce01e
@ -1,70 +1,79 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.dto.CircuitDto;
|
||||
import de.ph87.electro.circuit.dto.PartDto;
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x1;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitchCross;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
public class Circuit {
|
||||
|
||||
private final List<Part> parts = new ArrayList<>();
|
||||
|
||||
private final List<PartBattery> batteries = new ArrayList<>();
|
||||
|
||||
private final List<PartOther> others = new ArrayList<>();
|
||||
public Circuit(final CircuitDto dto) {
|
||||
final List<Junction> junctions = new ArrayList<>();
|
||||
for (PartDto partDto : dto.getParts()) {
|
||||
final Part part = Part.of(partDto);
|
||||
parts.add(part);
|
||||
junctions.addAll(part.getJunctions());
|
||||
}
|
||||
parts.forEach(part -> part.link(junctions));
|
||||
}
|
||||
|
||||
public PartBattery addBattery(final String name, final int x, final int y, final double voltage) {
|
||||
final PartBattery battery = new PartBattery(name, x, y, voltage);
|
||||
parts.add(battery);
|
||||
batteries.add(battery);
|
||||
return battery;
|
||||
}
|
||||
|
||||
public PartLight addLight(final String name, final int x, final int y, final double maxVoltage) {
|
||||
final PartLight light = new PartLight(name, x, y, maxVoltage);
|
||||
parts.add(light);
|
||||
others.add(light);
|
||||
return light;
|
||||
}
|
||||
|
||||
public PartSwitch1x1 addSwitch1x1(final String name, final int x, final int y) {
|
||||
final PartSwitch1x1 switch1x1 = new PartSwitch1x1(name, x, y);
|
||||
parts.add(switch1x1);
|
||||
others.add(switch1x1);
|
||||
return switch1x1;
|
||||
}
|
||||
|
||||
public PartSwitch1x2 addSwitch1x2(final String name, final int x, final int y) {
|
||||
final PartSwitch1x2 switch1x2 = new PartSwitch1x2(name, x, y);
|
||||
parts.add(switch1x2);
|
||||
others.add(switch1x2);
|
||||
return switch1x2;
|
||||
}
|
||||
|
||||
public PartSwitchCross addSwitchCross(final String name, final int x, final int y) {
|
||||
final PartSwitchCross switchCross = new PartSwitchCross(name, x, y);
|
||||
parts.add(switchCross);
|
||||
others.add(switchCross);
|
||||
return switchCross;
|
||||
}
|
||||
|
||||
public void connect(final Junction a, final Junction b) {
|
||||
a.getJunctions().add(b);
|
||||
b.getJunctions().add(a);
|
||||
a.getDestinations().add(b);
|
||||
b.getDestinations().add(a);
|
||||
}
|
||||
|
||||
public void evaluate() {
|
||||
parts.forEach(Part::reset);
|
||||
batteries.forEach(PartBattery::startPropagation);
|
||||
streamBatteries().forEach(PartBattery::startPropagation);
|
||||
}
|
||||
|
||||
private Stream<PartBattery> streamBatteries() {
|
||||
return parts.stream().filter(part -> part instanceof PartBattery).map(part -> (PartBattery) part);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
22
src/main/java/de/ph87/electro/circuit/CircuitService.java
Normal file
22
src/main/java/de/ph87/electro/circuit/CircuitService.java
Normal file
@ -0,0 +1,22 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.ph87.electro.circuit.dto.CircuitDto;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class CircuitService {
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public static void write(final Circuit circuit, final OutputStream stream) throws IOException {
|
||||
objectMapper.writeValue(stream, new CircuitDto(circuit));
|
||||
}
|
||||
|
||||
public static Circuit read(final InputStream stream) throws IOException {
|
||||
return new Circuit(objectMapper.readValue(stream, CircuitDto.class));
|
||||
}
|
||||
|
||||
}
|
||||
21
src/main/java/de/ph87/electro/circuit/dto/CircuitDto.java
Normal file
21
src/main/java/de/ph87/electro/circuit/dto/CircuitDto.java
Normal file
@ -0,0 +1,21 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.Circuit;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class CircuitDto {
|
||||
|
||||
private List<PartDto> parts;
|
||||
|
||||
public CircuitDto(final Circuit circuit) {
|
||||
this.parts = circuit.getParts().stream().map(PartDto::of).toList();
|
||||
}
|
||||
|
||||
}
|
||||
31
src/main/java/de/ph87/electro/circuit/dto/JunctionDto.java
Normal file
31
src/main/java/de/ph87/electro/circuit/dto/JunctionDto.java
Normal file
@ -0,0 +1,31 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class JunctionDto {
|
||||
|
||||
private String uuid;
|
||||
|
||||
private String name;
|
||||
|
||||
private Point position;
|
||||
|
||||
private List<String> destinations;
|
||||
|
||||
public JunctionDto(final Junction junction) {
|
||||
this.uuid = junction.getUuid();
|
||||
this.name = junction.getName();
|
||||
this.position = junction.getPosition();
|
||||
this.destinations = junction.getDestinations().stream().map(Junction::getUuid).toList();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class PartBatteryDto extends PartDto {
|
||||
|
||||
private JunctionDto minus;
|
||||
|
||||
private JunctionDto plus;
|
||||
|
||||
private double voltage;
|
||||
|
||||
public PartBatteryDto(final PartBattery partBattery) {
|
||||
super(partBattery);
|
||||
this.minus = new JunctionDto(partBattery.getMinus());
|
||||
this.plus = new JunctionDto(partBattery.getPlus());
|
||||
this.voltage = partBattery.getVoltage();
|
||||
}
|
||||
|
||||
}
|
||||
49
src/main/java/de/ph87/electro/circuit/dto/PartDto.java
Normal file
49
src/main/java/de/ph87/electro/circuit/dto/PartDto.java
Normal file
@ -0,0 +1,49 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x1;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitchCross;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS)
|
||||
public class PartDto {
|
||||
|
||||
private String uuid;
|
||||
|
||||
private String name;
|
||||
|
||||
private Point position;
|
||||
|
||||
private List<JunctionDto> junctions;
|
||||
|
||||
protected PartDto(final Part part) {
|
||||
this.uuid = part.getUuid();
|
||||
this.name = part.getName();
|
||||
this.position = part.getPosition();
|
||||
this.junctions = part.getJunctions().stream().map(JunctionDto::new).toList();
|
||||
}
|
||||
|
||||
public static PartDto of(final Part abstractPart) {
|
||||
return switch (abstractPart) {
|
||||
case final PartBattery part -> new PartBatteryDto(part);
|
||||
case final PartLight part -> new PartLightDto(part);
|
||||
case final PartSwitch1x1 part -> new PartSwitch1x1Dto(part);
|
||||
case final PartSwitch1x2 part -> new PartSwitch1x2Dto(part);
|
||||
case final PartSwitchCross part -> new PartSwitchCrossDto(part);
|
||||
case null, default -> throw new RuntimeException();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
29
src/main/java/de/ph87/electro/circuit/dto/PartLightDto.java
Normal file
29
src/main/java/de/ph87/electro/circuit/dto/PartLightDto.java
Normal file
@ -0,0 +1,29 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class PartLightDto extends PartDto {
|
||||
|
||||
private JunctionDto plus;
|
||||
|
||||
private JunctionDto minus;
|
||||
|
||||
private double maxVoltage;
|
||||
|
||||
private boolean defect;
|
||||
|
||||
public PartLightDto(final PartLight partLight) {
|
||||
super(partLight);
|
||||
this.plus = new JunctionDto(partLight.getPlus());
|
||||
this.minus = new JunctionDto(partLight.getMinus());
|
||||
this.defect = partLight.isDefect();
|
||||
this.maxVoltage = partLight.getMaxVoltage();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x1;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class PartSwitch1x1Dto extends PartDto {
|
||||
|
||||
private JunctionDto common;
|
||||
|
||||
private JunctionDto output;
|
||||
|
||||
private boolean state;
|
||||
|
||||
public PartSwitch1x1Dto(final PartSwitch1x1 partSwitch1x1) {
|
||||
super(partSwitch1x1);
|
||||
this.common = new JunctionDto(partSwitch1x1.getCommon());
|
||||
this.output = new JunctionDto(partSwitch1x1.getOutput());
|
||||
this.state = partSwitch1x1.isState();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class PartSwitch1x2Dto extends PartDto {
|
||||
|
||||
private JunctionDto common;
|
||||
|
||||
private JunctionDto output0;
|
||||
|
||||
private JunctionDto output1;
|
||||
|
||||
private boolean state;
|
||||
|
||||
public PartSwitch1x2Dto(final PartSwitch1x2 partSwitch1x2) {
|
||||
super(partSwitch1x2);
|
||||
this.common = new JunctionDto(partSwitch1x2.getCommon());
|
||||
this.output0 = new JunctionDto(partSwitch1x2.getOutput0());
|
||||
this.output1 = new JunctionDto(partSwitch1x2.getOutput1());
|
||||
this.state = partSwitch1x2.isState();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package de.ph87.electro.circuit.dto;
|
||||
|
||||
import de.ph87.electro.circuit.part.other.PartSwitchCross;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class PartSwitchCrossDto extends PartDto {
|
||||
|
||||
private JunctionDto common0;
|
||||
|
||||
private JunctionDto common1;
|
||||
|
||||
private JunctionDto output0;
|
||||
|
||||
private JunctionDto output1;
|
||||
|
||||
private boolean state;
|
||||
|
||||
public PartSwitchCrossDto(final PartSwitchCross partSwitchCross) {
|
||||
super(partSwitchCross);
|
||||
this.common0 = new JunctionDto(partSwitchCross.getCommon0());
|
||||
this.common1 = new JunctionDto(partSwitchCross.getCommon1());
|
||||
this.output0 = new JunctionDto(partSwitchCross.getOutput0());
|
||||
this.output1 = new JunctionDto(partSwitchCross.getOutput1());
|
||||
this.state = partSwitchCross.isState();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,19 +1,24 @@
|
||||
package de.ph87.electro.circuit.part;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.JunctionDto;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@ToString(onlyExplicitlyIncluded = true)
|
||||
public class Junction {
|
||||
|
||||
private final JunctionDto _dto;
|
||||
|
||||
@ToString.Include
|
||||
private final String uuid;
|
||||
|
||||
private final Part owner;
|
||||
@ -27,13 +32,35 @@ public class Junction {
|
||||
@ToString.Include
|
||||
private double voltage = Double.NaN;
|
||||
|
||||
private final List<Junction> junctions = new ArrayList<>();
|
||||
private final Set<Junction> destinations = new HashSet<>();
|
||||
|
||||
@ToString.Include
|
||||
public List<String> destinations() {
|
||||
return destinations.stream().map(Junction::getUuid).toList();
|
||||
}
|
||||
|
||||
public Junction(final Part owner, final String name, final int x, final int y) {
|
||||
this.uuid = UUID.randomUUID().toString();
|
||||
this.owner = owner;
|
||||
this.name = name;
|
||||
this.position = new Point(x, y);
|
||||
this._dto = null;
|
||||
}
|
||||
|
||||
public Junction(final Part owner, final JunctionDto dto) {
|
||||
this.uuid = dto.getUuid();
|
||||
this.owner = owner;
|
||||
this.name = dto.getName();
|
||||
this.position = dto.getPosition();
|
||||
this._dto = dto;
|
||||
}
|
||||
|
||||
public void link(final List<Junction> junctions) {
|
||||
for (final String otherUuid : _dto.getDestinations()) {
|
||||
final Junction other = junctions.stream().filter(junction -> junction.getUuid().equals(otherUuid)).findFirst().orElseThrow();
|
||||
destinations.add(other);
|
||||
other.destinations.add(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
@ -47,7 +74,7 @@ public class Junction {
|
||||
if (Double.isNaN(voltage)) {
|
||||
voltage = newVoltage;
|
||||
owner.propagate(this);
|
||||
for (Junction junction : junctions) {
|
||||
for (Junction junction : destinations) {
|
||||
junction.propagate(newVoltage);
|
||||
}
|
||||
return;
|
||||
|
||||
@ -1,6 +1,11 @@
|
||||
package de.ph87.electro.circuit.part;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.*;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x1;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitch1x2;
|
||||
import de.ph87.electro.circuit.part.other.PartSwitchCross;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
@ -15,6 +20,7 @@ import java.util.UUID;
|
||||
@ToString(onlyExplicitlyIncluded = true)
|
||||
public abstract class Part {
|
||||
|
||||
@ToString.Include
|
||||
private final String uuid;
|
||||
|
||||
@ToString.Include
|
||||
@ -30,10 +36,31 @@ public abstract class Part {
|
||||
this.position = new Point(x, y);
|
||||
}
|
||||
|
||||
protected Part(final PartDto dto) {
|
||||
this.uuid = dto.getUuid();
|
||||
this.name = dto.getName();
|
||||
this.position = dto.getPosition();
|
||||
}
|
||||
|
||||
public void link(final List<Junction> junctions) {
|
||||
junctions.forEach(junction -> junction.link(junctions));
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
junctions.forEach(Junction::reset);
|
||||
}
|
||||
|
||||
public abstract void propagate(final Junction source) throws ShortCircuit;
|
||||
|
||||
public static Part of(final PartDto abstractDto) {
|
||||
return switch (abstractDto) {
|
||||
case final PartBatteryDto dto -> new PartBattery(dto);
|
||||
case final PartLightDto dto -> new PartLight(dto);
|
||||
case final PartSwitch1x1Dto dto -> new PartSwitch1x1(dto);
|
||||
case final PartSwitch1x2Dto dto -> new PartSwitch1x2(dto);
|
||||
case final PartSwitchCrossDto dto -> new PartSwitchCross(dto);
|
||||
case null, default -> throw new RuntimeException();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,28 +1,40 @@
|
||||
package de.ph87.electro.circuit.part;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.PartBatteryDto;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class PartBattery extends Part {
|
||||
|
||||
private final Junction plus;
|
||||
|
||||
private final Junction minus;
|
||||
|
||||
private final double voltage;
|
||||
private final Junction plus;
|
||||
|
||||
@Setter
|
||||
private double voltage;
|
||||
|
||||
private ShortCircuit shortCircuit = null;
|
||||
|
||||
public PartBattery(final String name, final int x, final int y, final double voltage) {
|
||||
public PartBattery(final String name, final int x, final int y, final double initialVoltage) {
|
||||
super(name, x, y);
|
||||
this.voltage = voltage;
|
||||
minus = new Junction(this, "-", 0, 1);
|
||||
plus = new Junction(this, "+", 2, 1);
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
voltage = initialVoltage;
|
||||
}
|
||||
|
||||
public PartBattery(final PartBatteryDto dto) {
|
||||
super(dto);
|
||||
minus = new Junction(this, dto.getMinus());
|
||||
plus = new Junction(this, dto.getPlus());
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
voltage = dto.getVoltage();
|
||||
}
|
||||
|
||||
public void startPropagation() {
|
||||
|
||||
@ -1,9 +1,15 @@
|
||||
package de.ph87.electro.circuit.part;
|
||||
|
||||
import de.ph87.electro.circuit.dto.PartLightDto;
|
||||
|
||||
public abstract class PartOther extends Part {
|
||||
|
||||
protected PartOther(final String name, final int x, final int y) {
|
||||
super(name, x, y);
|
||||
}
|
||||
|
||||
protected PartOther(final PartLightDto dto) {
|
||||
super(dto);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
|
||||
import de.ph87.electro.circuit.dto.PartLightDto;
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import lombok.Getter;
|
||||
@ -11,23 +12,33 @@ import static java.lang.Math.abs;
|
||||
@ToString(callSuper = true)
|
||||
public class PartLight extends PartOther {
|
||||
|
||||
private final double maxVoltage;
|
||||
|
||||
private final Junction minus;
|
||||
|
||||
private final Junction plus;
|
||||
|
||||
private final double maxVoltage;
|
||||
|
||||
private boolean defect = false;
|
||||
|
||||
private double voltage = 0;
|
||||
|
||||
public PartLight(final String name, final int x, final int y, final double maxVoltage) {
|
||||
public PartLight(final String name, final int x, final int y, final double initialMaxVoltage) {
|
||||
super(name, x, y);
|
||||
this.maxVoltage = maxVoltage;
|
||||
minus = new Junction(this, "", 0, 1);
|
||||
plus = new Junction(this, "", 2, 1);
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
maxVoltage = initialMaxVoltage;
|
||||
}
|
||||
|
||||
public PartLight(final PartLightDto dto) {
|
||||
super(dto);
|
||||
minus = new Junction(this, dto.getMinus());
|
||||
plus = new Junction(this, dto.getPlus());
|
||||
junctions.add(minus);
|
||||
junctions.add(plus);
|
||||
maxVoltage = dto.getMaxVoltage();
|
||||
defect = dto.isDefect();
|
||||
}
|
||||
|
||||
public void repair() {
|
||||
@ -35,7 +46,7 @@ public class PartLight extends PartOther {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void propagate(final Junction source) {
|
||||
public void propagate(final Junction source) {
|
||||
voltage = abs(plus.getVoltage() - minus.getVoltage());
|
||||
if (voltage > maxVoltage) {
|
||||
defect = true;
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.PartSwitch1x1Dto;
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import lombok.Getter;
|
||||
@ -26,6 +27,15 @@ public class PartSwitch1x1 extends PartOther {
|
||||
junctions.add(output);
|
||||
}
|
||||
|
||||
public PartSwitch1x1(final PartSwitch1x1Dto dto) {
|
||||
super(dto.getName(), dto.getPosition().x, dto.getPosition().y);
|
||||
common = new Junction(this, dto.getCommon());
|
||||
output = new Junction(this, dto.getOutput());
|
||||
junctions.add(common);
|
||||
junctions.add(output);
|
||||
state = dto.isState();
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.PartSwitch1x2Dto;
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import lombok.Getter;
|
||||
@ -30,6 +31,17 @@ public class PartSwitch1x2 extends PartOther {
|
||||
junctions.add(output1);
|
||||
}
|
||||
|
||||
public PartSwitch1x2(final PartSwitch1x2Dto dto) {
|
||||
super(dto.getName(), dto.getPosition().x, dto.getPosition().y);
|
||||
common = new Junction(this, dto.getCommon());
|
||||
output0 = new Junction(this, dto.getOutput0());
|
||||
output1 = new Junction(this, dto.getOutput1());
|
||||
junctions.add(common);
|
||||
junctions.add(output0);
|
||||
junctions.add(output1);
|
||||
state = dto.isState();
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package de.ph87.electro.circuit.part.other;
|
||||
|
||||
import de.ph87.electro.circuit.ShortCircuit;
|
||||
import de.ph87.electro.circuit.dto.PartSwitchCrossDto;
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import de.ph87.electro.circuit.part.PartOther;
|
||||
import lombok.Getter;
|
||||
@ -34,6 +35,19 @@ public class PartSwitchCross extends PartOther {
|
||||
junctions.add(output1);
|
||||
}
|
||||
|
||||
public PartSwitchCross(final PartSwitchCrossDto dto) {
|
||||
super(dto.getName(), dto.getPosition().x, dto.getPosition().y);
|
||||
common0 = new Junction(this, dto.getCommon0());
|
||||
common1 = new Junction(this, dto.getCommon1());
|
||||
output0 = new Junction(this, dto.getOutput0());
|
||||
output1 = new Junction(this, dto.getOutput1());
|
||||
junctions.add(common0);
|
||||
junctions.add(common1);
|
||||
junctions.add(output0);
|
||||
junctions.add(output1);
|
||||
state = dto.isState();
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
@ -0,0 +1,64 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.Junction;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.PartBattery;
|
||||
import de.ph87.electro.circuit.part.other.PartLight;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class CircuitServiceTest {
|
||||
|
||||
@Test
|
||||
void serialization() throws IOException {
|
||||
final Circuit circuit = new Circuit();
|
||||
final PartBattery battery = circuit.addBattery("Batterie", 1, 0, 3.0);
|
||||
final PartLight light = circuit.addLight("Licht", 1, 1, 3.0);
|
||||
circuit.connect(battery.getPlus(), light.getPlus());
|
||||
circuit.connect(light.getMinus(), battery.getMinus());
|
||||
check(circuit);
|
||||
}
|
||||
|
||||
private void check(final Circuit original) throws IOException {
|
||||
final ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
CircuitService.write(original, output);
|
||||
System.out.println(output.toString(StandardCharsets.UTF_8));
|
||||
|
||||
final ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
|
||||
final Circuit reloaded = CircuitService.read(input);
|
||||
|
||||
original.evaluate();
|
||||
reloaded.evaluate();
|
||||
|
||||
assertEquals(original.getParts().size(), reloaded.getParts().size());
|
||||
for (final Part originalPart : original.getParts()) {
|
||||
System.out.printf("Part: %s\n", originalPart.getUuid());
|
||||
final Part reloadedPart = reloaded.getParts().stream().filter(part -> part.getUuid().equals(originalPart.getUuid())).findFirst().orElseThrow();
|
||||
assertEquals(originalPart.getUuid(), reloadedPart.getUuid());
|
||||
assertEquals(originalPart.getName(), reloadedPart.getName());
|
||||
assertEquals(originalPart.getPosition(), reloadedPart.getPosition());
|
||||
assertEquals(originalPart.getJunctions().size(), reloadedPart.getJunctions().size());
|
||||
for (final Junction originalJunction : originalPart.getJunctions()) {
|
||||
System.out.printf(" - Junction: %s\n", originalJunction.getUuid());
|
||||
final Junction reloadedJunction = reloadedPart.getJunctions().stream().filter(junction -> junction.getUuid().equals(originalJunction.getUuid())).findFirst().orElseThrow();
|
||||
assertEquals(originalJunction.getUuid(), reloadedJunction.getUuid());
|
||||
assertEquals(originalJunction.getName(), reloadedJunction.getName());
|
||||
assertEquals(originalJunction.getPosition(), reloadedJunction.getPosition());
|
||||
assertEquals(originalJunction.getDestinations().size(), reloadedJunction.getDestinations().size());
|
||||
for (final Junction originalDestination : originalJunction.getDestinations()) {
|
||||
System.out.printf(" - Destination: %s\n", originalDestination.getUuid());
|
||||
final Junction reloadedDestination = reloadedJunction.getDestinations().stream().filter(destination -> destination.getUuid().equals(originalDestination.getUuid())).findFirst().orElseThrow();
|
||||
assertEquals(originalDestination.getUuid(), reloadedDestination.getUuid());
|
||||
assertEquals(originalDestination.getOwner().getUuid(), reloadedDestination.getOwner().getUuid());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user