Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2012-12-11 10:22:27 +0000
committerEike Stepper2012-12-11 10:22:27 +0000
commita7cc973c217e3313df011cad0da2ff557d83f312 (patch)
tree65e0ab8836b76a29880e091f24e881d3665194da /plugins/org.eclipse.net4j.util
parentdaa9cf753da1f50d21d6e2ad86754545d13e0e90 (diff)
downloadcdo-a7cc973c217e3313df011cad0da2ff557d83f312.tar.gz
cdo-a7cc973c217e3313df011cad0da2ff557d83f312.tar.xz
cdo-a7cc973c217e3313df011cad0da2ff557d83f312.zip
[396173] UnsupportedOperationException at
...GrowingRandomAccessList.add(...) https://bugs.eclipse.org/bugs/show_bug.cgi?id=396173
Diffstat (limited to 'plugins/org.eclipse.net4j.util')
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/GrowingRandomAccessList.java50
1 files changed, 46 insertions, 4 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/GrowingRandomAccessList.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/GrowingRandomAccessList.java
index 2bd35a0ff4..b6fffcb9b8 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/GrowingRandomAccessList.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/GrowingRandomAccessList.java
@@ -26,12 +26,12 @@ import java.util.RandomAccess;
*/
public class GrowingRandomAccessList<E> extends AbstractList<E> implements Deque<E>, RandomAccess
{
- private final List<E[]> pages = new ArrayList<E[]>();
-
private final Class<E> componentType;
private final int pageCapacity;
+ private List<E[]> pages = new ArrayList<E[]>();
+
private int firstFree;
private int lastFree;
@@ -52,7 +52,7 @@ public class GrowingRandomAccessList<E> extends AbstractList<E> implements Deque
}
index += firstFree;
- E[] page = pages.get(index / pageCapacity);
+ E[] page = getPage(index);
return page[index % pageCapacity];
}
@@ -129,7 +129,39 @@ public class GrowingRandomAccessList<E> extends AbstractList<E> implements Deque
@Override
public void add(int index, E element)
{
- throw new UnsupportedOperationException();
+ int size = size();
+ if (index > size || index < 0)
+ {
+ throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
+ }
+
+ if (index == 0)
+ {
+ addFirst(element);
+ }
+ else if (index == size)
+ {
+ addLast(element);
+ }
+ else
+ {
+ GrowingRandomAccessList<E> result = new GrowingRandomAccessList<E>(componentType, pageCapacity);
+ for (int i = 0; i < size; i++)
+ {
+ E e = get(i);
+
+ if (i == index)
+ {
+ result.add(element);
+ }
+
+ result.add(e);
+ }
+
+ pages = result.pages;
+ firstFree = result.firstFree;
+ lastFree = result.lastFree;
+ }
}
@Override
@@ -288,6 +320,16 @@ public class GrowingRandomAccessList<E> extends AbstractList<E> implements Deque
return page;
}
+ protected E[] getPage(int index)
+ {
+ return pages.get(getPageIndex(index));
+ }
+
+ protected int getPageIndex(int index)
+ {
+ return index / pageCapacity;
+ }
+
private void initFirstPage()
{
int centerIndex = (pageCapacity - 1) / 2;

Back to the top