View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.LinkedHashMap;
26  import java.util.LinkedList;
27  import java.util.List;
28  import java.util.Map;
29  
30  class Graph {
31      private enum DfsState {
32          VISITING,
33          VISITED
34      }
35  
36      final Map<String, Vertex> vertices = new LinkedHashMap<>();
37  
38      public Vertex getVertex(String id) {
39          return vertices.get(id);
40      }
41  
42      public Collection<Vertex> getVertices() {
43          return vertices.values();
44      }
45  
46      Vertex addVertex(String label) {
47          return vertices.computeIfAbsent(label, Vertex::new);
48      }
49  
50      void addEdge(Vertex from, Vertex to) throws CycleDetectedException {
51          from.children.add(to);
52          to.parents.add(from);
53          List<String> cycle = findCycle(to);
54          if (cycle != null) {
55              // remove edge which introduced cycle
56              removeEdge(from, to);
57              throw new CycleDetectedException(
58                      "Edge between '" + from.label + "' and '" + to.label + "' introduces to cycle in the graph", cycle);
59          }
60      }
61  
62      void removeEdge(Vertex from, Vertex to) {
63          from.children.remove(to);
64          to.parents.remove(from);
65      }
66  
67      List<String> visitAll() {
68          return visitAll(vertices.values(), new HashMap<>(), new ArrayList<>());
69      }
70  
71      List<String> findCycle(Vertex vertex) {
72          return visitCycle(Collections.singleton(vertex), new HashMap<>(), new LinkedList<>());
73      }
74  
75      private static List<String> visitAll(
76              Collection<Vertex> children, Map<Vertex, DfsState> stateMap, List<String> list) {
77          for (Vertex v : children) {
78              DfsState state = stateMap.putIfAbsent(v, DfsState.VISITING);
79              if (state == null) {
80                  visitAll(v.children, stateMap, list);
81                  stateMap.put(v, DfsState.VISITED);
82                  list.add(v.label);
83              }
84          }
85          return list;
86      }
87  
88      private static List<String> visitCycle(
89              Collection<Vertex> children, Map<Vertex, DfsState> stateMap, LinkedList<String> cycle) {
90          for (Vertex v : children) {
91              DfsState state = stateMap.putIfAbsent(v, DfsState.VISITING);
92              if (state == null) {
93                  cycle.addLast(v.label);
94                  List<String> ret = visitCycle(v.children, stateMap, cycle);
95                  if (ret != null) {
96                      return ret;
97                  }
98                  cycle.removeLast();
99                  stateMap.put(v, DfsState.VISITED);
100             } else if (state == DfsState.VISITING) {
101                 // we are already visiting this vertex, this mean we have a cycle
102                 int pos = cycle.lastIndexOf(v.label);
103                 List<String> ret = cycle.subList(pos, cycle.size());
104                 ret.add(v.label);
105                 return ret;
106             }
107         }
108         return null;
109     }
110 
111     static class Vertex {
112         final String label;
113         final List<Vertex> children = new ArrayList<>();
114         final List<Vertex> parents = new ArrayList<>();
115 
116         Vertex(String label) {
117             this.label = label;
118         }
119 
120         String getLabel() {
121             return label;
122         }
123 
124         List<Vertex> getChildren() {
125             return children;
126         }
127 
128         List<Vertex> getParents() {
129             return parents;
130         }
131     }
132 }