Esempio n. 1
0
  private static class Sequence {
    String key;
    String name;
    Color color;
    boolean isPlotted;
    Stroke transitionStroke = null;

    // Values are stored in an int[] if all values will fit,
    // otherwise in a long[]. An int can represent up to 2 GB.
    // Use a random start size, so all arrays won't need to
    // be grown during the same update interval
    Object values = new byte[ARRAY_SIZE_INCREMENT + (int) (Math.random() * 100)];

    // Number of stored values
    int size = 0;

    public Sequence(String key) {
      this.key = key;
    }

    /** Returns the value at index i */
    public long value(int i) {
      return Array.getLong(values, i);
    }

    public void add(long value) {
      // May need to switch to a larger array type
      if ((values instanceof byte[] || values instanceof short[] || values instanceof int[])
          && value > Integer.MAX_VALUE) {
        long[] la = new long[Array.getLength(values)];
        for (int i = 0; i < size; i++) {
          la[i] = Array.getLong(values, i);
        }
        values = la;
      } else if ((values instanceof byte[] || values instanceof short[])
          && value > Short.MAX_VALUE) {
        int[] ia = new int[Array.getLength(values)];
        for (int i = 0; i < size; i++) {
          ia[i] = Array.getInt(values, i);
        }
        values = ia;
      } else if (values instanceof byte[] && value > Byte.MAX_VALUE) {
        short[] sa = new short[Array.getLength(values)];
        for (int i = 0; i < size; i++) {
          sa[i] = Array.getShort(values, i);
        }
        values = sa;
      }

      // May need to extend the array size
      if (Array.getLength(values) == size) {
        values = extendArray(values);
      }

      // Store the value
      if (values instanceof long[]) {
        ((long[]) values)[size] = value;
      } else if (values instanceof int[]) {
        ((int[]) values)[size] = (int) value;
      } else if (values instanceof short[]) {
        ((short[]) values)[size] = (short) value;
      } else {
        ((byte[]) values)[size] = (byte) value;
      }
      size++;
    }
  }