javap/src/main/java/org/apidesign/javap/TypeArray.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 10 Feb 2013 12:14:40 +0100
branchemul
changeset 706 a48961ff3e6b
parent 307 eaf4e8387065
permissions -rw-r--r--
Using FastJar to read the content table of JAR files
lubomir@319
     1
/*
lubomir@319
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
lubomir@319
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
lubomir@319
     4
 *
lubomir@319
     5
 * This code is free software; you can redistribute it and/or modify it
lubomir@319
     6
 * under the terms of the GNU General Public License version 2 only, as
lubomir@319
     7
 * published by the Free Software Foundation.  Oracle designates this
lubomir@319
     8
 * particular file as subject to the "Classpath" exception as provided
lubomir@319
     9
 * by Oracle in the LICENSE file that accompanied this code.
lubomir@319
    10
 *
lubomir@319
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
lubomir@319
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
lubomir@319
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
lubomir@319
    14
 * version 2 for more details (a copy is included in the LICENSE file that
lubomir@319
    15
 * accompanied this code).
lubomir@319
    16
 *
lubomir@319
    17
 * You should have received a copy of the GNU General Public License version
lubomir@319
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
lubomir@319
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
lubomir@319
    20
 *
lubomir@319
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
lubomir@319
    22
 * or visit www.oracle.com if you need additional information or have any
lubomir@319
    23
 * questions.
lubomir@319
    24
 */
lubomir@319
    25
lubomir@319
    26
package org.apidesign.javap;
lubomir@319
    27
lubomir@319
    28
import static org.apidesign.javap.RuntimeConstants.ITEM_Bogus;
lubomir@319
    29
import static org.apidesign.javap.RuntimeConstants.ITEM_Integer;
lubomir@319
    30
import static org.apidesign.javap.RuntimeConstants.ITEM_Float;
lubomir@319
    31
import static org.apidesign.javap.RuntimeConstants.ITEM_Double;
lubomir@319
    32
import static org.apidesign.javap.RuntimeConstants.ITEM_Long;
lubomir@319
    33
import static org.apidesign.javap.RuntimeConstants.ITEM_Null;
lubomir@319
    34
import static org.apidesign.javap.RuntimeConstants.ITEM_InitObject;
lubomir@319
    35
import static org.apidesign.javap.RuntimeConstants.ITEM_Object;
lubomir@319
    36
import static org.apidesign.javap.RuntimeConstants.ITEM_NewObject;
lubomir@319
    37
lubomir@319
    38
