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