Bag.java

Go to the documentation of this file.
00001 package com.graphbuilder.struc;
00002 
00003 public class Bag {
00004 
00005     protected Object[] data = null;
00006     protected int size = 0;
00007 
00008     public Bag() {
00009         data = new Object[2];
00010     }
00011 
00012     public Bag(int initialCapacity) {
00013         data = new Object[initialCapacity];
00014     }
00015 
00016     public void add(Object o) {
00017         insert(o, size);
00018     }
00019 
00020     public int size() {
00021         return size;
00022     }
00023 
00024     public void insert(Object o, int i) {
00025         if (i < 0 || i > size)
00026             throw new IllegalArgumentException("required: (i >= 0 && i <= size) but: (i = " + i + ", size = " + size + ")");
00027 
00028         ensureCapacity(size + 1);
00029 
00030         for (int j = size; j > i; j--)
00031             data[j] = data[j - 1];
00032 
00033         data[i] = o;
00034         size++;
00035     }
00036 
00037     public void ensureCapacity(int minCapacity) {
00038         if (minCapacity > data.length) {
00039             int x = 2 * data.length;
00040 
00041             if (x < minCapacity)
00042                 x = minCapacity;
00043 
00044             Object[] arr = new Object[x];
00045 
00046             for (int i = 0; i < size; i++)
00047                 arr[i] = data[i];
00048 
00049             data = arr;
00050         }
00051     }
00052 
00053     public int getCapacity() {
00054         return data.length;
00055     }
00056 
00057     private int indexOf(Object o, int i, boolean forward) {
00058         if (size == 0) return -1;
00059 
00060         if (i < 0 || i >= size)
00061             throw new IllegalArgumentException("required: (i >= 0 && i < size) when: (size > 0) but: (i = " + i + ", size = " + size + ")");
00062 
00063         if (forward) {
00064             if (o == null) {
00065                 for (; i < size; i++)
00066                     if (data[i] == null)
00067                         return i;
00068             }
00069             else {
00070                 for (; i < size; i++)
00071                     if (o.equals(data[i]))
00072                         return i;
00073             }
00074         }
00075         else {
00076             if (o == null) {
00077                 for (; i >= 0; i--)
00078                     if (data[i] == null)
00079                         return i;
00080             }
00081             else {
00082                 for (; i >= 0; i--)
00083                     if (o.equals(data[i]))
00084                         return i;
00085             }
00086         }
00087         return -1;
00088     }
00089 
00090     public int remove(Object o) {
00091         int i = indexOf(o, 0, true);
00092         if (i >= 0)
00093             remove(i);
00094         return i;
00095     }
00096 
00097     public Object remove(int i) {
00098         if (i < 0 || i >= size)
00099             throw new IllegalArgumentException("required: (i >= 0 && i < size) but: (i = " + i + ", size = " + size + ")");
00100 
00101         Object o = data[i];
00102 
00103         for (int j = i + 1; j < size; j++)
00104             data[j-1] = data[j];
00105 
00106         data[--size] = null;
00107         return o;
00108     }
00109 
00110     public Object get(int i) {
00111         if (i < 0 || i >= size)
00112             throw new IllegalArgumentException("required: (i >= 0 && i < size) but: (i = " + i + ", size = " + size + ")");
00113 
00114         return data[i];
00115     }
00116 
00117     public boolean contains(Object o) {
00118         return indexOf(o, 0, true) >= 0;
00119     }
00120 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1