javap/src/main/java/org/apidesign/javap/TypeArray.java
branchregisters
changeset 307 eaf4e8387065
parent 281 f2352e0b713e
child 319 83f638b13242
     1.1 --- a/javap/src/main/java/org/apidesign/javap/TypeArray.java	Fri Dec 07 15:02:35 2012 +0100
     1.2 +++ b/javap/src/main/java/org/apidesign/javap/TypeArray.java	Wed Dec 12 11:04:02 2012 +0100
     1.3 @@ -25,38 +25,85 @@
     1.4  
     1.5  package org.apidesign.javap;
     1.6  
     1.7 -public class TypeArray {
     1.8 +import static org.apidesign.javap.RuntimeConstants.ITEM_Bogus;
     1.9 +import static org.apidesign.javap.RuntimeConstants.ITEM_Integer;
    1.10 +import static org.apidesign.javap.RuntimeConstants.ITEM_Float;
    1.11 +import static org.apidesign.javap.RuntimeConstants.ITEM_Double;
    1.12 +import static org.apidesign.javap.RuntimeConstants.ITEM_Long;
    1.13 +import static org.apidesign.javap.RuntimeConstants.ITEM_Null;
    1.14 +import static org.apidesign.javap.RuntimeConstants.ITEM_InitObject;
    1.15 +import static org.apidesign.javap.RuntimeConstants.ITEM_Object;
    1.16 +import static org.apidesign.javap.RuntimeConstants.ITEM_NewObject;
    1.17 +
    1.18 +public final class TypeArray {
    1.19      private static final int CAPACITY_INCREMENT = 16;
    1.20  
    1.21      private int[] types;
    1.22      private int size;
    1.23  
    1.24 +    public TypeArray() {
    1.25 +    }
    1.26 +    
    1.27 +    public TypeArray(final TypeArray initialTypes) {
    1.28 +        setAll(initialTypes);
    1.29 +    }
    1.30 +
    1.31      public void add(final int newType) {
    1.32          ensureCapacity(size + 1);
    1.33          types[size++] = newType;
    1.34      }
    1.35  
    1.36 +    public void addAll(final TypeArray newTypes) {
    1.37 +        addAll(newTypes.types, 0, newTypes.size);
    1.38 +    }
    1.39 +
    1.40      public void addAll(final int[] newTypes) {
    1.41 -        if (newTypes.length > 0) {
    1.42 -            ensureCapacity(size + newTypes.length);
    1.43 -            arraycopy(newTypes, 0, types, size, newTypes.length);
    1.44 -            size += newTypes.length;
    1.45 +        addAll(newTypes, 0, newTypes.length);
    1.46 +    }
    1.47 +
    1.48 +    public void addAll(final int[] newTypes,
    1.49 +                       final int offset,
    1.50 +                       final int count) {
    1.51 +        if (count > 0) {
    1.52 +            ensureCapacity(size + count);
    1.53 +            arraycopy(newTypes, offset, types, size, count);
    1.54 +            size += count;
    1.55          }
    1.56      }
    1.57  
    1.58 +    public void set(final int index, final int newType) {
    1.59 +        types[index] = newType;
    1.60 +    }
    1.61 +
    1.62 +    public void setAll(final TypeArray newTypes) {
    1.63 +        setAll(newTypes.types, 0, newTypes.size);
    1.64 +    }
    1.65 +
    1.66      public void setAll(final int[] newTypes) {
    1.67 -        if (newTypes.length > 0) {
    1.68 -            ensureCapacity(newTypes.length);
    1.69 -            arraycopy(newTypes, 0, types, 0, newTypes.length);
    1.70 -            size = newTypes.length;
    1.71 +        setAll(newTypes, 0, newTypes.length);
    1.72 +    }
    1.73 +
    1.74 +    public void setAll(final int[] newTypes,
    1.75 +                       final int offset,
    1.76 +                       final int count) {
    1.77 +        if (count > 0) {
    1.78 +            ensureCapacity(count);
    1.79 +            arraycopy(newTypes, offset, types, 0, count);
    1.80 +            size = count;
    1.81          } else {
    1.82              clear();
    1.83          }
    1.84      }
    1.85  
    1.86      public void setSize(final int newSize) {
    1.87 -        ensureCapacity(newSize);
    1.88 -        size = newSize;
    1.89 +        if (size != newSize) {
    1.90 +            ensureCapacity(newSize);
    1.91 +
    1.92 +            for (int i = size; i < newSize; ++i) {
    1.93 +                types[i] = 0;
    1.94 +            }
    1.95 +            size = newSize;
    1.96 +        }
    1.97      }
    1.98  
    1.99      public void clear() {
   1.100 @@ -71,6 +118,45 @@
   1.101          return types[index];
   1.102      }
   1.103  
   1.104 +    public static String typeString(final int type) {
   1.105 +        switch (type & 0xff) {
   1.106 +            case ITEM_Bogus:
   1.107 +                return "_top_";
   1.108 +            case ITEM_Integer:
   1.109 +                return "_int_";
   1.110 +            case ITEM_Float:
   1.111 +                return "_float_";
   1.112 +            case ITEM_Double:
   1.113 +                return "_double_";
   1.114 +            case ITEM_Long:
   1.115 +                return "_long_";
   1.116 +            case ITEM_Null:
   1.117 +                return "_null_";
   1.118 +            case ITEM_InitObject: // UninitializedThis
   1.119 +                return "_init_";
   1.120 +            case ITEM_Object:
   1.121 +                return "_object_";
   1.122 +            case ITEM_NewObject: // Uninitialized
   1.123 +                return "_new_";
   1.124 +            default:
   1.125 +                throw new IllegalArgumentException("Unknown type");
   1.126 +        }
   1.127 +    }
   1.128 +
   1.129 +    @Override
   1.130 +    public String toString() {
   1.131 +        final StringBuilder sb = new StringBuilder("[");
   1.132 +        if (size > 0) {
   1.133 +            sb.append(typeString(types[0]));
   1.134 +            for (int i = 1; i < size; ++i) {
   1.135 +                sb.append(", ");
   1.136 +                sb.append(typeString(types[i]));
   1.137 +            }
   1.138 +        }
   1.139 +
   1.140 +        return sb.append(']').toString();
   1.141 +    }
   1.142 +
   1.143      private void ensureCapacity(final int minCapacity) {
   1.144          if ((minCapacity == 0)
   1.145                  || (types != null) && (minCapacity <= types.length)) {