Skip to content

Commit f725775

Browse files
committed
refactor
1 parent 81601a1 commit f725775

File tree

5 files changed

+33
-49
lines changed

5 files changed

+33
-49
lines changed

com.avaloq.tools.ddk.xtext.expression/META-INF/MANIFEST.MF

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ Require-Bundle: org.eclipse.xtext,
2323
org.eclipse.jdt.launching,
2424
org.eclipse.core.resources,
2525
org.eclipse.core.runtime,
26-
org.eclipse.xtext.xbase.lib;bundle-version="2.14.0"
26+
org.eclipse.xtext.xbase.lib,
27+
com.avaloq.tools.ddk
2728
Import-Package: org.apache.logging.log4j,
2829
org.apache.log4j
2930
Export-Package: com.avaloq.tools.ddk.xtext.expression,

com.avaloq.tools.ddk.xtext.expression/src/com/avaloq/tools/ddk/xtext/expression/generator/EClassComparator.java

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.eclipse.emf.ecore.EPackage;
2020
import org.eclipse.emf.ecore.EcorePackage;
2121

22+
import com.avaloq.tools.ddk.util.Graph;
2223
import com.google.common.base.Function;
2324
import com.google.common.base.Predicate;
2425
import com.google.common.base.Predicates;

com.avaloq.tools.ddk.xtext.generator.test/META-INF/MANIFEST.MF

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Bundle-Vendor: Avaloq Group AG
77
Bundle-RequiredExecutionEnvironment: JavaSE-17
88
Bundle-ActivationPolicy: lazy
99
Fragment-Host: com.avaloq.tools.ddk.xtext.generator
10-
Require-Bundle: com.avaloq.tools.ddk.test.core,
10+
Require-Bundle: com.avaloq.tools.ddk.test.core,
1111
com.avaloq.tools.ddk.xtext.expression,
1212
com.avaloq.tools.ddk.xtext.test.core,
1313
org.eclipse.xtend,
@@ -16,7 +16,8 @@ Require-Bundle: com.avaloq.tools.ddk.test.core,
1616
org.junit,
1717
org.mockito,
1818
com.avaloq.tools.ddk.xtext.ui,
19-
org.eclipse.xtext.xtext.generator
19+
org.eclipse.xtext.xtext.generator,
20+
com.avaloq.tools.ddk
2021
Export-Package: com.avaloq.tools.ddk.xtext.generator.test.generator
2122
Import-Package: org.eclipse.xtext.ui.resource
2223
Automatic-Module-Name: com.avaloq.tools.ddk.xtext.generator.test

com.avaloq.tools.ddk.xtext.generator.test/src/com/avaloq/tools/ddk/xtext/generator/test/util/GraphTest.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,28 @@
1818

1919
import org.junit.Test;
2020

21-
import com.avaloq.tools.ddk.xtext.expression.generator.Graph;
21+
import com.avaloq.tools.ddk.util.Graph;
2222
import com.google.common.collect.HashMultimap;
2323
import com.google.common.collect.ImmutableMultimap;
2424
import com.google.common.collect.Multimap;
2525

2626

