javap/src/main/java/org/apidesign/javap/TypeArray.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Feb 2013 12:58:12 +0100
branchemul
changeset 694 0d277415ed02
parent 307 eaf4e8387065
permissions -rw-r--r--
Rebasing the Inflater support on jzlib which, unlike GNU ClassPath, has correct implementation of Huffman code. Making the implementation more easily testable by turning Inflater and ZipInputStream into pure delegates. Current implementation is going to need proper long support.
     1 /*
     2  * Copyright (c) 2012, 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 
    26 package org.apidesign.javap;
    27 
    28 import static org.apidesign.javap.RuntimeConstants.ITEM_Bogus;
    29 import static org.apidesign.javap.RuntimeConstants.ITEM_Integer;
    30 import static org.apidesign.javap.RuntimeConstants.ITEM_Float;
    31 import static org.apidesign.javap.RuntimeConstants.ITEM_Double;
    32 import static org.apidesign.javap.RuntimeConstants.ITEM_Long;
    33 import static org.apidesign.javap.RuntimeConstants.ITEM_Null;
    34 import static org.apidesign.javap.RuntimeConstants.ITEM_InitObject;
    35 import static org.apidesign.javap.RuntimeConstants.ITEM_Object;
    36 import static org.apidesign.javap.RuntimeConstants.ITEM_NewObject;
    37 
    38 public final class TypeArray {
    39     private static final int CAPACITY_INCREMENT = 16;
    40 
    41     private int[] types;
    42     private int size;
    43 
    44     public TypeArray() {
    45     }
    46     
    47     public TypeArray(final TypeArray initialTypes) {
    48         setAll(initialTypes);
    49     }
    50 
    51     public void add(final int newType) {
    52         ensureCapacity(size + 1);
    53         types[size++] = newType;
    54     }
    55 
    56     public void addAll(final TypeArray newTypes) {
    57         addAll(newTypes.types, 0, newTypes.size);
    58     }
    59 
    60     public void addAll(final int[] newTypes) {
    61         addAll(newTypes, 0, newTypes.length);
    62     }
    63 
    64     public void addAll(final int[] newTypes,
    65                        final int offset,
    66                        final int count) {
    67         if (count > 0) {
    68             ensureCapacity(size + count);
    69             arraycopy(newTypes, offset, types, size, count);
    70             size += count;
    71         }
    72     }
    73 
    74     public void set(final int index, final int newType) {
    75         types[index] = newType;
    76     }
    77 
    78     public void setAll(final TypeArray newTypes) {
    79         setAll(newTypes.types, 0, newTypes.size);
    80     }
    81 
    82     public void setAll(final int[] newTypes) {
    83         setAll(newTypes, 0, newTypes.length);
    84     }
    85 
    86     public void setAll(final int[] newTypes,
    87                        final int offset,
    88                        final int count) {
    89         if (count > 0) {
    90             ensureCapacity(count);
    91             arraycopy(newTypes, offset, types, 0, count);
    92             size = count;
    93         } else {
    94             clear();
    95         }
    96     }
    97 
    98     public void setSize(final int newSize) {
    99         if (size != newSize) {
   100             ensureCapacity(newSize);
   101 
   102             for (int i = size; i < newSize; ++i) {
   103                 types[i] = 0;
   104             }
   105             size = newSize;
   106         }
   107     }
   108 
   109     public void clear() {
   110         size = 0;
   111     }
   112 
   113     public int getSize() {
   114         return size;
   115     }
   116 
   117     public int get(final int index) {
   118         return types[index];
   119     }
   120 
   121     public static String typeString(final int type) {
   122         switch (type & 0xff) {
   123             case ITEM_Bogus:
   124                 return "_top_";
   125             case ITEM_Integer:
   126                 return "_int_";
   127             case ITEM_Float:
   128                 return "_float_";
   129             case ITEM_Double:
   130                 return "_double_";
   131             case ITEM_Long:
   132                 return "_long_";
   133             case ITEM_Null:
   134                 return "_null_";
   135             case ITEM_InitObject: // UninitializedThis
   136                 return "_init_";
   137             case ITEM_Object:
   138                 return "_object_";
   139             case ITEM_NewObject: // Uninitialized
   140                 return "_new_";
   141             default:
   142                 throw new IllegalArgumentException("Unknown type");
   143         }
   144     }
   145 
   146     @Override
   147     public String toString() {
   148         final StringBuilder sb = new StringBuilder("[");
   149         if (size > 0) {
   150             sb.append(typeString(types[0]));
   151             for (int i = 1; i < size; ++i) {
   152                 sb.append(", ");
   153                 sb.append(typeString(types[i]));
   154             }
   155         }
   156 
   157         return sb.append(']').toString();
   158     }
   159 
   160     private void ensureCapacity(final int minCapacity) {
   161         if ((minCapacity == 0)
   162                 || (types != null) && (minCapacity <= types.length)) {
   163             return;
   164         }
   165 
   166         final int newCapacity =
   167                 ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT)
   168                     * CAPACITY_INCREMENT;
   169         final int[] newTypes = new int[newCapacity];
   170 
   171         if (size > 0) {
   172             arraycopy(types, 0, newTypes, 0, size);
   173         }
   174 
   175         types = newTypes;
   176     }
   177 
   178     // no System.arraycopy
   179     private void arraycopy(final int[] src, final int srcPos,
   180                            final int[] dest, final int destPos,
   181                            final int length) {
   182         for (int i = 0; i < length; ++i) {
   183             dest[destPos + i] = src[srcPos + i];
   184         }
   185     }
   186 }