Skip to content

Commit

Permalink
Fix code smell: duplication
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas ADAM <tadam@silicom.fr>
  • Loading branch information
tadam50 committed Jul 25, 2023
1 parent eb6ec4c commit 5b54803
Showing 1 changed file with 30 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public abstract class AbstractRawBuilder implements BaseRawBuilder {

protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractRawBuilder.class);

private static final String REQUIRED_N_VOLTAGELEVEL_RAW_BUILDER = "Require 3 VoltageLevelRawBuilder (found %d)";
private static final String REQUIRED_N_VOLTAGELEVEL_RAW_BUILDER = "Expected '%d' VoltageLevelRawBuilders but only '%d' found";

private static final String REQUIRED_N_ORDER = "Require 3 node order (found %d)";
private static final String REQUIRED_N_ORDER = "Expected '%d' node orders but only '%d' found";

private static final String REQUIRED_N_DIRECTION = "Require 3 node direction (found %d)";
private static final String REQUIRED_N_DIRECTION = "Expected '%d' node directions but only '%d' found";

protected abstract BaseGraph getGraph();

Expand All @@ -48,15 +48,8 @@ public Map<VoltageLevelRawBuilder, FeederNode> createLine(String id,
List<VoltageLevelRawBuilder> vls,
List<Integer> orders,
List<Direction> directions) {
if (vls.size() != 2) {
throw new IllegalArgumentException(String.format(REQUIRED_N_VOLTAGELEVEL_RAW_BUILDER, Math.abs(3 - vls.size())));
}
if (orders.size() != 2) {
throw new IllegalArgumentException(String.format(REQUIRED_N_ORDER, Math.abs(3 - orders.size())));
}
if (directions.size() != 2) {
throw new IllegalArgumentException(String.format(REQUIRED_N_DIRECTION, Math.abs(3 - directions.size())));
}
checkInputParameters(2, vls, orders, directions);

VoltageLevelRawBuilder vl1 = vls.get(0);
VoltageLevelRawBuilder vl2 = vls.get(1);
int order1 = orders.get(0);
Expand All @@ -70,10 +63,10 @@ public Map<VoltageLevelRawBuilder, FeederNode> createLine(String id,
feederLineNodes.put(vl2, feederLineNode2);

if (containsVoltageLevelRawBuilders(vl1, vl2)) {
// All VoltageLevel must be in the same Substation
// All VoltageLevel must be in the same Substation or the same Zone
getGraph().addLineEdge(id, feederLineNode1, feederLineNode2);
} else {
// At least one VoltageLevel is not in the same Substation (Zone graph case)
// Using SubstationRawBuilder in place of ZoneRawBuilder
manageErrorCase(Stream.of(vl1, vl2));
}
return feederLineNodes;
Expand All @@ -89,15 +82,8 @@ public Map<VoltageLevelRawBuilder, FeederNode> createFeeder2WT(String id,
List<VoltageLevelRawBuilder> vls,
List<Integer> orders,
List<Direction> directions) {
if (vls.size() != 2) {
throw new IllegalArgumentException(String.format(REQUIRED_N_VOLTAGELEVEL_RAW_BUILDER, Math.abs(3 - vls.size())));
}
if (orders.size() != 2) {
throw new IllegalArgumentException(String.format(REQUIRED_N_ORDER, Math.abs(3 - orders.size())));
}
if (directions.size() != 2) {
throw new IllegalArgumentException(String.format(REQUIRED_N_DIRECTION, Math.abs(3 - directions.size())));
}
checkInputParameters(2, vls, orders, directions);

VoltageLevelRawBuilder vl1 = vls.get(0);
VoltageLevelRawBuilder vl2 = vls.get(1);
int order1 = orders.get(0);
Expand All @@ -110,10 +96,10 @@ public Map<VoltageLevelRawBuilder, FeederNode> createFeeder2WT(String id,
f2WTNodes.put(vl1, feeder2WtNode1);
f2WTNodes.put(vl2, feeder2WTNode2);
if (containsVoltageLevelRawBuilders(vl1, vl2)) {
// All VoltageLevel must be in the same Substation
// All VoltageLevel must be in the same Substation or the same Zone
NodeFactory.createMiddle2WTNode(getGraph(), id, id, feeder2WtNode1, feeder2WTNode2, vl1.getVoltageLevelInfos(), vl2.getVoltageLevelInfos(), false);
} else {
// At least one VoltageLevel is not in the same Substation (Zone graph case)
// Using SubstationRawBuilder in place of ZoneRawBuilder
manageErrorCase(Stream.of(vl1, vl2));
}
return f2WTNodes;
Expand All @@ -129,15 +115,8 @@ public Map<VoltageLevelRawBuilder, FeederNode> createFeeder3WT(String id,
List<VoltageLevelRawBuilder> vls,
List<Integer> orders,
List<Direction> directions) {
if (vls.size() != 3) {
throw new IllegalArgumentException(String.format(REQUIRED_N_VOLTAGELEVEL_RAW_BUILDER, Math.abs(3 - vls.size())));
}
if (orders.size() != 3) {
throw new IllegalArgumentException(String.format(REQUIRED_N_ORDER, Math.abs(3 - orders.size())));
}
if (directions.size() != 3) {
throw new IllegalArgumentException(String.format(REQUIRED_N_DIRECTION, Math.abs(3 - directions.size())));
}
checkInputParameters(3, vls, orders, directions);

VoltageLevelRawBuilder vl1 = vls.get(0);
VoltageLevelRawBuilder vl2 = vls.get(1);
VoltageLevelRawBuilder vl3 = vls.get(2);
Expand All @@ -156,10 +135,11 @@ public Map<VoltageLevelRawBuilder, FeederNode> createFeeder3WT(String id,
f3WTNodes.put(vl3, feeder3WTNode3);

if (containsVoltageLevelRawBuilders(vl1, vl2, vl3)) {
// All VoltageLevel must be in the same Substation
// All VoltageLevel must be in the same Substation or the same Zone
// creation of the middle node and the edges linking the transformer leg nodes to this middle node
NodeFactory.createMiddle3WTNode(getGraph(), id, id, feeder3WTNode1, feeder3WTNode2, feeder3WTNode3);
} else {
// Using SubstationRawBuilder in place of ZoneRawBuilder
manageErrorCase(Stream.of(vl1, vl2, vl3));
}
return f3WTNodes;
Expand All @@ -169,4 +149,19 @@ public Map<VoltageLevelRawBuilder, FeederNode> createFeeder3WT(String id,
public Map<VoltageLevelRawBuilder, FeederNode> createFeeder3WT(String id, VoltageLevelRawBuilder vl1, VoltageLevelRawBuilder vl2, VoltageLevelRawBuilder vl3) {
return createFeeder3WT(id, List.of(vl1, vl2, vl3), List.of(0, 0, 0), Stream.of((Direction) null, null, null).toList());
}

private void checkInputParameters(int expectedSize,
List<VoltageLevelRawBuilder> vls,
List<Integer> orders,
List<Direction> directions) {
if (vls.size() != expectedSize) {
throw new IllegalArgumentException(String.format(REQUIRED_N_VOLTAGELEVEL_RAW_BUILDER, expectedSize, Math.abs(expectedSize - vls.size())));
}
if (orders.size() != expectedSize) {
throw new IllegalArgumentException(String.format(REQUIRED_N_ORDER, expectedSize, Math.abs(expectedSize - orders.size())));
}
if (directions.size() != expectedSize) {
throw new IllegalArgumentException(String.format(REQUIRED_N_DIRECTION, expectedSize, Math.abs(expectedSize - directions.size())));
}
}
}

0 comments on commit 5b54803

Please sign in to comment.