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 public boolean hasNext() 48 { 49 popMarker(); 50 return !finishCurrentIteration && doHasNext(); 51 } 52 53 public T next() 54 { 55 try 56 { 57 if ( popMarker() && finishCurrentIteration ) 58 { 59 throw new NoSuchElementException( "iterator closed" ); 60 } 61 return doNext(); 62 } 63 finally 64 { 65 finishCurrentIteration = null; 66 } 67 } 68 69 public void remove() 70 { 71 try 72 { 73 if ( popMarker() && finishCurrentIteration ) 74 { 75 throw new IllegalStateException( "iterator closed" ); 76 } 77 doRemove(); 78 } 79 finally 80 { 81 finishCurrentIteration = null; 82 } 83 } 84 85 /** 86 * @return {@code true} if marker changed from NULL to anything 87 */ 88 private boolean popMarker() 89 { 90 if ( finishCurrentIteration == null ) 91 { 92 finishCurrentIteration = isClosed(); 93 return true; 94 } 95 return false; 96 } 97 }