170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes/* 270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * Copyright (C) 2011 The Android Open Source Project 370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * 470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * you may not use this file except in compliance with the License. 670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * You may obtain a copy of the License at 770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * 870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * 1070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * Unless required by applicable law or agreed to in writing, software 1170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 1270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * See the License for the specific language governing permissions and 1470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * limitations under the License. 1570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes */ 1670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 1770c820401677ca251ad09ac64cc23c760764e75dElliott Hughespackage java.util; 1870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 1970c820401677ca251ad09ac64cc23c760764e75dElliott Hughesimport java.lang.reflect.Array; 2070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 2170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes/** 2270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * An array-backed list that exposes its array. 2370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * 2470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes * @hide 2570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes */ 2670c820401677ca251ad09ac64cc23c760764e75dElliott Hughespublic class UnsafeArrayList<T> extends AbstractList<T> { 2770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes private final Class<T> elementType; 2870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes private T[] array; 2970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes private int size; 3070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 3170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes public UnsafeArrayList(Class<T> elementType, int initialCapacity) { 3270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes this.array = (T[]) Array.newInstance(elementType, initialCapacity); 3370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes this.elementType = elementType; 3470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 3570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 3670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes @Override public boolean add(T element) { 3770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes if (size == array.length) { 3870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes T[] newArray = (T[]) Array.newInstance(elementType, size * 2); 3970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes System.arraycopy(array, 0, newArray, 0, size); 4070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes array = newArray; 4170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 4270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes array[size++] = element; 4370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes ++modCount; 4470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes return true; 4570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 4670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 4770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes public T[] array() { 4870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes return array; 4970c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 5070c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 5170c820401677ca251ad09ac64cc23c760764e75dElliott Hughes public T get(int i) { 5270c820401677ca251ad09ac64cc23c760764e75dElliott Hughes return array[i]; 5370c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 5470c820401677ca251ad09ac64cc23c760764e75dElliott Hughes 5570c820401677ca251ad09ac64cc23c760764e75dElliott Hughes public int size() { 5670c820401677ca251ad09ac64cc23c760764e75dElliott Hughes return size; 5770c820401677ca251ad09ac64cc23c760764e75dElliott Hughes } 5870c820401677ca251ad09ac64cc23c760764e75dElliott Hughes} 59