blob: 5ee594bf026958e33830bde834b7435ea703540f [file] [log] [blame]
/*
* Copyright (c) 2008-2013, 2015 Eike Stepper (Berlin, Germany) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eike Stepper - initial API and implementation
*/
package org.eclipse.net4j.util.collection;
import java.util.ArrayList;
/**
* A list with O(1) effort for random access.
* <p>
* Whenever possible EMF's BasicEList should be used in favour of this list implementation.
*
* @author Eike Stepper
*/
public class MoveableArrayList<E> extends ArrayList<E>implements MoveableList<E>
{
private static final long serialVersionUID = 1L;
public MoveableArrayList(int initialCapacity)
{
super(initialCapacity);
}
/**
* @since 3.0
*/
public void move(int newPosition, Object object)
{
move(newPosition, indexOf(object));
}
public E move(int targetIndex, int sourceIndex)
{
int size = size();
if (sourceIndex >= size)
{
throw new IndexOutOfBoundsException("sourceIndex=" + sourceIndex + ", size=" + size); //$NON-NLS-1$ //$NON-NLS-2$
}
if (targetIndex >= size)
{
throw new IndexOutOfBoundsException("targetIndex=" + targetIndex + ", size=" + size); //$NON-NLS-1$ //$NON-NLS-2$
}
E object = get(sourceIndex);
if (targetIndex == sourceIndex)
{
return object;
}
if (targetIndex < sourceIndex)
{
moveUp1(targetIndex, sourceIndex - targetIndex);
}
else
{
moveDown1(targetIndex, targetIndex - sourceIndex);
}
set(targetIndex, object);
return object;
}
private void moveUp1(int index, int count)
{
for (int i = count; i > 0; i--)
{
set(index + i, get(index + i - 1));
}
}
private void moveDown1(int index, int count)
{
for (int i = count; i > 0; i--)
{
set(index - i, get(index - i + 1));
}
}
}