1/*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.java2d.xr;
27
28import java.util.*;
29
30/**
31 * Growable int array, designed to allow subclasses to emulate
32 * the behaviour of value types.
33 *
34 * @author Clemens Eisserer
35 */
36
37public class GrowableIntArray {
38
39    int[] array;
40    int size;
41    int cellSize;
42
43    public GrowableIntArray(int cellSize, int initialSize) {
44        array = new int[initialSize];
45        size = 0;
46        this.cellSize = cellSize;
47    }
48
49    private int getNextCellIndex() {
50        int oldSize = size;
51        size += cellSize;
52
53        if (size >= array.length) {
54            growArray();
55        }
56
57        return oldSize;
58    }
59
60    /**
61     * @return a direct reference to the backing array.
62     */
63    public int[] getArray() {
64        return array;
65    }
66
67    /**
68     * @return a copy of the backing array.
69     */
70    public int[] getSizedArray() {
71        return Arrays.copyOf(array, getSize());
72    }
73
74    /**
75     * Returns the index of the next free cell,
76     * and grows the backing arrays if required.
77     */
78    public final int getNextIndex() {
79        return getNextCellIndex() / cellSize;
80    }
81
82    protected final int getCellIndex(int cellIndex) {
83        return cellSize * cellIndex;
84    }
85
86    public final int getInt(int cellIndex) {
87        return array[cellIndex];
88    }
89
90    public final void addInt(int i) {
91        int nextIndex = getNextIndex();
92        array[nextIndex] = i;
93    }
94
95    /**
96     * @return The number of stored cells.
97     */
98    public final int getSize() {
99        return size / cellSize;
100    }
101
102    public void clear() {
103        size = 0;
104    }
105
106    protected void growArray() {
107        int newSize = Math.max(array.length * 2, 10);
108        int[] oldArray = array;
109        array = new int[newSize];
110
111        System.arraycopy(oldArray, 0, array, 0, oldArray.length);
112    }
113
114}
115