lubomir@281: /* lubomir@281: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. lubomir@281: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. lubomir@281: * lubomir@281: * This code is free software; you can redistribute it and/or modify it lubomir@281: * under the terms of the GNU General Public License version 2 only, as lubomir@281: * published by the Free Software Foundation. Oracle designates this lubomir@281: * particular file as subject to the "Classpath" exception as provided lubomir@281: * by Oracle in the LICENSE file that accompanied this code. lubomir@281: * lubomir@281: * This code is distributed in the hope that it will be useful, but WITHOUT lubomir@281: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or lubomir@281: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License lubomir@281: * version 2 for more details (a copy is included in the LICENSE file that lubomir@281: * accompanied this code). lubomir@281: * lubomir@281: * You should have received a copy of the GNU General Public License version lubomir@281: * 2 along with this work; if not, write to the Free Software Foundation, lubomir@281: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. lubomir@281: * lubomir@281: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA lubomir@281: * or visit www.oracle.com if you need additional information or have any lubomir@281: * questions. lubomir@281: */ lubomir@281: lubomir@281: package org.apidesign.javap; lubomir@281: lubomir@281: public class TypeArray { lubomir@281: private static final int CAPACITY_INCREMENT = 16; lubomir@281: lubomir@281: private int[] types; lubomir@281: private int size; lubomir@281: lubomir@281: public void add(final int newType) { lubomir@281: ensureCapacity(size + 1); lubomir@281: types[size++] = newType; lubomir@281: } lubomir@281: lubomir@281: public void addAll(final int[] newTypes) { lubomir@281: if (newTypes.length > 0) { lubomir@281: ensureCapacity(size + newTypes.length); lubomir@281: arraycopy(newTypes, 0, types, size, newTypes.length); lubomir@281: size += newTypes.length; lubomir@281: } lubomir@281: } lubomir@281: lubomir@281: public void setAll(final int[] newTypes) { lubomir@281: if (newTypes.length > 0) { lubomir@281: ensureCapacity(newTypes.length); lubomir@281: arraycopy(newTypes, 0, types, 0, newTypes.length); lubomir@281: size = newTypes.length; lubomir@281: } else { lubomir@281: clear(); lubomir@281: } lubomir@281: } lubomir@281: lubomir@281: public void setSize(final int newSize) { lubomir@281: ensureCapacity(newSize); lubomir@281: size = newSize; lubomir@281: } lubomir@281: lubomir@281: public void clear() { lubomir@281: size = 0; lubomir@281: } lubomir@281: lubomir@281: public int getSize() { lubomir@281: return size; lubomir@281: } lubomir@281: lubomir@281: public int get(final int index) { lubomir@281: return types[index]; lubomir@281: } lubomir@281: lubomir@281: private void ensureCapacity(final int minCapacity) { lubomir@281: if ((minCapacity == 0) lubomir@281: || (types != null) && (minCapacity <= types.length)) { lubomir@281: return; lubomir@281: } lubomir@281: lubomir@281: final int newCapacity = lubomir@281: ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT) lubomir@281: * CAPACITY_INCREMENT; lubomir@281: final int[] newTypes = new int[newCapacity]; lubomir@281: lubomir@281: if (size > 0) { lubomir@281: arraycopy(types, 0, newTypes, 0, size); lubomir@281: } lubomir@281: lubomir@281: types = newTypes; lubomir@281: } lubomir@281: lubomir@281: // no System.arraycopy lubomir@281: private void arraycopy(final int[] src, final int srcPos, lubomir@281: final int[] dest, final int destPos, lubomir@281: final int length) { lubomir@281: for (int i = 0; i < length; ++i) { lubomir@281: dest[destPos + i] = src[srcPos + i]; lubomir@281: } lubomir@281: } lubomir@281: }