Skip to content

Commit

Permalink
Making InputContainers immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
ckittl committed Mar 11, 2020
1 parent da2cd75 commit 64e7629
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@
import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.models.input.graphics.LineGraphicInput;
import edu.ie3.datamodel.models.input.graphics.NodeGraphicInput;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.*;

/** Represents the aggregation of graphic data elements (node graphics, line graphics) */
/** Represents the accumulation of graphic data elements (node graphics, line graphics) */
public class GraphicElements implements InputContainer {

private final LinkedList<NodeGraphicInput> nodeGraphics = new LinkedList<>();
private final LinkedList<LineGraphicInput> lineGraphics = new LinkedList<>();
private final Set<NodeGraphicInput> nodeGraphics;
private final Set<LineGraphicInput> lineGraphics;

@Override
public void add(UniqueEntity entity) {
if (entity instanceof NodeGraphicInput) add((NodeGraphicInput) entity);
else if (entity instanceof LineGraphicInput) add((LineGraphicInput) entity);
else
throw new IllegalArgumentException(
"Entity type is unknown, cannot add entity [" + entity + "]");
public GraphicElements(Set<NodeGraphicInput> nodeGraphics, Set<LineGraphicInput> lineGraphics) {
this.nodeGraphics = nodeGraphics;
this.lineGraphics = lineGraphics;
}

@Override
Expand All @@ -40,21 +34,13 @@ public boolean areValuesValid() {
return true; // no check defined in ValidationTools, so no need for unnecessary instanceofs
}

public void add(NodeGraphicInput entity) {
nodeGraphics.add(entity);
}

public void add(LineGraphicInput entity) {
lineGraphics.add(entity);
}

/** @return unmodifiable List of all node graphic data for this grid */
public List<NodeGraphicInput> getNodeGraphics() {
return Collections.unmodifiableList(nodeGraphics);
/** @return unmodifiable Set of all node graphic data for this grid */
public Set<NodeGraphicInput> getNodeGraphics() {
return Collections.unmodifiableSet(nodeGraphics);
}

/** @return unmodifiable List of all line graphic data for this grid */
public List<LineGraphicInput> getLineGraphics() {
return Collections.unmodifiableList(lineGraphics);
/** @return unmodifiable Set of all line graphic data for this grid */
public Set<LineGraphicInput> getLineGraphics() {
return Collections.unmodifiableSet(lineGraphics);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,6 @@ public GridContainer(
this.graphics = graphics;
}

@Override
public void add(UniqueEntity entity) {
try {
rawGrid.add(entity);
return;
} catch (IllegalArgumentException ignored) { // No further exception handling needed
}
try {
systemParticipants.add(entity);
return;
} catch (IllegalArgumentException ignored) { // No further exception handling needed
}
try {
graphics.add(entity);
return;
} catch (IllegalArgumentException ignored) { // No further exception handling needed
}
throw new IllegalArgumentException(
"Entity type is unknown, cannot add entity [" + entity + "]");
}

@Override
public List<UniqueEntity> allEntitiesAsList() {
List<UniqueEntity> allEntities = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,11 @@

import edu.ie3.datamodel.models.UniqueEntity;
import edu.ie3.datamodel.utils.ValidationUtils;
import java.util.Collection;
import java.util.List;

/** Represents an aggregation of different entities */
public interface InputContainer {

/** Adds an entity to the accumulated entities if its type is compatible */
void add(UniqueEntity entity);

/**
* Adds all elements of a collection of entities to the accumulated entities if their type is
* compatible
*/
default void addAll(Collection<? extends UniqueEntity> entities) {
for (UniqueEntity entity : entities) {
add(entity);
}
}

/** @return unmodifiable List of all entities */
List<UniqueEntity> allEntitiesAsList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,41 @@
import edu.ie3.datamodel.models.input.connector.Transformer2WInput;
import edu.ie3.datamodel.models.input.connector.Transformer3WInput;
import edu.ie3.datamodel.utils.ValidationUtils;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.*;

/** Represents the aggregation of raw grid elements (nodes, lines, transformers, switches) */
public class RawGridElements implements InputContainer {
/** List of nodes in this grid */
private final LinkedList<NodeInput> nodes = new LinkedList<>();
/** List of lines in this grid */
private final LinkedList<LineInput> lines = new LinkedList<>();
/** List of two winding transformers in this grid */
private final LinkedList<Transformer2WInput> transformer2Ws = new LinkedList<>();
/** List of three winding in this grid */
private final LinkedList<Transformer3WInput> transformer3Ws = new LinkedList<>();
/** List of switches in this grid */
private final LinkedList<SwitchInput> switches = new LinkedList<>();
/** Set of nodes in this grid */
private final Set<NodeInput> nodes;
/** Set of lines in this grid */
private final Set<LineInput> lines;
/** Set of two winding transformers in this grid */
private final Set<Transformer2WInput> transformer2Ws;
/** Set of three winding in this grid */
private final Set<Transformer3WInput> transformer3Ws;
/** Set of switches in this grid */
private final Set<SwitchInput> switches;
/** Measurement units in this grid */
private final List<MeasurementUnitInput> measurementUnits = new LinkedList<>();

@Override
public void add(UniqueEntity entity) {
if (entity instanceof MeasurementUnitInput) {
add((MeasurementUnitInput) entity);
return;
}
if (entity instanceof NodeInput) {
add((NodeInput) entity);
return;
}
if (entity instanceof LineInput) {
add((LineInput) entity);
return;
}
if (entity instanceof Transformer2WInput) {
add((Transformer2WInput) entity);
return;
}
if (entity instanceof Transformer3WInput) {
add((Transformer3WInput) entity);
return;
}
if (entity instanceof SwitchInput) {
add((SwitchInput) entity);
return;
}
throw new IllegalArgumentException(
"Entity type is unknown, cannot add entity [" + entity + "]");
private final Set<MeasurementUnitInput> measurementUnits;

public RawGridElements(
Set<NodeInput> nodes,
Set<LineInput> lines,
Set<Transformer2WInput> transformer2Ws,
Set<Transformer3WInput> transformer3Ws,
Set<SwitchInput> switches,
Set<MeasurementUnitInput> measurementUnits) {
this.nodes = nodes;
this.lines = lines;
this.transformer2Ws = transformer2Ws;
this.transformer3Ws = transformer3Ws;
this.switches = switches;
this.measurementUnits = measurementUnits;
}

@Override
public List<UniqueEntity> allEntitiesAsList() {
List<UniqueEntity> allEntities = new LinkedList<>();
List<UniqueEntity> allEntities = new ArrayList<>();
allEntities.addAll(nodes);
allEntities.addAll(lines);
allEntities.addAll(transformer2Ws);
Expand Down Expand Up @@ -97,56 +80,33 @@ public boolean areValuesValid() {
return true;
}

public void add(NodeInput entity) {
nodes.add(entity);
}

public void add(LineInput entity) {
lines.add(entity);
}

public void add(Transformer2WInput entity) {
transformer2Ws.add(entity);
}

public void add(Transformer3WInput entity) {
transformer3Ws.add(entity);
}

public void add(SwitchInput entity) {
switches.add(entity);
}

public void add(MeasurementUnitInput entity) {
measurementUnits.add(entity);
}

/** @return unmodifiable List of all three winding transformers in this grid */
public List<NodeInput> getNodes() {
return Collections.unmodifiableList(nodes);
/** @return unmodifiable ; of all three winding transformers in this grid */
public Set<NodeInput> getNodes() {
return Collections.unmodifiableSet(nodes);
}

/** @return unmodifiable List of all lines in this grid */
public List<LineInput> getLines() {
return Collections.unmodifiableList(lines);
/** @return unmodifiable Set of all lines in this grid */
public Set<LineInput> getLines() {
return Collections.unmodifiableSet(lines);
}

/** @return unmodifiable List of all two winding transformers in this grid */
public List<Transformer2WInput> getTransformer2Ws() {
return Collections.unmodifiableList(transformer2Ws);
/** @return unmodifiable Set of all two winding transformers in this grid */
public Set<Transformer2WInput> getTransformer2Ws() {
return Collections.unmodifiableSet(transformer2Ws);
}

/** @return unmodifiable List of all three winding transformers in this grid */
public List<Transformer3WInput> getTransformer3Ws() {
return Collections.unmodifiableList(transformer3Ws);
/** @return unmodifiable Set of all three winding transformers in this grid */
public Set<Transformer3WInput> getTransformer3Ws() {
return Collections.unmodifiableSet(transformer3Ws);
}

/** @return unmodifiable List of all switches in this grid */
public List<SwitchInput> getSwitches() {
return Collections.unmodifiableList(switches);
/** @return unmodifiable Set of all switches in this grid */
public Set<SwitchInput> getSwitches() {
return Collections.unmodifiableSet(switches);
}

public List<MeasurementUnitInput> getMeasurementUnits() {
return Collections.unmodifiableList(measurementUnits);
/** @return unmodifiable Set of all measurement units in this grid */
public Set<MeasurementUnitInput> getMeasurementUnits() {
return Collections.unmodifiableSet(measurementUnits);
}
}

0 comments on commit 64e7629

Please sign in to comment.