public final class TypeArray {
lubomir@319
    39
    private static final int CAPACITY_INCREMENT = 16;
lubomir@319
    40
lubomir@319
    41
    private int[] types;
lubomir@319
    42
    private int size;
lubomir@319
    43
lubomir@319
    44
    public TypeArray() {
lubomir@319
    45
    }
lubomir@319
    46
    
lubomir@319
    47
    public TypeArray(final TypeArray initialTypes) {
lubomir@319
    48
        setAll(initialTypes);
lubomir@319
    49
    }
lubomir@319
    50
lubomir@319
    51
    public void add(final int newType) {
lubomir@319
    52
        ensureCapacity(size + 1);
lubomir@319
    53
        types[size++] = newType;
lubomir@319
    54
    }
lubomir@319
    55
lubomir@319
    56
    public void addAll(final TypeArray newTypes) {
lubomir@319
    57
        addAll(newTypes.types, 0, newTypes.size);
lubomir@319
    58
    }
lubomir@319
    59
lubomir@319
    60
    public void addAll(final int[] newTypes) {
lubomir@319
    61
        addAll(newTypes, 0, newTypes.length);
lubomir@319
    62
    }
lubomir@319
    63
lubomir@319
    64
    public void addAll(final int[] newTypes,
lubomir@319
    65
                       final int offset,
lubomir@319
    66
                       final int count) {
lubomir@319
    67
        if (count > 0) {
lubomir@319
    68
            ensureCapacity(size + count);
lubomir@319
    69
            arraycopy(newTypes, offset, types, size, count);
lubomir@319
    70
            size += count;
lubomir@319
    71
        }
lubomir@319
    72
    }
lubomir@319
    73
lubomir@319
    74
    public void set(final int index, final int newType) {
lubomir@319
    75
        types[index] = newType;
lubomir@319
    76
    }
lubomir@319
    77
lubomir@319
    78
    public void setAll(final TypeArray newTypes) {
lubomir@319
    79
        setAll(newTypes.types, 0, newTypes.size);
lubomir@319
    80
    }
lubomir@319
    81
lubomir@319
    82
    public void setAll(final int[] newTypes) {
lubomir@319
    83
        setAll(newTypes, 0, newTypes.length);
lubomir@319
    84
    }
lubomir@319
    85
lubomir@319
    86
    public void setAll(final int[] newTypes,
lubomir@319
    87
                       final int offset,
lubomir@319
    88
                       final int count) {
lubomir@319
    89
        if (count > 0) {
lubomir@319
    90
            ensureCapacity(count);
lubomir@319
    91
            arraycopy(newTypes, offset, types, 0, count);
lubomir@319
    92
            size = count;
lubomir@319
    93
        } else {
lubomir@319
    94
            clear();
lubomir@319
    95
        }
lubomir@319
    96
    }
lubomir@319
    97
lubomir@319
    98
    public void setSize(final int newSize) {
lubomir@319
    99
        if (size != newSize) {
lubomir@319
   100
            ensureCapacity(newSize);
lubomir@319
   101
lubomir@319
   102
            for (int i = size; i < newSize; ++i) {
lubomir@319
   103
                types[i] = 0;
lubomir@319
   104
            }
lubomir@319
   105
            size = newSize;
lubomir@319
   106
        }
lubomir@319
   107
    }
lubomir@319
   108
lubomir@319
   109
    public void clear() {
lubomir@319
   110
        size = 0;
lubomir@319
   111
    }
lubomir@319
   112
lubomir@319
   113
    public int getSize() {
lubomir@319
   114
        return size;
lubomir@319
   115
    }
lubomir@319
   116
lubomir@319
   117
    public int get(final int index) {
lubomir@319
   118
        return types[index];
lubomir@319
   119
    }
lubomir@319
   120
lubomir@319
   121
    public static String typeString(final int type) {
lubomir@319
   122
        switch (type & 0xff) {
lubomir@319
   123
            case ITEM_Bogus:
lubomir@319
   124
                return "_top_";
lubomir@319
   125
            case ITEM_Integer:
lubomir@319
   126
                return "_int_";
lubomir@319
   127
            case ITEM_Float:
lubomir@319
   128
                return "_float_";
lubomir@319
   129
            case ITEM_Double:
lubomir@319
   130
                return "_double_";
lubomir@319
   131
            case ITEM_Long:
lubomir@319
   132
                return "_long_";
lubomir@319
   133
            case ITEM_Null:
lubomir@319
   134
                return "_null_";
lubomir@319
   135
            case ITEM_InitObject: // UninitializedThis
lubomir@319
   136
                return "_init_";
lubomir@319
   137
            case ITEM_Object:
lubomir@319
   138
                return "_object_";
lubomir@319
   139
            case ITEM_NewObject: // Uninitialized
lubomir@319
   140
                return "_new_";
lubomir@319
   141
            default:
lubomir@319
   142
                throw new IllegalArgumentException("Unknown type");
lubomir@319
   143
        }
lubomir@319
   144
    }
lubomir@319
   145
lubomir@319
   146
    @Override
lubomir@319
   147
    public String toString() {
lubomir@319
   148
        final StringBuilder sb = new StringBuilder("[");
lubomir@319
   149
        if (size > 0) {
lubomir@319
   150
            sb.append(typeString(types[0]));
lubomir@319
   151
            for (int i = 1; i < size; ++i) {
lubomir@319
   152
                sb.append(", ");
lubomir@319
   153
                sb.append(typeString(types[i]));
lubomir@319
   154
            }
lubomir@319
   155
        }
lubomir@319
   156
lubomir@319
   157
        return sb.append(']').toString();
lubomir@319
   158
    }
lubomir@319
   159
lubomir@319
   160
    private void ensureCapacity(final int minCapacity) {
lubomir@319
   161
        if ((minCapacity == 0)
lubomir@319
   162
                || (types != null) && (minCapacity <= types.length)) {
lubomir@319
   163
            return;
lubomir@319
   164
        }
lubomir@319
   165
lubomir@319
   166
        final int newCapacity =
lubomir@319
   167
                ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
lubomir@319
   168
                    * CAPACITY_INCREMENT;
lubomir@319
   169
        final int[] newTypes = new int[newCapacity];
lubomir@319
   170
lubomir@319
   171
        if (size > 0) {
lubomir@319
   172
            arraycopy(types, 0, newTypes, 0, size);
lubomir@319
   173
        }
lubomir@319
   174
lubomir@319
   175
        types = newTypes;
lubomir@319
   176
    }
lubomir@319
   177
lubomir@319
   178
    // no System.arraycopy
lubomir@319
   179
    private void arraycopy(final int[] src, final int srcPos,
lubomir@319
   180
                           final int[] dest, final int destPos,
lubomir@319
   181
                           final int length) {
lubomir@319
   182
        for (int i = 0; i < length; ++i) {
lubomir@319
   183
            dest[destPos + i] = src[srcPos + i];
lubomir@319
   184
        }
lubomir@319
   185
    }
lubomir@319
   186
}