Skip to content

Commit 6bea69c

Browse files
authored
refactor: use more java 17 features (#130)
This is the first redesign. Other improvements remain to be made and could be made in the future, for example about : - switch - record - stream API
1 parent a04ae29 commit 6bea69c

17 files changed

+48
-77
lines changed

java/src/main/java/io/process/analytics/tools/bpmn/generator/App.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.File;
2222
import java.nio.file.Files;
2323
import java.nio.file.NoSuchFileException;
24-
import java.nio.file.Path;
2524
import java.util.concurrent.Callable;
2625

2726
import io.process.analytics.tools.bpmn.generator.BPMNLayoutGenerator.ExportType;
@@ -54,7 +53,6 @@ public class App implements Callable<Integer> {
5453
private File[] inputFiles;
5554

5655
public static void main(String[] args) throws Exception {
57-
5856
int exitCode = runApp(args);
5957
System.exit(exitCode);
6058
}

java/src/main/java/io/process/analytics/tools/bpmn/generator/BPMNLayoutGenerator.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
import static io.process.analytics.tools.bpmn.generator.export.BPMNExporter.defaultBpmnExporter;
44
import static io.process.analytics.tools.bpmn.generator.internal.BpmnInOut.defaultBpmnInOut;
55

6-
import java.io.File;
7-
import java.io.IOException;
8-
96
import io.process.analytics.tools.bpmn.generator.algo.ShapeLayouter;
107
import io.process.analytics.tools.bpmn.generator.algo.ShapeSorter;
118
import io.process.analytics.tools.bpmn.generator.converter.BpmnToAlgoModelConverter;
129
import io.process.analytics.tools.bpmn.generator.export.ASCIIExporter;
1310
import io.process.analytics.tools.bpmn.generator.export.SVGExporter;
1411
import io.process.analytics.tools.bpmn.generator.input.CSVtoBPMN;
1512
import io.process.analytics.tools.bpmn.generator.internal.BpmnInOut;
16-
import io.process.analytics.tools.bpmn.generator.internal.FileUtils;
1713
import io.process.analytics.tools.bpmn.generator.internal.generated.model.TDefinitions;
1814
import io.process.analytics.tools.bpmn.generator.model.Diagram;
1915
import io.process.analytics.tools.bpmn.generator.model.Grid;
@@ -93,20 +89,17 @@ private String exportToSvg(LayoutSortedDiagram diagram) {
9389

9490

9591
private String export(LayoutSortedDiagram layout, ExportType exportType) {
96-
switch (exportType) {
97-
case ASCII:
98-
return exportToAscii(layout);
99-
case BPMN:
100-
return exportToBpmn(layout);
101-
case SVG:
102-
return exportToSvg(layout);
103-
default:
104-
throw new IllegalStateException("Unexpected Export Type: " + exportType);
105-
}
92+
return switch (exportType) {
93+
case ASCII -> exportToAscii(layout);
94+
case BPMN -> exportToBpmn(layout);
95+
case SVG -> exportToSvg(layout);
96+
default -> throw new IllegalStateException("Unexpected Export Type: " + exportType);
97+
};
10698
}
10799

108100
@RequiredArgsConstructor
109101
@Getter
102+
// TODO switch to record
110103
public static class LayoutSortedDiagram {
111104

112105
private final TDefinitions originalDefinitions;

java/src/main/java/io/process/analytics/tools/bpmn/generator/algo/ShapeLayouter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static io.process.analytics.tools.bpmn.generator.model.Position.position;
55

66
import java.util.List;
7-
import java.util.stream.Collectors;
87

98
import io.process.analytics.tools.bpmn.generator.model.Edge;
109
import io.process.analytics.tools.bpmn.generator.model.Grid;
@@ -22,6 +21,7 @@ public Grid layout(Diagram diagram) {
2221
for (Shape shape : diagram.getShapes()) {
2322
Position positionOfCurrentShape = positionShape(diagram, grid, shape);
2423
putOnGrid(grid, positionOfCurrentShape);
24+
// TODO check usage of supplier, intellij says it is deprecated
2525
log.debug("Adding {}:\n{}", shape::getName, () -> toAscii(grid));
2626
addRowsWhenShapeIsASplit(diagram, grid, shape, positionOfCurrentShape);
2727
}
@@ -76,8 +76,8 @@ private Position positionShape(Diagram diagram, Grid grid, Shape shape) {
7676
private void compactGrid(Grid grid) {
7777
int i = 0;
7878
while (i < grid.getLastRowIndex()) {
79-
List<Integer> currentRow = grid.getRow(i).stream().map(Position::getX).collect(Collectors.toList());
80-
List<Integer> nextRow = grid.getRow(i + 1).stream().map(Position::getX).collect(Collectors.toList());
79+
List<Integer> currentRow = grid.getRow(i).stream().map(Position::getX).toList();
80+
List<Integer> nextRow = grid.getRow(i + 1).stream().map(Position::getX).toList();
8181

8282
boolean currentRowCanBeMovedBelow = true;
8383
for (Integer shapeIndexInCurrentRow : currentRow) {
@@ -109,7 +109,7 @@ private Position addStartShape(Grid grid, Shape shape) {
109109
private Position addSplit(Grid grid, Shape shape, String previousShapeID, List<Edge> outgoingEdgesOfPreviousShape) {
110110
Position previousShapePosition = grid.getPosition(previousShapeID);
111111
int numberOfShapesInTheSplit = outgoingEdgesOfPreviousShape.size();
112-
int indexOfCurrentShape = outgoingEdgesOfPreviousShape.stream().map(Edge::getTo).collect(Collectors.toList()).indexOf(shape.getId());
112+
int indexOfCurrentShape = outgoingEdgesOfPreviousShape.stream().map(Edge::getTo).toList().indexOf(shape.getId());
113113
//put element right to the split vertically distributed according to the index
114114
int relativeYPosition;
115115
if (numberOfShapesInTheSplit % 2 == 0 && indexOfCurrentShape >= numberOfShapesInTheSplit / 2) {
@@ -124,7 +124,7 @@ private Position addSplit(Grid grid, Shape shape, String previousShapeID, List<E
124124
private Position addJoin(Grid grid, Shape shape, List<Edge> incomingEdges) {
125125
//first implementation: middle of elements it joins
126126
// later we should also try yo find the split to align it to that if possible
127-
List<Position> positions = incomingEdges.stream().map(Edge::getFrom).map(grid::getPosition).collect(Collectors.toList());
127+
List<Position> positions = incomingEdges.stream().map(Edge::getFrom).map(grid::getPosition).toList();
128128
int xMax = positions.stream().map(Position::getX).reduce(0, Math::max);
129129
int yMax = positions.stream().map(Position::getY).reduce(0, Math::max);
130130
int yMin = positions.stream().map(Position::getY).reduce(Integer.MAX_VALUE, Math::min);

java/src/main/java/io/process/analytics/tools/bpmn/generator/algo/ShapeSorter.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
*/
3636
public class ShapeSorter {
3737

38-
3938
/**
4039
* sort nodes of a diagram in topological order
4140
*
@@ -78,12 +77,9 @@ private boolean isAJoinInOriginalDiagram(Diagram diagram, Shape currentElement)
7877
}
7978

8079
private Diagram doSort(Diagram diagram) {
81-
List<Shape> shapeToSort = new ArrayList<>();
82-
List<Edge> remainingEdges = new ArrayList<>();
83-
List<Edge> finalEdges = new ArrayList<>();
84-
shapeToSort.addAll(diagram.getShapes());
85-
remainingEdges.addAll(diagram.getEdges());
86-
finalEdges.addAll(diagram.getEdges());
80+
List<Shape> shapeToSort = new ArrayList<>(diagram.getShapes());
81+
List<Edge> remainingEdges = new ArrayList<>(diagram.getEdges());
82+
List<Edge> finalEdges = new ArrayList<>(diagram.getEdges());
8783
List<Join> joins = findAllJoins(shapeToSort, remainingEdges);
8884

8985
Diagram.DiagramBuilder sortedDiagram = Diagram.builder();

java/src/main/java/io/process/analytics/tools/bpmn/generator/input/CSVtoBPMN.java

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,37 +61,7 @@ private List<TFlowNode> getFlowNodeElements(String nodes) {
6161
continue;
6262
}
6363
String[] node = line.split(",");
64-
String type = removeEnclosingDoubleQuote(node[3]);
65-
TFlowNode flowNode;
66-
switch (type) {
67-
case "start_event":
68-
flowNode = new TStartEvent();
69-
break;
70-
case "end_event":
71-
flowNode = new TEndEvent();
72-
break;
73-
case "gateway":
74-
flowNode = new TParallelGateway();
75-
break;
76-
case "parallel_gateway":
77-
flowNode = new TParallelGateway();
78-
break;
79-
case "exclusive_gateway":
80-
flowNode = new TExclusiveGateway();
81-
break;
82-
case "inclusive_gateway":
83-
flowNode = new TInclusiveGateway();
84-
break;
85-
case "user_task":
86-
flowNode = new TUserTask();
87-
break;
88-
case "service_task":
89-
flowNode = new TServiceTask();
90-
break;
91-
case "task":
92-
default:
93-
flowNode = new TTask();
94-
}
64+
TFlowNode flowNode = gettFlowNode(node);
9565
flowNode.setName(removeEnclosingDoubleQuote(node[2]));
9666

9767
String originalId = node[1];
@@ -107,6 +77,21 @@ private List<TFlowNode> getFlowNodeElements(String nodes) {
10777
return flowElements;
10878
}
10979

80+
private static TFlowNode gettFlowNode(String[] node) {
81+
String type = removeEnclosingDoubleQuote(node[3]);
82+
TFlowNode flowNode = switch (type) {
83+
case "start_event" -> new TStartEvent();
84+
case "end_event" -> new TEndEvent();
85+
case "gateway", "parallel_gateway" -> new TParallelGateway();
86+
case "exclusive_gateway" -> new TExclusiveGateway();
87+
case "inclusive_gateway" -> new TInclusiveGateway();
88+
case "user_task" -> new TUserTask();
89+
case "service_task" -> new TServiceTask();
90+
default -> new TTask();
91+
};
92+
return flowNode;
93+
}
94+
11095
private void assignIncomingAndOutgoingReferences(List<TFlowNode> flowNodeElements) {
11196
for (TFlowNode flowNode : flowNodeElements) {
11297
EdgeRelation edgeRelation = this.shapeRelations.get(flowNode.getId());
@@ -191,7 +176,7 @@ private static boolean isNumeric(String s) {
191176
private static class EdgeRelation {
192177

193178
public final List<String> incoming = new ArrayList<>();
194-
public final List<String> outgoing = new ArrayList<>();;
179+
public final List<String> outgoing = new ArrayList<>();
195180

196181
}
197182

java/src/main/java/io/process/analytics/tools/bpmn/generator/internal/BpmnInOut.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package io.process.analytics.tools.bpmn.generator.internal;
1717

18-
import static io.process.analytics.tools.bpmn.generator.internal.FileUtils.createParents;
1918
import static io.process.analytics.tools.bpmn.generator.internal.FileUtils.fileContent;
2019

2120
import java.io.File;

java/src/main/java/io/process/analytics/tools/bpmn/generator/internal/Semantic.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
* Helper to access to the BPMN semantic part
3232
*/
3333
@RequiredArgsConstructor
34+
// TODO switch to record
35+
// TODO lombok getter on class
3436
public class Semantic {
3537

3638
@NonNull
@@ -42,18 +44,12 @@ public static String getId(Object object) {
4244
return ((TBaseElement) object).getId();
4345
}
4446

45-
public List<TParticipant> getParticipants() {
46-
return getCollaboration()
47-
.map(TCollaboration::getParticipant)
48-
.orElseGet(Collections::emptyList);
49-
}
50-
5147
public Optional<TCollaboration> getCollaboration() {
5248
List<TCollaboration> collaborations = definitions.getRootElement().stream()
5349
.map(JAXBElement::getValue)
5450
.filter(TCollaboration.class::isInstance)
5551
.map(TCollaboration.class::cast)
56-
.collect(Collectors.toList());
52+
.toList();
5753

5854
// TODO check at most 1 otherwise error
5955
// TODO refactor into a more functional way
@@ -106,9 +102,7 @@ public void add(TProcess process) {
106102

107103
public static void addFlowNodes(TProcess process, Collection<TFlowNode> flowElements) {
108104
flowElements.stream()
109-
.map(f -> {
110-
return new JAXBElement<>(bpmnElementQName(f), TFlowNode.class, null, f);
111-
})
105+
.map(f -> new JAXBElement<>(bpmnElementQName(f), TFlowNode.class, null, f))
112106
.forEach(f -> process.getFlowElement().add(f));
113107
}
114108

@@ -123,7 +117,7 @@ private static QName bpmnElementQName(String bpmnElement) {
123117
}
124118

125119
//TODO add other type of flow node elements
126-
private static final Map<Class<? extends TFlowNode>, String> bpmnElementBindings = new HashMap<Class<? extends TFlowNode>, String>() {{
120+
private static final Map<Class<? extends TFlowNode>, String> bpmnElementBindings = new HashMap<>() {{
127121
put(TParallelGateway.class, "parallelGateway");
128122
put(TInclusiveGateway.class, "inclusiveGateway");
129123
put(TExclusiveGateway.class, "exclusiveGateway");

java/src/main/java/io/process/analytics/tools/bpmn/generator/internal/XmlParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package io.process.analytics.tools.bpmn.generator.internal;
1717

18-
import java.io.File;
1918
import java.io.StringReader;
2019
import java.io.StringWriter;
2120

@@ -56,6 +55,7 @@ private Marshaller createMarshaller() throws JAXBException {
5655
} catch(PropertyException e) {
5756
// In case another JAXB implementation is used
5857
// do not stop processing, namespace prefixes will be generated automatically in that case
58+
// TODO switch to logger
5959
e.printStackTrace();
6060
}
6161
return marshaller;

java/src/main/java/io/process/analytics/tools/bpmn/generator/model/Grid.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package io.process.analytics.tools.bpmn.generator.model;
22

33
import java.util.ArrayList;
4-
import java.util.Arrays;
54
import java.util.List;
65
import java.util.stream.Collectors;
76

87
/**
98
*
10-
* Represent a grid with coordinate as follow
9+
* Represent a grid with coordinate as follows
1110
*
1211
* <pre>
1312
* + ➞ x
1413
* ↓
1514
* y
1615
* </pre>
1716
*/
17+
// TODO use lombok getter?
1818
public class Grid {
1919

2020
private final List<Position> positions = new ArrayList<>();

java/src/main/java/io/process/analytics/tools/bpmn/generator/model/Shape.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
@Data
2525
@Builder(toBuilder = true)
2626
@RequiredArgsConstructor
27+
// TODO switch to record
2728
public class Shape {
2829

2930
private final String id; // the bpmnElement id

java/src/main/java/io/process/analytics/tools/bpmn/generator/model/ShapeType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
package io.process.analytics.tools.bpmn.generator.model;
1717

1818
public enum ShapeType {
19-
ACTIVITY, EVENT, GATEWAY;
19+
ACTIVITY, EVENT, GATEWAY
2020
}

java/src/main/java/io/process/analytics/tools/bpmn/generator/model/display/DisplayDimension.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import lombok.RequiredArgsConstructor;
1616

1717
@RequiredArgsConstructor
18+
// TODO switch to record
1819
public class DisplayDimension {
1920

2021
public final int x;

java/src/main/java/io/process/analytics/tools/bpmn/generator/model/display/DisplayEdge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
*/
1313
package io.process.analytics.tools.bpmn.generator.model.display;
1414

15-
import io.process.analytics.tools.bpmn.generator.model.display.DisplayPoint;
1615
import lombok.Builder;
1716
import lombok.RequiredArgsConstructor;
1817

1918
import java.util.List;
2019

2120
@RequiredArgsConstructor
2221
@Builder
22+
// TODO switch to record
2323
public class DisplayEdge {
2424

2525
public final String bpmnElementId;

java/src/main/java/io/process/analytics/tools/bpmn/generator/model/display/DisplayFlowNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
@RequiredArgsConstructor
2020
@Builder
21+
// TODO switch to record
2122
public class DisplayFlowNode {
2223

2324
public final String bpmnElementId;

java/src/main/java/io/process/analytics/tools/bpmn/generator/model/display/DisplayLabel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import lombok.RequiredArgsConstructor;
1616

1717
@RequiredArgsConstructor
18+
// TODO switch to record
1819
public class DisplayLabel {
1920

2021
public final String text; // for non BPMN exporters only

java/src/main/java/io/process/analytics/tools/bpmn/generator/model/display/DisplayModel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
@RequiredArgsConstructor
2222
@Builder
23+
// TODO switch to record
2324
public class DisplayModel {
2425
public final int width;
2526
public final int height;

java/src/main/java/io/process/analytics/tools/bpmn/generator/model/display/DisplayPoint.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
@RequiredArgsConstructor
1919
@ToString
20+
// TODO switch to record
2021
public class DisplayPoint {
2122

2223
public final int x;

0 commit comments

Comments
 (0)