1 /* 2 * Copyright (C) 2015 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.collect; 16 17 import static com.google.common.base.Preconditions.checkNotNull; 18 19 import com.google.common.annotations.GwtCompatible; 20 import java.util.Queue; 21 import javax.annotation.CheckForNull; 22 import org.checkerframework.checker.nullness.qual.Nullable; 23 24 /** 25 * An Iterator implementation which draws elements from a queue, removing them from the queue as it 26 * iterates. This class is not thread safe. 27 */ 28 @GwtCompatible 29 @ElementTypesAreNonnullByDefault 30 final class ConsumingQueueIterator<T extends @Nullable Object> extends AbstractIterator<T> { 31 private final Queue<T> queue; 32 ConsumingQueueIterator(Queue<T> queue)33 ConsumingQueueIterator(Queue<T> queue) { 34 this.queue = checkNotNull(queue); 35 } 36 37 @Override 38 @CheckForNull computeNext()39 protected T computeNext() { 40 // TODO(b/192579700): Use a ternary once it no longer confuses our nullness checker. 41 if (queue.isEmpty()) { 42 return endOfData(); 43 } 44 return queue.remove(); 45 } 46 } 47