Github user tillrohrmann commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2837#discussion_r88886636
  
    --- Diff: 
flink-runtime/src/main/java/org/apache/flink/runtime/util/EvictingBoundedList.java
 ---
    @@ -0,0 +1,139 @@
    +/*
    + * 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.util.Preconditions;
    +
    +import java.util.ConcurrentModificationException;
    +import java.util.Iterator;
    +
    +/**
    + * This class implements a list (array based) that is physically bounded 
in maximum size, but can virtually grow beyond
    + * the bounded size. When the list grows beyond the size bound, elements 
are dropped from the head of the list (FIFO
    + * order). If dropped elements are accessed, a default element is returned 
instead.
    + * <p>
    + * TODO this class could eventually implement the whole actual List 
interface.
    + *
    + * @param <T> type of the list elements
    + */
    +public class EvictingBoundedList<T> implements Iterable<T> {
    +
    +   private final T defaultElement;
    +   private final Object[] elements;
    +   private int idx;
    +   private int count;
    +   private long modCount;
    +
    +   public EvictingBoundedList(int sizeLimit) {
    +           this(sizeLimit, null);
    +   }
    +
    +   public EvictingBoundedList(int sizeLimit, T defaultElement) {
    +           this.elements = new Object[sizeLimit];
    +           this.defaultElement = defaultElement;
    +           this.idx = 0;
    +           this.count = 0;
    +           this.modCount = 0;
    +   }
    +
    +   public int size() {
    +           return count;
    +   }
    +
    +   public boolean isEmpty() {
    +           return 0 == count;
    +   }
    +
    +   public boolean add(T t) {
    +           elements[idx] = t;
    +           idx = (idx + 1) % elements.length;
    +           ++count;
    +           ++modCount;
    +           return true;
    +   }
    +
    +   public void clear() {
    +           if (!isEmpty()) {
    +                   for (int i = 0; i < elements.length; ++i) {
    +                           elements[i] = null;
    +                   }
    +                   count = 0;
    +                   idx = 0;
    +                   ++modCount;
    +           }
    +   }
    +
    +   public T get(int index) {
    +           Preconditions.checkArgument(index >= 0 && index < count);
    +           return isDroppedIndex(index) ? getDefaultElement() : 
accessInternal(index % elements.length);
    +   }
    +
    +   public T set(int index, T element) {
    +           Preconditions.checkArgument(index >= 0 && index < count);
    +           ++modCount;
    +           if (isDroppedIndex(index)) {
    +                   return getDefaultElement();
    +           } else {
    +                   int idx = index % elements.length;
    +                   T old = accessInternal(idx);
    +                   elements[idx] = element;
    +                   return old;
    +           }
    +   }
    +
    +   public T getDefaultElement() {
    +           return defaultElement;
    +   }
    +
    +   private boolean isDroppedIndex(int idx) {
    +           return idx < count - elements.length;
    +   }
    +
    +   @SuppressWarnings("unchecked")
    +   private T accessInternal(int arrayIndex) {
    +           return (T) elements[arrayIndex];
    +   }
    +
    +   @Override
    +   public Iterator<T> iterator() {
    +           return new Iterator<T>() {
    +
    +                   int pos = 0;
    +                   final long oldModCount = modCount;
    +
    +                   @Override
    +                   public boolean hasNext() {
    +                           return pos < count;
    +                   }
    +
    +                   @Override
    +                   public T next() {
    +                           if (oldModCount != modCount) {
    +                                   throw new 
ConcurrentModificationException();
    +                           }
    +                           return get(pos++);
    --- End diff --
    
    I think we should throw an `NoSuchElementException` if we see an 
`IllegalArgumentException` here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to