lukecwik commented on a change in pull request #15405: URL: https://github.com/apache/beam/pull/15405#discussion_r703810887
########## File path: sdks/java/fn-execution/src/main/java/org/apache/beam/sdk/fn/stream/PrefetchableIterators.java ########## @@ -0,0 +1,210 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.fn.stream; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class PrefetchableIterators { + + private static final PrefetchableIterator<Object> EMPTY_ITERATOR = + new PrefetchableIterator<Object>() { + @Override + public boolean isReady() { + return true; + } + + @Override + public void prefetch() {} + + @Override + public boolean hasNext() { + return false; + } + + @Override + public Object next() { + throw new NoSuchElementException(); + } + }; + + /** Returns an empty {@link PrefetchableIterator}. */ + public static <T> PrefetchableIterator<T> emptyIterator() { + return (PrefetchableIterator<T>) EMPTY_ITERATOR; + } + + /** + * Returns a {@link PrefetchableIterator} over the specified values. + * + * <p>{@link PrefetchableIterator#prefetch()} is a no-op and {@link + * PrefetchableIterator#isReady()} always returns true. + */ + public static <T> PrefetchableIterator<T> fromArray(T... values) { + if (values.length == 0) { + return emptyIterator(); + } + return new PrefetchableIterator<T>() { + int currentIndex; + + @Override + public boolean isReady() { + return true; + } + + @Override + public void prefetch() {} + + @Override + public boolean hasNext() { + return currentIndex < values.length; + } + + @Override + public T next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + return values[currentIndex++]; + } + }; + } + + /** + * If the {@link Iterator} is not a {@link PrefetchableIterator} then one is constructed that + * ensures {@link PrefetchableIterator#prefetch} is a no-op and {@link + * PrefetchableIterator#isReady} always returns true. + */ + // package private for PrefetchableIterables. + static <T> PrefetchableIterator<T> maybePrefetchable(Iterator<T> iterator) { + if (iterator == null) { + throw new IllegalArgumentException("Expected non-null iterator."); + } + if (iterator instanceof PrefetchableIterator) { + return (PrefetchableIterator<T>) iterator; + } + return new PrefetchableIterator<T>() { + @Override + public boolean isReady() { + return true; + } + + @Override + public void prefetch() {} + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public T next() { + return iterator.next(); + } + }; + } + + public static <T> PrefetchableIterator<T> concatIterators(Iterator<Iterator<T>> iterators) { + if (!iterators.hasNext()) { + return emptyIterator(); + } + // TODO: Share implementation details with #concat version Review comment: Yes, I had already made `PrefetchableIterables#concat` re-use `PrefetchableIterators#concat` and forgot to remove this comment. It used to be that `PrefetchableIterables#concat` was a copy. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
