package io.aeron;

import java.util.ArrayList;

public abstract class ArrayListUtil
{
    private ArrayListUtil()
    {
    }

    /**
     * Removes element at i, but instead of copying all elements to the left, moves into the same slot the last
     * element. If i is the last element it is just removed. This avoids the copy costs, but spoils the list order.
     *
     * @param list      to be modified
     * @param i         removal index
     * @param lastIndex last element index
     * @param <T>       element type
     */
    public static <T> void fastUnorderedRemove(final ArrayList<T> list, final int i, final int lastIndex)
    {
        final T last = list.remove(lastIndex);
        if (i != lastIndex)
        {
            list.set(i, last);
        }
    }

    /**
     * Removes element at i, but instead of copying all elements to the left, moves into the same slot the last
     * element. If i is the last element it is just removed. This avoids the copy costs, but spoils the list order.
     *
     * @param list      to be modified
     * @param e         to be removed
     * @param <T>       element type
     * @return true if found and removed, false otherwise
     */
    public static <T> boolean fastUnorderedRemove(final ArrayList<T> list, final T e)
    {
        for (int i = 0, size = list.size(); i < size; i++)
        {
            if (e == list.get(i))
            {
                fastUnorderedRemove(list, i, size - 1);
                return true;
            }
        }
        return false;
    }
}

@@ -1,8 +1,610 @@
+/*%0A * Copyright 2014 - 2017 Real Logic Ltd.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -748,32 +748,38 @@
 oves element at 
+index 
 i, but instead o
@@ -867,53 +867,8 @@
 ent.
- If i is the last element it is just removed.
  Thi
@@ -910,32 +910,77 @@
  the list order.
+ If i is the last element it is just removed.
 %0A     *%0A     * @
@@ -1001,32 +1001,33 @@
   to be modified
+.
 %0A     * @param i
@@ -1044,24 +1044,25 @@
 emoval index
+.
 %0A     * @par
@@ -1092,16 +1092,17 @@
 nt index
+.
 %0A     * 
@@ -1126,24 +1126,103 @@
 element type
+.%0A     * @throws IndexOutOfBoundsException if i or lastIndex are out of bounds.
 %0A     */%0A   
@@ -1497,14 +1497,8 @@
 ment
- at i,
  but
@@ -1568,39 +1568,32 @@
 me slot the last
-%0A     *
  element. If i i
@@ -1589,53 +1589,15 @@
 ent.
- If i is the last element it is just removed.
+%0A     *
  Thi
@@ -1647,16 +1647,61 @@
 t order.
+ If e is the last element it is just removed.
 %0A     *%0A
@@ -1738,16 +1738,17 @@
 modified
+.
 %0A     * 
@@ -1777,16 +1777,17 @@
  removed
+.
 %0A     * 
@@ -1815,16 +1815,17 @@
 ent type
+.
 %0A     * 
@@ -1874,16 +1874,17 @@
 therwise
+.
 %0A     */
@@ -2194,32 +2194,33 @@
     %7D%0A        %7D%0A
+%0A
         return f

Add copyright and fix Javadoc.