27-
@SuppressWarnings("PMD.JUnitAssertionsShouldIncludeMessage")
27+
@SuppressWarnings({"nls", "PMD.JUnitAssertionsShouldIncludeMessage"})
2828
public class GraphTest {
2929
// CHECKSTYLE:CONSTANTS-OFF
3030

31+
private static Graph<String> create(final Multimap<String, String> graph) {
32+
Graph<String> g = new Graph<String>();
33+
for (Map.Entry<String, String> entry : graph.entries()) {
34+
String from = entry.getKey();
35+
String to = entry.getValue();
36+
g.addNode(from);
37+
g.addNode(to);
38+
g.addEdge(from, to);
39+
}
40+
return g;
41+
}
42+
3143
@Test
3244
public void testTopologicalSorting() {
3345
Multimap<String, String> graph = HashMultimap.create();
@@ -42,7 +54,7 @@ public void testTopologicalSorting() {
4254
graph.put("8", "9");
4355
graph.put("8", "10");
4456

45-
List<String> sorted = Graph.create(graph).sort();
57+
List<String> sorted = create(graph).sort();
4658
assertSorting(graph, sorted);
4759
}
4860

@@ -51,7 +63,7 @@ public void testDependencyCycle() {
5163
Multimap<String, String> graph = ImmutableMultimap.of("1", "2", "2", "3", "3", "1");
5264

5365
try {
54-
Graph.create(graph).sort();
66+
create(graph).sort();
5567
fail();
5668
} catch (IllegalStateException e) {
5769
assertTrue(true); // NOPMD
+11-42
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,16 @@
88
* Contributors:
99
* Avaloq Group AG - initial API and implementation
1010
*******************************************************************************/
11-
package com.avaloq.tools.ddk.xtext.expression.generator;
11+
package com.avaloq.tools.ddk.util;
1212

13+
import java.util.ArrayList;
1314
import java.util.Iterator;
15+
import java.util.LinkedHashMap;
16+
import java.util.LinkedHashSet;
1417
import java.util.List;
1518
import java.util.Map;
1619
import java.util.Set;
1720

18-
import com.google.common.base.Function;
19-
import com.google.common.collect.Lists;
20-
import com.google.common.collect.Maps;
21-
import com.google.common.collect.Multimap;
22-
import com.google.common.collect.Sets;
23-
2421

2522
/**
2623
* A generic graph which can be topologically sorted.
@@ -38,8 +35,8 @@ public class Graph<T> {
3835
*/
3936
private static class Node<T> {
4037
private final T ref;
41-
private final Set<Node<T>> inEdges = Sets.newLinkedHashSet();
42-
private final Set<Node<T>> outEdges = Sets.newLinkedHashSet();
38+
private final Set<Node<T>> inEdges = new LinkedHashSet<>();
39+
private final Set<Node<T>> outEdges = new LinkedHashSet<>();
4340

4441
Node(final T ref) {
4542
this.ref = ref;
@@ -64,7 +61,7 @@ public String toString() {
6461
}
6562
}
6663

67-
private final Map<T, Node<T>> nodes = Maps.newLinkedHashMap();
64+
private final Map<T, Node<T>> nodes = new LinkedHashMap<>();
6865

6966
/**
7067
* Helper method to create a new graph.
@@ -83,27 +80,6 @@ public static <T> Graph<T> create(final Iterable<T> nodes) {
8380
return g;
8481
}
8582

86-
/**
87-
* Helper method to create a new graph.
88-
*
89-
* @param <T>
90-
* node type
91-
* @param graph
92-
* graph as multiset where values represent targets of edges with key as source
93-
* @return graph
94-
*/
95-
public static <T> Graph<T> create(final Multimap<T, T> graph) {
96-
Graph<T> g = new Graph<T>();
97-
for (Map.Entry<T, T> entry : graph.entries()) {
98-
T from = entry.getKey();
99-
T to = entry.getValue();
100-
g.addNode(from);
101-
g.addNode(to);
102-
g.addEdge(from, to);
103-
}
104-
return g;
105-
}
106-
10783
/**
10884
* Adds a node to this graph.
10985
*
@@ -140,13 +116,11 @@ public Graph<T> addEdge(final T from, final T to) {
140116
* @return sorted graph
141117
*/
142118
public List<T> sort() {
143-
// TODO try to sort to something as close as possible to the original
144-
145119
// L <- Empty list that will contain the sorted elements
146-
List<Node<T>> l = Lists.newArrayList();
120+
List<Node<T>> l = new ArrayList<>();
147121

148122
// S <- Set of all nodes with no incoming edges
149-
Set<Node<T>> s = Sets.newLinkedHashSet();
123+
Set<Node<T>> s = new LinkedHashSet<>();
150124
for (Node<T> n : nodes.values()) {
151125
if (n.inEdges.isEmpty()) {
152126
s.add(n);
@@ -180,16 +154,11 @@ public List<T> sort() {
180154
// Check to see if all edges are removed
181155
for (Node<T> n : nodes.values()) {
182156
if (!n.inEdges.isEmpty()) {
183-
throw new IllegalStateException("Cycle present, topological sort not possible: " + n.ref + " -> " + n.inEdges);
157+
throw new IllegalStateException("Cycle present, topological sort not possible: " + n.ref + " -> " + n.inEdges); //$NON-NLS-1$ //$NON-NLS-2$
184158
}
185159
}
186160

187-
return Lists.transform(l, new Function<Node<T>, T>() {
188-
@Override
189-
public T apply(final Node<T> from) {
190-
return from.ref;
191-
}
192-
});
161+
return l.stream().map(f -> f.ref).toList();
193162
}
194163

195164
}

0 commit comments

Comments
 (0)