01: /*
02: * SimpleArrayList.java
03: *
04: * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import java.util.*;
12:
13: class SimpleArrayList extends AbstractList implements RandomAccess,
14: java.io.Serializable {
15: private final Object[] a;
16:
17: SimpleArrayList(Object[] array) {
18: if (array == null)
19: throw new NullPointerException();
20: a = array;
21: }
22:
23: public int size() {
24: return a.length;
25: }
26:
27: public Object[] toArray() {
28: return (Object[]) a.clone();
29: }
30:
31: public Object[] toArray(Object[] a) {
32: int size = size();
33: if (a.length < size) {
34: for (int i = 0; i < a.length; i++) {
35: a[i] = this .a[i];
36: }
37: return a;
38: }
39: System.arraycopy(this .a, 0, a, 0, size);
40: if (a.length > size) {
41: a[size] = null;
42: }
43: return a;
44: }
45:
46: public Object get(int index) {
47: return a[index];
48: }
49:
50: public Object set(int index, Object element) {
51: Object oldValue = a[index];
52: a[index] = element;
53: return oldValue;
54: }
55:
56: public int indexOf(Object o) {
57: if (o == null) {
58: for (int i = 0; i < a.length; i++)
59: if (a[i] == null)
60: return i;
61: } else {
62: for (int i = 0; i < a.length; i++)
63: if (o.equals(a[i]))
64: return i;
65: }
66: return -1;
67: }
68:
69: public boolean contains(Object o) {
70: return indexOf(o) != -1;
71: }
72: }
|