Source of ArrayBag2.java


  1: /**
  2:     A class that implements a bag of objects by using an array.
  3:     INCOMPLETE DEFINITION; includes security checks and definitions for
  4:     the methods isEmpty, getCurrentSize, getFrequencyOf, and contains.
  5:     @author Frank M. Carrano
  6:     @version 4.0
  7: */
  8: public final class ArrayBag2<T> implements BagInterface<T>
  9: {
 10:         private final T[] bag;
 11:         private int numberOfEntries;
 12:     private boolean initialized = false;
 13:         private static final int DEFAULT_CAPACITY = 25;
 14:         private static final int MAX_CAPACITY = 10000;
 15: 
 16:         /** Creates an empty bag whose initial capacity is 25. */
 17:         public ArrayBag2()
 18:         {
 19:                 this(DEFAULT_CAPACITY);
 20:         } // end default constructor
 21: 
 22:         /** Creates an empty bag having a given capacity.
 23:        @param desiredCapacity  The integer capacity desired. */
 24:         public ArrayBag2(int desiredCapacity)
 25:         {
 26:       if (desiredCapacity <= MAX_CAPACITY)
 27:       {
 28:          // The cast is safe because the new array contains null entries
 29:          @SuppressWarnings("unchecked")
 30:          T[] tempBag = (T[])new Object[desiredCapacity]; // Unchecked cast
 31:          bag = tempBag;
 32:          numberOfEntries = 0;
 33:          initialized = true;
 34:       }
 35:       else
 36:          throw new IllegalStateException("Attempt to create a bag " +
 37:                                          "whose capacity exceeds " +
 38:                                          "allowed maximum.");
 39:         } // end constructor
 40: 
 41:         /** Adds a new entry to this bag.
 42:        @param newEntry  The object to be added as a new entry.
 43:        @return  True if the addition is successful, or false if not. */
 44:         public boolean add(T newEntry)
 45:         {
 46:                 checkInitialization();
 47:       boolean result = true;
 48:       if (isArrayFull())
 49:       {
 50:          result = false;
 51:       }
 52:       else
 53:       {  // Assertion: result is true here
 54:          bag[numberOfEntries] = newEntry;
 55:          numberOfEntries++;
 56:       } // end if
 57: 
 58:       return result;
 59:         } // end add
 60: 
 61:         /** Retrieves all entries that are in this bag.
 62:        @return  A newly allocated array of all the entries in this bag. */
 63:         public T[] toArray()
 64:         {
 65:                 checkInitialization();
 66: 
 67:       // The cast is safe because the new array contains null entries.
 68:       @SuppressWarnings("unchecked")
 69:       T[] result = (T[])new Object[numberOfEntries]; // Unchecked cast
 70: 
 71:       for (int index = 0; index < numberOfEntries; index++)
 72:       {
 73:          result[index] = bag[index];
 74:       } // end for
 75:       return result;
 76:       // Note: The body of this method could consist of one return statement,
 77:       // if you call Arrays.copyOf
 78:         } // end toArray
 79: 
 80:         /** Sees whether this bag is empty.
 81:        @return  True if this bag is empty, or false if not. */
 82:         public boolean isEmpty()
 83:         {
 84:       return numberOfEntries == 0;
 85:         } // end isEmpty
 86: 
 87:         /** Gets the current number of entries in this bag.
 88:        @return  The integer number of entries currently in this bag. */
 89:         public int getCurrentSize()
 90:         {
 91:       return numberOfEntries;
 92:         } // end getCurrentSize
 93: 
 94:         /** Counts the number of times a given entry appears in this bag.
 95:        @param anEntry  The entry to be counted.
 96:        @return  The number of times anEntry appears in this ba. */
 97:         public int getFrequencyOf(T anEntry)
 98:         {
 99:                 checkInitialization();
100:       int counter = 0;
101: 
102:       for (int index = 0; index < numberOfEntries; index++)
103:       {
104:          if (anEntry.equals(bag[index]))
105:          {
106:             counter++;
107:          } // end if
108:       } // end for
109: 
110:       return counter;
111:         } // end getFrequencyOf
112: 
113:         /** Tests whether this bag contains a given entry.
114:        @param anEntry  The entry to locate.
115:        @return  True if this bag contains anEntry, or false otherwise. */
116:    public boolean contains(T anEntry)
117:         {
118:                 checkInitialization();
119:       boolean found = false;
120:                 int index = 0;
121:       while (!found && (index < numberOfEntries))
122:                 {
123:                         if (anEntry.equals(bag[index]))
124:                         {
125:                                 found = true;
126:                         } // end if
127:          index++;
128:                 } // end while
129:       return found;
130:    } // end contains
131: 
132:         /** Removes one unspecified entry from this bag, if possible.
133:        @return  Either the removed entry, if the removal
134:                 was successful, or null. */
135:         public T remove()
136:    {
137:       return null; // STUB
138:    } // end remove
139: 
140:         /** Removes one occurrence of a given entry from this bag.
141:        @param anEntry  The entry to be removed.
142:        @return  True if the removal was successful, or false if not. */
143:    public boolean remove(T anEntry)
144:    {
145:       return false; // STUB
146:    } // end remove
147: 
148:         /** Removes all entries from this bag. */
149:         public void clear()
150:    {
151:       // STUB
152:    } // end clear
153: 
154:    // Returns true if the array bag is full, or false if not.
155:         private boolean isArrayFull()
156:         {
157:                 return numberOfEntries >= bag.length;
158:         } // end isArrayFull
159:    // Throws an exception if this object is not initialized.
160:    private void checkInitialization()
161:    {
162:       if (!initialized)
163:          throw new SecurityException("ArrayBag object is not initialized properly.");
164:    } // end checkInitialization
165: } // end ArrayBag2
166: 
167: 
168: