1/* 2 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24package com.sun.hotspot.igv.hierarchicallayout; 25 26import java.util.ArrayList; 27import java.util.Collections; 28import java.util.List; 29 30/** 31 * 32 * @author Thomas Wuerthinger 33 */ 34public class Node<N, E> { 35 36 private N data; 37 private List<Edge<N, E>> inEdges; 38 private List<Edge<N, E>> outEdges; 39 private boolean visited; 40 private boolean active; 41 private boolean reachable; 42 private Graph<N, E> graph; 43 44 protected boolean isVisited() { 45 return visited; 46 } 47 48 protected void setVisited(boolean b) { 49 visited = b; 50 } 51 52 protected boolean isReachable() { 53 return reachable; 54 } 55 56 protected void setReachable(boolean b) { 57 reachable = b; 58 } 59 60 protected boolean isActive() { 61 return active; 62 } 63 64 protected void setActive(boolean b) { 65 active = b; 66 } 67 68 public int getInDegree() { 69 return getInDegree(true); 70 } 71 72 public int getInDegree(boolean countSelfLoops) { 73 if (countSelfLoops) { 74 return inEdges.size(); 75 } else { 76 int cnt = 0; 77 for (Edge<N, E> e : inEdges) { 78 if (e.getSource() != this) { 79 cnt++; 80 } 81 } 82 return cnt; 83 } 84 } 85 86 public int getOutDegree() { 87 return outEdges.size(); 88 } 89 90 protected Node(Graph<N, E> graph, N data) { 91 setData(data); 92 this.graph = graph; 93 inEdges = new ArrayList<>(); 94 outEdges = new ArrayList<>(); 95 } 96 97 protected void addInEdge(Edge<N, E> e) { 98 inEdges.add(e); 99 } 100 101 public Graph<N, E> getGraph() { 102 return graph; 103 } 104 105 protected void addOutEdge(Edge<N, E> e) { 106 outEdges.add(e); 107 } 108 109 protected void removeInEdge(Edge<N, E> e) { 110 //assert inEdges.contains(e); 111 inEdges.remove(e); 112 } 113 114 protected void removeOutEdge(Edge<N, E> e) { 115 //assert outEdges.contains(e); 116 outEdges.remove(e); 117 } 118 119 public List<Edge<N, E>> getInEdges() { 120 return Collections.unmodifiableList(inEdges); 121 } 122 123 public List<Edge<N, E>> getOutEdges() { 124 return Collections.unmodifiableList(outEdges); 125 } 126 127 public List<Node<N, E>> getSuccessors() { 128 ArrayList<Node<N, E>> succ = new ArrayList<>(); 129 for (Edge<N, E> e : getOutEdges()) { 130 Node<N, E> n = e.getDest(); 131 if (!succ.contains(n)) { 132 succ.add(n); 133 } 134 } 135 return succ; 136 } 137 138 public List<Node<N, E>> getPredecessors() { 139 ArrayList<Node<N, E>> pred = new ArrayList<>(); 140 for (Edge<N, E> e : getInEdges()) { 141 Node<N, E> n = e.getSource(); 142 if (!pred.contains(n)) { 143 pred.add(n); 144 } 145 } 146 return pred; 147 } 148 149 public N getData() { 150 return data; 151 } 152 153 public void setData(N d) { 154 data = d; 155 } 156 157 @Override 158 public String toString() { 159 return "Node: " + data; 160 } 161} 162