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