View Javadoc
1   package org.apache.maven.surefire.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Iterator;
23  import java.util.NoSuchElementException;
24  
25  /**
26   * This iterator is marked as stopped if {@link #isClosed()} returns {@code true}.
27   * If the iterator has been closed before calling {@link #hasNext()} then the method returns {@code false}.
28   * If the iterator was closed after {@link #hasNext() hasNext returns true} but before {@link #next()}, the
29   * method {@link #next()} throws {@link java.util.NoSuchElementException}.
30   * The method {@link #remove()} throws {@link IllegalStateException} if the iterator has been closed.
31   *
32   * @param <T> the type of elements returned by this iterator
33   *
34   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
35   * @since 2.19.1
36   */
37  public abstract class CloseableIterator<T>
38          implements Iterator<T>
39  {
40      private Boolean finishCurrentIteration;
41  
42      protected abstract boolean isClosed();
43      protected abstract boolean doHasNext();
44      protected abstract T doNext();
45      protected abstract void doRemove();
46  
47      @Override
48      public boolean hasNext()
49      {
50          popMarker();
51          return !finishCurrentIteration && doHasNext();
52      }
53  
54      @Override
55      public T next()
56      {
57          try
58          {
59              if ( popMarker() && finishCurrentIteration )
60              {
61                  throw new NoSuchElementException( "iterator closed" );
62              }
63              return doNext();
64          }
65          finally
66          {
67              finishCurrentIteration = null;
68          }
69      }
70  
71      @Override
72      public void remove()
73      {
74          try
75          {
76              if ( popMarker() && finishCurrentIteration )
77              {
78                  throw new IllegalStateException( "iterator closed" );
79              }
80              doRemove();
81          }
82          finally
83          {
84              finishCurrentIteration = null;
85          }
86      }
87  
88      /**
89       * @return {@code true} if marker changed from NULL to anything
90       */
91      private boolean popMarker()
92      {
93          if ( finishCurrentIteration == null )
94          {
95              finishCurrentIteration = isClosed();
96              return true;
97          }
98          return false;
99      }
100 }