8
8
* Contributors:
9
9
* Avaloq Group AG - initial API and implementation
10
10
*******************************************************************************/
11
- package com .avaloq .tools .ddk .xtext . expression . generator ;
11
+ package com .avaloq .tools .ddk .util ;
12
12
13
+ import java .util .ArrayList ;
13
14
import java .util .Iterator ;
15
+ import java .util .LinkedHashMap ;
16
+ import java .util .LinkedHashSet ;
14
17
import java .util .List ;
15
18
import java .util .Map ;
16
19
import java .util .Set ;
17
20
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
-
24
21
25
22
/**
26
23
* A generic graph which can be topologically sorted.
@@ -38,8 +35,8 @@ public class Graph<T> {
38
35
*/
39
36
private static class Node <T > {
40
37
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 <> ();
43
40
44
41
Node (final T ref ) {
45
42
this .ref = ref ;
@@ -64,7 +61,7 @@ public String toString() {
64
61
}
65
62
}
66
63
67
- private final Map <T , Node <T >> nodes = Maps . newLinkedHashMap ();
64
+ private final Map <T , Node <T >> nodes = new LinkedHashMap <> ();
68
65
69
66
/**
70
67
* Helper method to create a new graph.
@@ -83,27 +80,6 @@ public static <T> Graph<T> create(final Iterable<T> nodes) {
83
80
return g ;
84
81
}
85
82
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
-
107
83
/**
108
84
* Adds a node to this graph.
109
85
*
@@ -140,13 +116,11 @@ public Graph<T> addEdge(final T from, final T to) {
140
116
* @return sorted graph
141
117
*/
142
118
public List <T > sort () {
143
- // TODO try to sort to something as close as possible to the original
144
-
145
119
// L <- Empty list that will contain the sorted elements
146
- List <Node <T >> l = Lists . newArrayList ();
120
+ List <Node <T >> l = new ArrayList <> ();
147
121
148
122
// S <- Set of all nodes with no incoming edges
149
- Set <Node <T >> s = Sets . newLinkedHashSet ();
123
+ Set <Node <T >> s = new LinkedHashSet <> ();
150
124
for (Node <T > n : nodes .values ()) {
151
125
if (n .inEdges .isEmpty ()) {
152
126
s .add (n );
@@ -180,16 +154,11 @@ public List<T> sort() {
180
154
// Check to see if all edges are removed
181
155
for (Node <T > n : nodes .values ()) {
182
156
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$
184
158
}
185
159
}
186
160
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 ();
193
162
}
194
163
195
164
}
0 commit comments