Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/MoveableArrayList.java')
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/MoveableArrayList.java67
1 files changed, 67 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/MoveableArrayList.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/MoveableArrayList.java
new file mode 100644
index 0000000000..62d42d3bb0
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/MoveableArrayList.java
@@ -0,0 +1,67 @@
+package org.eclipse.net4j.util.collection;
+
+import java.util.ArrayList;
+
+/**
+ * A list with O(1) effort for random access.
+ *
+ * @author Eike Stepper
+ */
+public final class MoveableArrayList<E> extends ArrayList<E> implements MoveableList<E>
+{
+ private static final long serialVersionUID = 1L;
+
+ public MoveableArrayList(int initialCapacity)
+ {
+ super(initialCapacity);
+
+ }
+
+ public E move(int targetIndex, int sourceIndex)
+ {
+ int size = size();
+ if (sourceIndex >= size)
+ {
+ throw new IndexOutOfBoundsException("sourceIndex=" + sourceIndex + ", size=" + size);
+ }
+
+ if (targetIndex >= size)
+ {
+ throw new IndexOutOfBoundsException("targetIndex=" + targetIndex + ", size=" + size);
+ }
+
+ 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));
+ }
+ }
+}

Back to the top