01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.cocoon.util;
19:
20: /**
21: * Add-only Container class.
22: *
23: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
24: * @version CVS $Id: ResizableContainer.java 433543 2006-08-22 06:22:54Z crossley $
25: */
26: public class ResizableContainer {
27:
28: private int pointer = -1;
29: private int size = 0;
30: private Object[] container;
31:
32: public ResizableContainer(int initialCapacity) {
33: this .container = new Object[initialCapacity];
34: }
35:
36: public void add(Object o) {
37: set(++pointer, o);
38: }
39:
40: public void set(int index, Object o) {
41: adjustPointer(index);
42: ensureCapacity(index + 1);
43: container[index] = o;
44: size++;
45: }
46:
47: public Object get(int index) {
48: return (index < container.length) ? container[index] : null;
49: }
50:
51: public int size() {
52: return size;
53: }
54:
55: private void adjustPointer(int newPointer) {
56: this .pointer = Math.max(this .pointer, newPointer);
57: }
58:
59: private void ensureCapacity(int minCapacity) {
60: int oldCapacity = container.length;
61: if (oldCapacity < minCapacity) {
62: Object[] oldContainer = container;
63: int newCapacity = (oldCapacity * 3) / 2 + 1;
64: if (newCapacity < minCapacity) {
65: newCapacity = minCapacity;
66: }
67: container = new Object[newCapacity];
68: System.arraycopy(oldContainer, 0, container, 0,
69: oldContainer.length);
70: }
71: }
72: }
|