javap/src/main/java/org/apidesign/javap/TypeArray.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Fri, 07 Dec 2012 15:02:35 +0100
branchregisters
changeset 281 f2352e0b713e
child 307 eaf4e8387065
permissions -rw-r--r--
Type specific stack variables
lubomir@281
     1
/*
lubomir@281
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
lubomir@281
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
lubomir@281
     4
 *
lubomir@281
     5
 * This code is free software; you can redistribute it and/or modify it
lubomir@281
     6
 * under the terms of the GNU General Public License version 2 only, as
lubomir@281
     7
 * published by the Free Software Foundation.  Oracle designates this
lubomir@281
     8
 * particular file as subject to the "Classpath" exception as provided
lubomir@281
     9
 * by Oracle in the LICENSE file that accompanied this code.
lubomir@281
    10
 *
lubomir@281
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
lubomir@281
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
lubomir@281
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
lubomir@281
    14
 * version 2 for more details (a copy is included in the LICENSE file that
lubomir@281
    15
 * accompanied this code).
lubomir@281
    16
 *
lubomir@281
    17
 * You should have received a copy of the GNU General Public License version
lubomir@281
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
lubomir@281
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
lubomir@281
    20
 *
lubomir@281
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
lubomir@281
    22
 * or visit www.oracle.com if you need additional information or have any
lubomir@281
    23
 * questions.
lubomir@281
    24
 */
lubomir@281
    25
lubomir@281
    26
package org.apidesign.javap;
lubomir@281
    27
lubomir@281
    28
public class TypeArray {
lubomir@281
    29
    private static final int CAPACITY_INCREMENT = 16;
lubomir@281
    30
lubomir@281
    31
    private int[] types;
lubomir@281
    32
    private int size;
lubomir@281
    33
lubomir@281
    34
    public void add(final int newType) {
lubomir@281
    35
        ensureCapacity(size + 1);
lubomir@281
    36
        types[size++] = newType;
lubomir@281
    37
    }
lubomir@281
    38
lubomir@281
    39
    public void addAll(final int[] newTypes) {
lubomir@281
    40
        if (newTypes.length > 0) {
lubomir@281
    41
            ensureCapacity(size + newTypes.length);
lubomir@281
    42
            arraycopy(newTypes, 0, types, size, newTypes.length);
lubomir@281
    43
            size += newTypes.length;
lubomir@281
    44
        }
lubomir@281
    45
    }
lubomir@281
    46
lubomir@281
    47
    public void setAll(final int[] newTypes) {
lubomir@281
    48
        if (newTypes.length > 0) {
lubomir@281
    49
            ensureCapacity(newTypes.length);
lubomir@281
    50
            arraycopy(newTypes, 0, types, 0, newTypes.length);
lubomir@281
    51
            size = newTypes.length;
lubomir@281
    52
        } else {
lubomir@281
    53
            clear();
lubomir@281
    54
        }
lubomir@281
    55
    }
lubomir@281
    56
lubomir@281
    57
    public void setSize(final int newSize) {
lubomir@281
    58
        ensureCapacity(newSize);
lubomir@281
    59
        size = newSize;
lubomir@281
    60
    }
lubomir@281
    61
lubomir@281
    62
    public void clear() {
lubomir@281
    63
        size = 0;
lubomir@281
    64
    }
lubomir@281
    65
lubomir@281
    66
    public int getSize() {
lubomir@281
    67
        return size;
lubomir@281
    68
    }
lubomir@281
    69
lubomir@281
    70
    public int get(final int index) {
lubomir@281
    71
        return types[index];
lubomir@281
    72
    }
lubomir@281
    73
lubomir@281
    74
    private void ensureCapacity(final int minCapacity) {
lubomir@281
    75
        if ((minCapacity == 0)
lubomir@281
    76
                || (types != null) && (minCapacity <= types.length)) {
lubomir@281
    77
            return;
lubomir@281
    78
        }
lubomir@281
    79
lubomir@281
    80
        final int newCapacity =
lubomir@281
    81
                ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
lubomir@281
    82
                    * CAPACITY_INCREMENT;
lubomir@281
    83
        final int[] newTypes = new int[newCapacity];
lubomir@281
    84
lubomir@281
    85
        if (size > 0) {
lubomir@281
    86
            arraycopy(types, 0, newTypes, 0, size);
lubomir@281
    87
        }
lubomir@281
    88
lubomir@281
    89
        types = newTypes;
lubomir@281
    90
    }
lubomir@281
    91
lubomir@281
    92
    // no System.arraycopy
lubomir@281
    93
    private void arraycopy(final int[] src, final int srcPos,
lubomir@281
    94
                           final int[] dest, final int destPos,
lubomir@281
    95
                           final int length) {
lubomir@281
    96
        for (int i = 0; i < length; ++i) {
lubomir@281
    97
            dest[destPos + i] = src[srcPos + i];
lubomir@281
    98
        }
lubomir@281
    99
    }
lubomir@281
   100
}