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  */
  24 package com.sun.hotspot.igv.data;
  25 
  26 import java.util.*;
  27 
  28 /**
  29  *
  30  * @author Thomas Wuerthinger
  31  */
  32 public class InputBlock {
  33 
  34     private List<InputNode> nodes;
  35     private String name;
  36     private InputGraph graph;
  37     private Set<InputBlock> successors;
  38 
  39     @Override
  40     public int hashCode() {
  41         return name.hashCode();
  42     }
  43 
  44     @Override
  45     public boolean equals(Object o) {
  46 
  47         if (o == this) {
  48             return true;
  49         }
  50 
  51         if (o == null || (!(o instanceof InputBlock))) {
  52             return false;
  53         }
  54 
  55         final InputBlock b = (InputBlock)o;
  56         final boolean result = b.nodes.equals(nodes) && b.name.equals(name) && b.successors.size() == successors.size();
  57         if (!result) {
  58             return false;
  59         }
  60 
  61         final HashSet<String> s = new HashSet<>();
  62         for (InputBlock succ : successors) {
  63             s.add(succ.name);
  64         }
  65 
  66         for (InputBlock succ : b.successors) {
  67             if (!s.contains(succ.name)) {
  68                 return false;
  69             }
  70         }
  71 
  72         return true;
  73     }
  74 
  75     InputBlock(InputGraph graph, String name) {
  76         this.graph = graph;
  77         this.name = name;
  78         nodes = new ArrayList<>();
  79         successors = new LinkedHashSet<>(2);
  80     }
  81 
  82     public String getName() {
  83         return name;
  84     }
  85 
  86     public List<InputNode> getNodes() {
  87         return Collections.unmodifiableList(nodes);
  88     }
  89 
  90     public void addNode(int id) {
  91         InputNode node = graph.getNode(id);
  92         assert node != null;
  93         assert !nodes.contains(node) : "duplicate : " + node;
  94         graph.setBlock(node, this);
  95         nodes.add(node);
  96     }
  97 
  98     public Set<InputBlock> getSuccessors() {
  99         return Collections.unmodifiableSet(successors);
 100     }
 101 
 102     @Override
 103     public String toString() {
 104         return "Block " + this.getName();
 105     }
 106 
 107     void addSuccessor(InputBlock b) {
 108         if (!successors.contains(b)) {
 109             successors.add(b);
 110         }
 111     }
 112 }