github.com/searKing/golang/go@v1.2.74/util/iterator.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/searKing/golang/go/error/exception"
    11  	"github.com/searKing/golang/go/util/class"
    12  	"github.com/searKing/golang/go/util/function/consumer"
    13  	"github.com/searKing/golang/go/util/object"
    14  )
    15  
    16  /**
    17   * An iterator over a collection.  {@code Iterator} takes the place of
    18   * {@link Enumeration} in the Java Collections Framework.  Iterators
    19   * differ from enumerations in two ways:
    20   *
    21   * <ul>
    22   *      <li> Iterators allow the caller to remove elements from the
    23   *           underlying collection during the iteration with well-defined
    24   *           semantics.
    25   *      <li> Method names have been improved.
    26   * </ul>
    27   *
    28   * <p>This interface is a member of the
    29   * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
    30   * Java Collections Framework</a>.
    31   *
    32   * @apiNote
    33   * An {@link Enumeration} can be converted into an {@code Iterator} by
    34   * using the {@link Enumeration#asIterator} method.
    35   *
    36   * @param <E> the type of elements returned by this iterator
    37   *
    38   * @author  Josh Bloch
    39   * @see Collection
    40   * @see ListIterator
    41   * @see Iterable
    42   * @since 1.2
    43   */
    44  
    45  type Iterator interface {
    46  	/**
    47  	 * Returns {@code true} if the iteration has more elements.
    48  	 * (In other words, returns {@code true} if {@link #next} would
    49  	 * return an element rather than throwing an exception.)
    50  	 *
    51  	 * @return {@code true} if the iteration has more elements
    52  	 */
    53  	HasNext() bool
    54  	/**
    55  	 * Returns the next element in the iteration.
    56  	 *
    57  	 * @return the next element in the iteration
    58  	 * @throws NoSuchElementException if the iteration has no more elements
    59  	 */
    60  	Next() interface{}
    61  
    62  	/**
    63  	 * Removes from the underlying collection the last element returned
    64  	 * by this iterator (optional operation).  This method can be called
    65  	 * only once per call to {@link #next}.
    66  	 * <p>
    67  	 * The behavior of an iterator is unspecified if the underlying collection
    68  	 * is modified while the iteration is in progress in any way other than by
    69  	 * calling this method, unless an overriding class has specified a
    70  	 * concurrent modification policy.
    71  	 * <p>
    72  	 * The behavior of an iterator is unspecified if this method is called
    73  	 * after a call to the {@link #forEachRemaining forEachRemaining} method.
    74  	 *
    75  	 * @implSpec
    76  	 * The default implementation throws an instance of
    77  	 * {@link UnsupportedOperationException} and performs no other action.
    78  	 *
    79  	 * @throws UnsupportedOperationException if the {@code remove}
    80  	 *         operation is not supported by this iterator
    81  	 *
    82  	 * @throws IllegalStateException if the {@code next} method has not
    83  	 *         yet been called, or the {@code remove} method has already
    84  	 *         been called after the last call to the {@code next}
    85  	 *         method
    86  	 */
    87  	Remove()
    88  
    89  	/**
    90  	 * Performs the given action for each remaining element until all elements
    91  	 * have been processed or the action throws an exception.  Actions are
    92  	 * performed in the order of iteration, if that order is specified.
    93  	 * Exceptions thrown by the action are relayed to the caller.
    94  	 * <p>
    95  	 * The behavior of an iterator is unspecified if the action modifies the
    96  	 * collection in any way (even by calling the {@link #remove remove} method
    97  	 * or other mutator methods of {@code Iterator} subtypes),
    98  	 * unless an overriding class has specified a concurrent modification policy.
    99  	 * <p>
   100  	 * Subsequent behavior of an iterator is unspecified if the action throws an
   101  	 * exception.
   102  	 *
   103  	 * @implSpec
   104  	 * <p>The default implementation behaves as if:
   105  	 * <pre>{@code
   106  	 *     while (hasNext())
   107  	 *         action.accept(next());
   108  	 * }</pre>
   109  	 *
   110  	 * @param action The action to be performed for each element
   111  	 * @throws NullPointerException if the specified action is null
   112  	 * @since 1.8
   113  	 */
   114  	ForEachRemaining(ctx context.Context, action consumer.Consumer)
   115  }
   116  
   117  type AbstractIteratorClass struct {
   118  	class.Class
   119  }
   120  
   121  func (iter *AbstractIteratorClass) HasNext() bool {
   122  	panic(exception.NewIllegalStateException1("called wrong HasNext method"))
   123  }
   124  
   125  func (iter *AbstractIteratorClass) Next() interface{} {
   126  	panic(exception.NewIllegalStateException1("called wrong Next method"))
   127  }
   128  
   129  func (iter *AbstractIteratorClass) Remove() {
   130  	panic(exception.NewUnsupportedOperationException1("remove"))
   131  }
   132  
   133  func (iter *AbstractIteratorClass) ForEachRemaining(ctx context.Context, action consumer.Consumer) {
   134  	object.RequireNonNull(action)
   135  	for iter.GetDerivedElse(iter).(Iterator).HasNext() {
   136  		select {
   137  		case <-ctx.Done():
   138  			return
   139  		default:
   140  		}
   141  		action.Accept(iter.GetDerivedElse(iter).(Iterator).Next())
   142  	}
   143  }