XComp commented on a change in pull request #15049: URL: https://github.com/apache/flink/pull/15049#discussion_r590497571
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/util/BoundedFIFOQueue.java ########## @@ -0,0 +1,141 @@ +/* + * 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.flink.runtime.util; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.util.Preconditions; + +import java.io.Serializable; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Queue; + +/** + * {@code BoundedFIFOQueue} collects elements up to given amount. Reaching this limit will result in + * removing the oldest element from this queue (First-In/First-Out; FIFO). + * + * @param <T> The type of elements collected. + */ +public class BoundedFIFOQueue<T> implements Iterable<T>, Serializable { + + private static final long serialVersionUID = -890727339944580409L; + + private final int maxSize; + private final Queue<T> elements; + + private int elementCount; + + /** + * Creates an immutable copy of the passed {@code BoundedFIFOQueue}. + * + * @param boundedFIFOQueue The queue for which an immutable copy shall be created. + * @param <E> The type this queue collects. + * @return An immutable copy of the passed {@code BoundedFIFOQueue}. + */ + public static <E> BoundedFIFOQueue<E> immutableCopy(BoundedFIFOQueue<E> boundedFIFOQueue) { + return new BoundedFIFOQueue<E>( + boundedFIFOQueue.maxSize, + boundedFIFOQueue.elements, + boundedFIFOQueue.elementCount) { + private static final long serialVersionUID = -2763611608109517436L; + + @Override + public void add(E element) { + throw new UnsupportedOperationException( + "This BoundedFIFOQueue is immutable. add is not supported."); + } + }; + } + + /** + * Creates a {@code BoundedFIFOQueue} with the given maximum size. + * + * @param maxSize The maximum size of this queue. Exceeding this limit would result in removing + * the oldest element (FIFO). + * @throws IllegalArgumentException If {@code maxSize} is less than 0. + */ + public BoundedFIFOQueue(int maxSize) { + this(maxSize, new LinkedList<>(), 0); + } + + /** + * Creates full {@code BoundedFIFOQueue} from passed {@link Collection}. + * + * @param initialElements the initial elements the queue is filled with. + */ + @VisibleForTesting + public BoundedFIFOQueue(Collection<T> initialElements) { + this(initialElements.size(), initialElements, initialElements.size()); + } + + private BoundedFIFOQueue(int maxSize, Iterable<T> initialElements, int initialElementCount) { + Preconditions.checkArgument(maxSize >= 0, "The maximum size should be at least 0."); + + this.maxSize = maxSize; + this.elements = new LinkedList<>(); + initialElements.forEach(elements::add); + this.elementCount = initialElementCount; + } + + /** + * Adds an element to the end of the queue. An element will be removed from the head of the + * queue if the queue would exceed its maximum size by adding the new element. + * + * @param element The element that should be added to the end of the queue. + * @throws NullPointerException If {@code null} is passed as an element. + */ + public void add(T element) { + elementCount++; Review comment: args, good catch. I fixed it ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
