home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / OSpace / jgl.exe / jgl_2_0 / COM / objectspace / jgl / Array.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-03-12  |  8.2 KB  |  527 lines

  1. package COM.objectspace.jgl;
  2.  
  3. import java.util.Enumeration;
  4.  
  5. public class Array implements Sequence {
  6.    Object[] myStorage;
  7.    int myLength;
  8.    static final int DEFAULT_SIZE = 10;
  9.    static final int THRESHOLD = 2000;
  10.    static final int MULTIPLIER = 2;
  11.  
  12.    public Array() {
  13.       this.myStorage = new Object[10];
  14.    }
  15.  
  16.    public Array(int var1) {
  17.       if (var1 < 0) {
  18.          throw new IllegalArgumentException("Attempt to create an Array with a negative size");
  19.       } else {
  20.          this.myLength = var1;
  21.          this.myStorage = new Object[this.myLength];
  22.       }
  23.    }
  24.  
  25.    public Array(int var1, Object var2) {
  26.       this(var1);
  27.  
  28.       for(int var3 = 0; var3 < this.myLength; ++var3) {
  29.          this.myStorage[var3] = var2;
  30.       }
  31.  
  32.    }
  33.  
  34.    public Array(Object[] var1) {
  35.       synchronized(var1){}
  36.  
  37.       try {
  38.          this.myStorage = var1;
  39.          this.myLength = var1.length;
  40.       } catch (Throwable var4) {
  41.          throw var4;
  42.       }
  43.  
  44.    }
  45.  
  46.    public Array(Array var1) {
  47.       synchronized(var1){}
  48.  
  49.       try {
  50.          this.myLength = var1.myLength;
  51.          this.myStorage = new Object[this.myLength];
  52.          System.arraycopy(var1.myStorage, 0, this.myStorage, 0, this.myLength);
  53.       } catch (Throwable var4) {
  54.          throw var4;
  55.       }
  56.  
  57.    }
  58.  
  59.    public synchronized Object clone() {
  60.       return new Array(this);
  61.    }
  62.  
  63.    public boolean equals(Object var1) {
  64.       return var1 instanceof Array && this.equals((Array)var1);
  65.    }
  66.  
  67.    public synchronized boolean equals(Array var1) {
  68.       synchronized(var1){}
  69.  
  70.       boolean var2;
  71.       try {
  72.          var2 = this.size() == var1.size() && Comparing.equal(this.start(), this.finish(), var1.start());
  73.       } catch (Throwable var8) {
  74.          throw var8;
  75.       }
  76.  
  77.       return var2;
  78.    }
  79.  
  80.    public synchronized int hashCode() {
  81.       return Hashing.orderedHash(this.start(), this.finish());
  82.    }
  83.  
  84.    public synchronized String toString() {
  85.       return "Array" + Printing.toString(this.start(), this.finish());
  86.    }
  87.  
  88.    public synchronized void copy(Array var1) {
  89.       if (this != var1) {
  90.          synchronized(var1){}
  91.  
  92.          try {
  93.             if (var1.myLength > this.myStorage.length) {
  94.                this.myStorage = new Object[var1.myLength];
  95.                System.arraycopy(var1.myStorage, 0, this.myStorage, 0, var1.myLength);
  96.             } else if (this.myLength > var1.myLength) {
  97.                System.arraycopy(var1.myStorage, 0, this.myStorage, 0, var1.myLength);
  98.  
  99.                for(int var4 = var1.myLength; var4 < this.myLength; ++var4) {
  100.                   this.myStorage[var4] = null;
  101.                }
  102.             } else {
  103.                System.arraycopy(var1.myStorage, 0, this.myStorage, 0, var1.myLength);
  104.             }
  105.  
  106.             this.myLength = var1.myLength;
  107.          } catch (Throwable var6) {
  108.             throw var6;
  109.          }
  110.  
  111.       }
  112.    }
  113.  
  114.    public synchronized void copyTo(Object[] var1) {
  115.       synchronized(var1){}
  116.  
  117.       try {
  118.          if (this.myLength < var1.length) {
  119.             System.arraycopy(this.myStorage, 0, var1, 0, this.myLength);
  120.          } else {
  121.             System.arraycopy(this.myStorage, 0, var1, 0, var1.length);
  122.          }
  123.       } catch (Throwable var4) {
  124.          throw var4;
  125.       }
  126.  
  127.    }
  128.  
  129.    public boolean isEmpty() {
  130.       return this.myLength == 0;
  131.    }
  132.  
  133.    public int size() {
  134.       return this.myLength;
  135.    }
  136.  
  137.    public int maxSize() {
  138.       return Integer.MAX_VALUE;
  139.    }
  140.  
  141.    public int capacity() {
  142.       return this.myStorage.length;
  143.    }
  144.  
  145.    public synchronized Object back() {
  146.       if (this.myLength == 0) {
  147.          throw new InvalidOperationException("Array is empty");
  148.       } else {
  149.          return this.myStorage[this.myLength - 1];
  150.       }
  151.    }
  152.  
  153.    public synchronized Object front() {
  154.       if (this.myLength == 0) {
  155.          throw new InvalidOperationException("Array is empty");
  156.       } else {
  157.          return this.myStorage[0];
  158.       }
  159.    }
  160.  
  161.    // $FF: renamed from: at (int) java.lang.Object
  162.    public synchronized Object method_0(int var1) {
  163.       if (var1 >= 0 && var1 < this.myLength) {
  164.          return this.myStorage[var1];
  165.       } else {
  166.          throw new IndexOutOfBoundsException("Attempt to access index " + var1 + " when valid range is 0.." + (this.myLength - 1));
  167.       }
  168.    }
  169.  
  170.    public synchronized void put(int var1, Object var2) {
  171.       if (var1 >= this.myLength) {
  172.          throw new IndexOutOfBoundsException("Attempt to access index " + var1 + " when valid range is 0.." + (this.myLength - 1));
  173.       } else {
  174.          this.myStorage[var1] = var2;
  175.       }
  176.    }
  177.  
  178.    public synchronized void clear() {
  179.       this.myStorage = new Object[10];
  180.       this.myLength = 0;
  181.    }
  182.  
  183.    public Object remove(Enumeration var1) {
  184.       if (!(var1 instanceof ArrayIterator)) {
  185.          throw new IllegalArgumentException("Enumeration not an ArrayIterator");
  186.       } else if (((ArrayIterator)var1).myArray != this) {
  187.          throw new IllegalArgumentException("Enumeration not for this Array ");
  188.       } else {
  189.          ArrayIterator var2 = (ArrayIterator)var1;
  190.          Object var3 = var2.myArray.method_0(var2.myIndex);
  191.          this.remove(((ArrayIterator)var1).myIndex);
  192.          return var3;
  193.       }
  194.    }
  195.  
  196.    public synchronized Object remove(int var1) {
  197.       if (var1 >= 0 && var1 < this.myLength) {
  198.          Object var2 = this.myStorage[var1];
  199.          System.arraycopy(this.myStorage, var1 + 1, this.myStorage, var1, this.myLength - var1 - 1);
  200.          this.myStorage[--this.myLength] = null;
  201.          return var2;
  202.       } else {
  203.          throw new IndexOutOfBoundsException("Attempt to access index " + var1 + " when valid range is 0.." + (this.myLength - 1));
  204.       }
  205.    }
  206.  
  207.    public int remove(Enumeration var1, Enumeration var2) {
  208.       if (var1 instanceof ArrayIterator && var2 instanceof ArrayIterator) {
  209.          if (((ArrayIterator)var1).myArray == this && ((ArrayIterator)var2).myArray == this) {
  210.             return this.remove(((ArrayIterator)var1).myIndex, ((ArrayIterator)var2).myIndex - 1);
  211.          } else {
  212.             throw new IllegalArgumentException("Enumeration not for this Array ");
  213.          }
  214.       } else {
  215.          throw new IllegalArgumentException("Enumeration not an ArrayIterator");
  216.       }
  217.    }
  218.  
  219.    public synchronized int remove(int var1, int var2) {
  220.       if (var2 < var1) {
  221.          return 0;
  222.       } else {
  223.          this.checkRange(var1, var2);
  224.          int var3 = var2 - var1 + 1;
  225.          System.arraycopy(this.myStorage, var2 + 1, this.myStorage, var1, this.myLength - var2 - 1);
  226.  
  227.          for(int var4 = this.myLength - var3; var4 < this.myLength; ++var4) {
  228.             this.myStorage[var4] = null;
  229.          }
  230.  
  231.          this.myLength -= var3;
  232.          return var3;
  233.       }
  234.    }
  235.  
  236.    public synchronized Object popBack() {
  237.       if (this.myLength == 0) {
  238.          throw new InvalidOperationException("Array is empty");
  239.       } else {
  240.          Object var1 = this.myStorage[--this.myLength];
  241.          this.myStorage[this.myLength] = null;
  242.          return var1;
  243.       }
  244.    }
  245.  
  246.    public synchronized Object add(Object var1) {
  247.       if (this.myLength == this.myStorage.length) {
  248.          Object[] var2 = this.getNextStorage();
  249.          System.arraycopy(this.myStorage, 0, var2, 0, this.myLength);
  250.          this.myStorage = var2;
  251.       }
  252.  
  253.       this.myStorage[this.myLength++] = var1;
  254.       return null;
  255.    }
  256.  
  257.    public void pushBack(Object var1) {
  258.       this.add(var1);
  259.    }
  260.  
  261.    public ArrayIterator insert(ArrayIterator var1, Object var2) {
  262.       this.insert(var1.myIndex, var2);
  263.       return new ArrayIterator(this, var1.myIndex);
  264.    }
  265.  
  266.    public synchronized void insert(int var1, Object var2) {
  267.       if (var1 > this.myLength) {
  268.          throw new IndexOutOfBoundsException("Attempt to insert at index " + var1 + " when valid range is 0.." + this.myLength);
  269.       } else {
  270.          if (this.myLength != this.myStorage.length) {
  271.             if (var1 != this.myLength) {
  272.                System.arraycopy(this.myStorage, var1, this.myStorage, var1 + 1, this.myLength - var1);
  273.             }
  274.          } else {
  275.             Object[] var3 = this.getNextStorage();
  276.             System.arraycopy(this.myStorage, 0, var3, 0, var1);
  277.             System.arraycopy(this.myStorage, var1, var3, var1 + 1, this.myLength - var1);
  278.             this.myStorage = var3;
  279.          }
  280.  
  281.          this.myStorage[var1] = var2;
  282.          ++this.myLength;
  283.       }
  284.    }
  285.  
  286.    public void insert(ArrayIterator var1, int var2, Object var3) {
  287.       this.insert(var1.myIndex, var2, var3);
  288.    }
  289.  
  290.    public synchronized void insert(int var1, int var2, Object var3) {
  291.       if (var1 >= 0 && var1 <= this.myLength) {
  292.          if (var2 < 0) {
  293.             throw new IllegalArgumentException("Attempt to insert a negative number of objects.");
  294.          } else if (var2 != 0) {
  295.             if (this.myStorage.length - this.myLength >= var2) {
  296.                System.arraycopy(this.myStorage, var1, this.myStorage, var1 + var2, this.myLength - var1);
  297.             } else {
  298.                Object[] var4 = this.getNextStorage(var2);
  299.                System.arraycopy(this.myStorage, 0, var4, 0, var1);
  300.                System.arraycopy(this.myStorage, var1, var4, var1 + var2, this.myLength - var1);
  301.                this.myStorage = var4;
  302.             }
  303.  
  304.             for(int var5 = var1; var5 < var1 + var2; ++var5) {
  305.                this.myStorage[var5] = var3;
  306.             }
  307.  
  308.             this.myLength += var2;
  309.          }
  310.       } else {
  311.          throw new IndexOutOfBoundsException("Attempt to insert at index " + var1 + " when valid range is 0.." + this.myLength);
  312.       }
  313.    }
  314.  
  315.    public void insert(ArrayIterator var1, ForwardIterator var2, ForwardIterator var3) {
  316.       this.insert(var1.myIndex, var2, var3);
  317.    }
  318.  
  319.    public synchronized void insert(int var1, ForwardIterator var2, ForwardIterator var3) {
  320.       int var4 = var2.distance(var3);
  321.       if (var4 != 0) {
  322.          ForwardIterator var5 = (ForwardIterator)var2.clone();
  323.          if (this.myStorage.length - this.myLength >= var4) {
  324.             System.arraycopy(this.myStorage, var1, this.myStorage, var1 + var4, this.myLength - var1);
  325.          } else {
  326.             Object[] var6 = this.getNextStorage(var4);
  327.             System.arraycopy(this.myStorage, 0, var6, 0, var1);
  328.             System.arraycopy(this.myStorage, var1, var6, var1 + var4, this.myLength - var1);
  329.             this.myStorage = var6;
  330.          }
  331.  
  332.          for(int var7 = var1; var7 < var1 + var4; ++var7) {
  333.             this.myStorage[var7] = var5.nextElement();
  334.          }
  335.  
  336.          this.myLength += var4;
  337.       }
  338.    }
  339.  
  340.    public synchronized void swap(Array var1) {
  341.       synchronized(var1){}
  342.  
  343.       try {
  344.          int var4 = this.myLength;
  345.          Object[] var5 = this.myStorage;
  346.          this.myLength = var1.myLength;
  347.          this.myStorage = var1.myStorage;
  348.          var1.myLength = var4;
  349.          var1.myStorage = var5;
  350.       } catch (Throwable var7) {
  351.          throw var7;
  352.       }
  353.  
  354.    }
  355.  
  356.    public synchronized Enumeration elements() {
  357.       return new ArrayIterator(this, 0);
  358.    }
  359.  
  360.    public ForwardIterator start() {
  361.       return this.begin();
  362.    }
  363.  
  364.    public ForwardIterator finish() {
  365.       return this.end();
  366.    }
  367.  
  368.    public synchronized ArrayIterator begin() {
  369.       return new ArrayIterator(this, 0);
  370.    }
  371.  
  372.    public synchronized ArrayIterator end() {
  373.       return new ArrayIterator(this, this.myLength);
  374.    }
  375.  
  376.    public synchronized void trimToSize() {
  377.       if (this.myLength < this.myStorage.length) {
  378.          Object[] var1 = this.myStorage;
  379.          this.myStorage = new Object[this.myLength];
  380.          System.arraycopy(var1, 0, this.myStorage, 0, this.myLength);
  381.       }
  382.  
  383.    }
  384.  
  385.    public synchronized void ensureCapacity(int var1) {
  386.       if (var1 < 0) {
  387.          throw new IllegalArgumentException("Attempt to reserve a negative size.");
  388.       } else {
  389.          if (this.myStorage.length < var1) {
  390.             Object[] var2 = new Object[var1];
  391.             if (this.myLength > 0) {
  392.                System.arraycopy(this.myStorage, 0, var2, 0, this.myLength);
  393.             }
  394.  
  395.             this.myStorage = var2;
  396.          }
  397.  
  398.       }
  399.    }
  400.  
  401.    public synchronized Object popFront() {
  402.       if (this.myLength == 0) {
  403.          throw new InvalidOperationException("Array is empty");
  404.       } else {
  405.          Object var1 = this.myStorage[0];
  406.          this.remove(0);
  407.          return var1;
  408.       }
  409.    }
  410.  
  411.    public void pushFront(Object var1) {
  412.       this.insert(0, var1);
  413.    }
  414.  
  415.    public int remove(Object var1) {
  416.       return this.remove(0, this.myLength - 1, var1);
  417.    }
  418.  
  419.    public synchronized int remove(Object var1, int var2) {
  420.       int var3 = 0;
  421.  
  422.       while(var2 > 0) {
  423.          int var4 = this.indexOf(var1);
  424.          if (var4 >= 0) {
  425.             --var2;
  426.             ++var3;
  427.             this.remove(var4);
  428.          }
  429.       }
  430.  
  431.       return var3;
  432.    }
  433.  
  434.    public synchronized int remove(int var1, int var2, Object var3) {
  435.       if (var2 < var1) {
  436.          return 0;
  437.       } else {
  438.          this.checkRange(var1, var2);
  439.          ArrayIterator var4 = new ArrayIterator(this, var1);
  440.          ArrayIterator var5 = new ArrayIterator(this, var2 + 1);
  441.          ArrayIterator var6 = (ArrayIterator)Removing.remove(var4, var5, var3);
  442.          return this.remove(var6.myIndex, var2);
  443.       }
  444.    }
  445.  
  446.    public synchronized int replace(Object var1, Object var2) {
  447.       return Replacing.replace(this.start(), this.finish(), var1, var2);
  448.    }
  449.  
  450.    public synchronized int replace(int var1, int var2, Object var3, Object var4) {
  451.       if (var2 < var1) {
  452.          return 0;
  453.       } else {
  454.          this.checkRange(var1, var2);
  455.          return Replacing.replace(new ArrayIterator(this, var1), new ArrayIterator(this, var2 + 1), var3, var4);
  456.       }
  457.    }
  458.  
  459.    public synchronized int count(Object var1) {
  460.       return Counting.count(this.start(), this.finish(), var1);
  461.    }
  462.  
  463.    public synchronized int count(int var1, int var2, Object var3) {
  464.       if (var2 < var1) {
  465.          return 0;
  466.       } else {
  467.          this.checkRange(var1, var2);
  468.          return Counting.count(new ArrayIterator(this, var1), new ArrayIterator(this, var2 + 1), var3);
  469.       }
  470.    }
  471.  
  472.    public int indexOf(Object var1) {
  473.       return this.indexOf(0, this.myLength - 1, var1);
  474.    }
  475.  
  476.    public synchronized int indexOf(int var1, int var2, Object var3) {
  477.       if (var2 < var1) {
  478.          return -1;
  479.       } else {
  480.          this.checkRange(var1, var2);
  481.          int var4 = ((ArrayIterator)Finding.find(new ArrayIterator(this, var1), new ArrayIterator(this, var2 + 1), var3)).myIndex;
  482.          return var4 == var2 + 1 ? -1 : var4;
  483.       }
  484.    }
  485.  
  486.    public synchronized void setSize(int var1) {
  487.       if (this.myLength > var1) {
  488.          this.remove(var1, this.myLength - 1);
  489.       } else {
  490.          if (this.myLength < var1) {
  491.             this.insert(this.myLength, var1 - this.myLength, (Object)null);
  492.          }
  493.  
  494.       }
  495.    }
  496.  
  497.    public boolean contains(Object var1) {
  498.       return this.indexOf(var1) != -1;
  499.    }
  500.  
  501.    private void checkRange(int var1, int var2) {
  502.       if (var1 >= 0 && var1 < this.myLength) {
  503.          if (var2 < 0 || var2 >= this.myLength) {
  504.             throw new IndexOutOfBoundsException("Attempt to access index " + var2 + " when valid range is 0.." + (this.myLength - 1));
  505.          }
  506.       } else {
  507.          throw new IndexOutOfBoundsException("Attempt to access index " + var1 + " when valid range is 0.." + (this.myLength - 1));
  508.       }
  509.    }
  510.  
  511.    private int getNextSize() {
  512.       int var1 = this.myLength > 2000 ? this.myLength + 2000 : this.myLength * 2;
  513.       return Math.max(1, var1);
  514.    }
  515.  
  516.    private Object[] getNextStorage() {
  517.       Object[] var1 = new Object[this.getNextSize()];
  518.       return var1;
  519.    }
  520.  
  521.    private Object[] getNextStorage(int var1) {
  522.       int var2 = Math.max(this.getNextSize(), this.myLength + var1);
  523.       Object[] var3 = new Object[var2];
  524.       return var3;
  525.    }
  526. }
  527.