Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2008-11-01 10:16:35 -0400
committerEike Stepper2008-11-01 10:16:35 -0400
commitc1962c65ea60b7026767924090ffe08566f40687 (patch)
tree2eccb2802982c1868e2295838b8233e01b663aa5
parent1bd480f81f09442906779ec673d77ef27d883a95 (diff)
downloadcdo-c1962c65ea60b7026767924090ffe08566f40687.tar.gz
cdo-c1962c65ea60b7026767924090ffe08566f40687.tar.xz
cdo-c1962c65ea60b7026767924090ffe08566f40687.zip
Added DynamicArray
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/DynamicArray.java97
1 files changed, 97 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/DynamicArray.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/DynamicArray.java
new file mode 100644
index 0000000000..84de914308
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/DynamicArray.java
@@ -0,0 +1,97 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2008 Eike Stepper (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;
+
+/**
+ * @author Eike Stepper
+ * @since 2.0
+ */
+public class DynamicArray<E>
+{
+ private Object[] elements;
+
+ public DynamicArray()
+ {
+ }
+
+ public int add(E element)
+ {
+ int length = elements.length;
+ for (int i = 0; i < length; i++)
+ {
+ if (elements[i] == null)
+ {
+ elements[i] = element;
+ return i;
+ }
+ }
+
+ grow(length);
+ elements[length] = element;
+ return length;
+ }
+
+ @SuppressWarnings("unchecked")
+ public E add(int index, E element)
+ {
+ grow(index);
+ Object old = elements[index];
+ elements[index] = element;
+ return (E)old;
+ }
+
+ @SuppressWarnings("unchecked")
+ public E remove(int index)
+ {
+ Object old = elements[index];
+ if (old != null)
+ {
+ elements[index] = null;
+ shrink();
+ }
+
+ return (E)old;
+ }
+
+ @SuppressWarnings("unchecked")
+ public E get(int index)
+ {
+ return (E)elements[index];
+ }
+
+ private void grow(int index)
+ {
+ if (index >= elements.length)
+ {
+ Object[] newChannels = new Object[index + 1];
+ System.arraycopy(elements, 0, newChannels, 0, elements.length);
+ elements = newChannels;
+ }
+ }
+
+ private void shrink()
+ {
+ boolean shrink = false;
+ int lastIndex = elements.length - 1;
+ while (lastIndex > 0 && (shrink = elements[lastIndex] == null))
+ {
+ --lastIndex;
+ }
+
+ if (shrink)
+ {
+ int newLength = lastIndex + 1;
+ Object[] newChannels = new Object[newLength];
+ System.arraycopy(elements, 0, newChannels, 0, newLength);
+ elements = newChannels;
+ }
+ }
+}

Back to the top