1 16 17 package org.apache.cocoon.util; 18 19 25 public class ResizableContainer { 26 27 private int pointer = -1; 28 private int size = 0; 29 private Object [] container; 30 31 public ResizableContainer(int initialCapacity){ 32 this.container = new Object [initialCapacity]; 33 } 34 35 public void add(Object o) { 36 set(++pointer,o); 37 } 38 39 public void set(int index, Object o) { 40 adjustPointer(index); 41 ensureCapacity(index+1); 42 container[index] = o; 43 size++; 44 } 45 46 public Object get(int index) { 47 return (index < container.length) ? container[index] : null; 48 } 49 50 public int size() { 51 return size; 52 } 53 54 private void adjustPointer(int newPointer) { 55 this.pointer = Math.max(this.pointer, newPointer); 56 } 57 58 private void ensureCapacity(int minCapacity) { 59 int oldCapacity = container.length; 60 if (oldCapacity < minCapacity) { 61 Object [] oldContainer = container; 62 int newCapacity = (oldCapacity * 3)/2 + 1; 63 if (newCapacity < minCapacity) { 64 newCapacity = minCapacity; 65 } 66 container = new Object [newCapacity]; 67 System.arraycopy(oldContainer, 0, container, 0, oldContainer.length); 68 } 69 } 70 } 71 | Popular Tags |