Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/java2d/xr/GrowableIntArray.java
32288 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.xr;2627import java.util.*;2829/**30* Growable int array, designed to allow subclasses to emulate31* the behaviour of value types.32*33* @author Clemens Eisserer34*/3536public class GrowableIntArray {3738int[] array;39int size;40int cellSize;4142public GrowableIntArray(int cellSize, int initialSize) {43array = new int[initialSize];44size = 0;45this.cellSize = cellSize;46}4748private int getNextCellIndex() {49int oldSize = size;50size += cellSize;5152if (size >= array.length) {53growArray();54}5556return oldSize;57}5859/**60* @return a direct reference to the backing array.61*/62public int[] getArray() {63return array;64}6566/**67* @return a copy of the backing array.68*/69public int[] getSizedArray() {70return Arrays.copyOf(array, getSize());71}7273/**74* Returns the index of the next free cell,75* and grows the backing arrays if required.76*/77public final int getNextIndex() {78return getNextCellIndex() / cellSize;79}8081protected final int getCellIndex(int cellIndex) {82return cellSize * cellIndex;83}8485public final int getInt(int cellIndex) {86return array[cellIndex];87}8889public final void addInt(int i) {90int nextIndex = getNextIndex();91array[nextIndex] = i;92}9394/**95* @return The number of stored cells.96*/97public final int getSize() {98return size / cellSize;99}100101public void clear() {102size = 0;103}104105protected void growArray() {106int newSize = Math.max(array.length * 2, 10);107int[] oldArray = array;108array = new int[newSize];109110System.arraycopy(oldArray, 0, array, 0, oldArray.length);111}112113}114115116