1 /*
   2  * Copyright (c) 2014, 2018, 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 
  25 package org.graalvm.compiler.graph;
  26 
  27 import static org.graalvm.compiler.graph.Graph.isModificationCountsEnabled;
  28 
  29 import java.util.Iterator;
  30 
  31 import org.graalvm.compiler.graph.iterators.NodeIterable;
  32 
  33 class NodeUsageIterable implements NodeIterable<Node> {
  34 
  35     private final Node node;
  36 
  37     NodeUsageIterable(Node node) {
  38         this.node = node;
  39     }
  40 
  41     @Override
  42     public NodeUsageIterator iterator() {
  43         if (isModificationCountsEnabled()) {
  44             return new NodeUsageWithModCountIterator(node);
  45         } else {
  46             return new NodeUsageIterator(node);
  47         }
  48     }
  49 
  50     @Override
  51     public Node first() {
  52         return node.usage0;
  53     }
  54 
  55     @Override
  56     public boolean isEmpty() {
  57         return node.usage0 == null;
  58     }
  59 
  60     @Override
  61     public boolean isNotEmpty() {
  62         return node.usage0 != null;
  63     }
  64 
  65     @Override
  66     public int count() {
  67         return node.getUsageCount();
  68     }
  69 
  70     @Override
  71     public String toString() {
  72         StringBuilder sb = new StringBuilder();
  73         Iterator<Node> iterator = iterator();
  74         boolean first = true;
  75         sb.append("usages=");
  76         sb.append('[');
  77         while (iterator.hasNext()) {
  78             Node input = iterator.next();
  79             if (!first) {
  80                 sb.append(", ");
  81             }
  82             sb.append(input);
  83             first = false;
  84         }
  85         sb.append(']');
  86         return sb.toString();
  87     }
  88 }