Skip to content

Commit

Permalink
refactor: deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
hoelger committed Jan 9, 2025
1 parent e2c1752 commit e24ce0a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,32 @@ private boolean handleEventResource(Object resource) {
return false;
}

private static class ChangeLaneStateImpl implements ChangeLaneState {
private static class ChangeLaneStateBaseImpl {

private final AbstractSimulationUnit unit;
final AbstractSimulationUnit unit;

private ChangeLaneStateBaseImpl(AbstractSimulationUnit unit) {
this.unit = unit;
}

void sendInteractionQuietly(Interaction interaction) {
try {
unit.sendInteractionToRti(interaction);
} catch (RuntimeException e) {
unit.getOsLog().error("Could not send interaction", e);
}
}
}

private static class ChangeLaneStateImpl extends ChangeLaneStateBaseImpl implements ChangeLaneState {

private final String edgeId;
private final int laneIndex;

private ChangeLaneStateImpl(String edgeId, int laneIndex, AbstractSimulationUnit unit) {
super(unit);
this.edgeId = edgeId;
this.laneIndex = laneIndex;
this.unit = unit;
}

@Override
Expand Down Expand Up @@ -217,42 +232,22 @@ public ChangeLaneState closeOnlyForVehicleClasses(VehicleClass... disallowVehicl
return this;
}

@Override
public ChangeLaneState closeForAll() {
return closeOnlyForVehicleClasses(VehicleClass.values());
}

@Override
public ChangeLaneState openForAll() {
return openOnlyForVehicleClasses(VehicleClass.values());
}

@Override
public ChangeLaneState setMaxSpeed(double maxSpeedMs) {
sendInteractionQuietly(new LanePropertyChange(unit.getSimulationTime(), edgeId, laneIndex, null, null, maxSpeedMs));
return this;
}

private void sendInteractionQuietly(Interaction interaction) {
try {
unit.sendInteractionToRti(interaction);
} catch (RuntimeException e) {
unit.getOsLog().error("Could not send interaction", e);
}
}
}

private static class ChangeVariableMessageSignStateImpl implements ChangeLaneState {

private final AbstractSimulationUnit unit;
private static class ChangeVariableMessageSignStateImpl extends ChangeLaneStateBaseImpl implements ChangeLaneState {

private final String signId;
private final int laneIndex;

private ChangeVariableMessageSignStateImpl(String signId, int laneIndex, AbstractSimulationUnit unit) {
super(unit);
this.signId = signId;
this.laneIndex = laneIndex;
this.unit = unit;
}

@Override
Expand All @@ -279,16 +274,6 @@ public ChangeLaneState closeOnlyForVehicleClasses(VehicleClass... disallowVehicl
return openOnlyForVehicleClasses(remaining.toArray(new VehicleClass[remaining.size()]));
}

@Override
public ChangeLaneState closeForAll() {
return closeOnlyForVehicleClasses(VehicleClass.values());
}

@Override
public ChangeLaneState openForAll() {
return openOnlyForVehicleClasses(VehicleClass.values());
}

@Override
public ChangeLaneState setMaxSpeed(double maxSpeedMs) {
if (laneIndex < 0) {
Expand All @@ -312,12 +297,5 @@ public ChangeLaneState setMaxSpeed(double maxSpeedMs) {
return this;
}

private void sendInteractionQuietly(Interaction interaction) {
try {
unit.sendInteractionToRti(interaction);
} catch (RuntimeException e) {
unit.getOsLog().error("Could not send interaction", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ interface ChangeLaneState {
* Closes the lane for all vehicle classes.
*/
@SuppressWarnings("UnusedReturnValue")
ChangeLaneState closeForAll();
default ChangeLaneState closeForAll(){
return closeOnlyForVehicleClasses(VehicleClass.values());
}

/**
* Opens the lane for all vehicle classes.
*/
@SuppressWarnings("UnusedReturnValue")
ChangeLaneState openForAll();
default ChangeLaneState openForAll() {
return openOnlyForVehicleClasses(VehicleClass.values());
}

/**
* Sets the maximum allowed speed of the lane to the given value.
Expand Down

0 comments on commit e24ce0a

Please sign in to comment.