1 /*
   2  * Copyright (c) 2011, 2012, 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.iterators;
  26 
  27 import java.util.ArrayList;
  28 import java.util.Collection;
  29 import java.util.Iterator;
  30 import java.util.List;
  31 
  32 import org.graalvm.compiler.graph.Node;
  33 
  34 public interface NodeIterable<T extends Node> extends Iterable<T> {
  35 
  36     @SuppressWarnings("unchecked")
  37     default <F extends T> NodeIterable<F> filter(Class<F> clazz) {
  38         return (NodeIterable<F>) new FilteredNodeIterable<>(this).and(NodePredicates.isA(clazz));
  39     }
  40 
  41     default FilteredNodeIterable<T> filter(NodePredicate predicate) {
  42         return new FilteredNodeIterable<>(this).and(predicate);
  43     }
  44 
  45     default List<T> snapshot() {
  46         ArrayList<T> list = new ArrayList<>();
  47         snapshotTo(list);
  48         return list;
  49     }
  50 
  51     default void snapshotTo(Collection<? super T> to) {
  52         for (T n : this) {
  53             to.add(n);
  54         }
  55     }
  56 
  57     default T first() {
  58         Iterator<T> iterator = iterator();
  59         if (iterator.hasNext()) {
  60             return iterator.next();
  61         }
  62         return null;
  63     }
  64 
  65     default int count() {
  66         int count = 0;
  67         Iterator<T> iterator = iterator();
  68         while (iterator.hasNext()) {
  69             iterator.next();
  70             count++;
  71         }
  72         return count;
  73     }
  74 
  75     default boolean isEmpty() {
  76         return !iterator().hasNext();
  77     }
  78 
  79     default boolean isNotEmpty() {
  80         return iterator().hasNext();
  81     }
  82 
  83     default boolean contains(T node) {
  84         for (T next : this) {
  85             if (next == node) {
  86                 return true;
  87             }
  88         }
  89         return false;
  90     }
  91 }