blob: 58fc6aeacd8a98c9b665c0af6bb54cff575292f5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.di.componentgraph.cycle;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
/**
* Class representing a directed graph.
*
* @author gjoranv
*/
public class Graph<T> {
private final Map<T, LinkedHashSet<T>> adjMap = new LinkedHashMap<>();
public void edge(T from, T to) {
if (from == null || to == null)
throw new IllegalArgumentException("Null vertices are not allowed, edge: " + from + "->" + to);
adjMap.computeIfAbsent(from, k -> new LinkedHashSet<>()).add(to);
adjMap.computeIfAbsent(to, k -> new LinkedHashSet<>());
}
Set<T> getVertices() {
return adjMap.keySet();
}
/**
* Returns the outgoing edges of the given vertex.
*/
Set<T> getAdjacent(T vertex) {
return adjMap.get(vertex);
}
private void throwIfMissingVertex(T vertex) {
if (! adjMap.containsKey(vertex)) throw new IllegalArgumentException("No such vertex in the graph: " + vertex);
}
}
|