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, Timothy M. Henry
  6:     @version 5.0
  7: */
  8: public final class ArrayBag2<T> implements BagInterface<T>
  9: {
 10:         private final T[] bag;
 11:         private int numberOfEntries;
 12:    private boolean integrityOK = 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:          integrityOK = true;
 34:       }
 35:       else
 36:          throw new IllegalStateException("Attempt to create a bag whose" +
 37:                                          "capacity exceeds allowed maximum.");
 38:         } // end constructor
 39:    
 40:         /** Adds a new entry to this bag.
 41:        @param newEntry  The object to be added as a new entry.
 42:        @return  True if the addition is successful, or false if not. */
 43:         public boolean add(T newEntry)
 44:         {
 45:                 checkIntegrity();
 46:       boolean result = true;
 47:       if (isArrayFull())
 48:       {
 49:          result = false;
 50:       }
 51:       else
 52:       {  // Assertion: result is true here
 53:          bag[numberOfEntries] = newEntry;
 54:          numberOfEntries++;
 55:       } // end if
 56:       
 57:       return result;
 58:         } // end add
 59:    
 60:         /** Retrieves all entries that are in this bag.
 61:        @return  A newly allocated array of all the entries in this bag. */
 62:         public T[] toArray()
 63:         {
 64:                 checkIntegrity();
 65:       
 66:       // The cast is safe because the new array contains null entries.
 67:       @SuppressWarnings("unchecked")
 68:       T[] result = (T[])new Object[numberOfEntries]; // Unchecked cast
 69:       
 70:       for (int index = 0; index < numberOfEntries; index++)
 71:       {
 72:          result[index] = bag[index];
 73:       } // end for
 74:       return result;
 75:       // Note: The body of this method could consist of one return statement,
 76:       // if you call Arrays.copyOf
 77:         } // end toArray
 78:    
 79:         /** Sees whether this bag is empty.
 80:        @return  True if this bag is empty, or false if not. */
 81:         public boolean isEmpty()
 82:         {
 83:       return numberOfEntries == 0;
 84:         } // end isEmpty
 85:    
 86:         /** Gets the current number of entries in this bag.
 87:        @return  The integer number of entries currently in this bag. */
 88:         public int getCurrentSize()
 89:         {
 90:       return numberOfEntries;
 91:         } // end getCurrentSize

 93:         /** Counts the number of times a given entry appears in this bag.
 94:        @param anEntry  The entry to be counted.
 95:        @return  The number of times anEntry appears in this ba. */
 96:         public int getFrequencyOf(T anEntry)
 97:         {
 98:                 checkIntegrity();
 99:       int counter = 0;
100:       
101:       for (int index = 0; index < numberOfEntries; index++)
102:       {
103:          if (anEntry.equals(bag[index]))
104:          {
105:             counter++;
106:          } // end if
107:       } // end for
108:       
109:       return counter;
110:         } // end getFrequencyOf
111:    
112:         /** Tests whether this bag contains a given entry.
113:        @param anEntry  The entry to locate.
114:        @return  True if this bag contains anEntry, or false otherwise. */
115:    public boolean contains(T anEntry)
116:         {
117:                 checkIntegrity();
118:       boolean found = false;
119:                 int index = 0;      
120:       while (!found && (index < numberOfEntries))
121:                 {
122:                         if (anEntry.equals(bag[index]))
123:                         {
124:                                 found = true;
125:                         } // end if
126:          index++;
127:                 } // end while
128:       return found;
129:    } // end contains
130:    
131:         /** Removes one unspecified entry from this bag, if possible.
132:        @return  Either the removed entry, if the removal
133:                 was successful, or null. */
134:         public T remove()
135:    {
136:       return null; // STUB
137:    } // end remove
138:    
139:         /** Removes one occurrence of a given entry from this bag.
140:        @param anEntry  The entry to be removed.
141:        @return  True if the removal was successful, or false if not. */
142:    public boolean remove(T anEntry)
143:    {
144:       return false; // STUB
145:    } // end remove
146:         
147:         /** Removes all entries from this bag. */
148:         public void clear()
149:    {
150:       // STUB
151:    } // end clear
152:    
153:    // Returns true if the array bag is full, or false if not.
154:         private boolean isArrayFull()
155:         {
156:                 return numberOfEntries >= bag.length;
157:         } // end isArrayFull
158:    
159:    // Throws an exception if this object is not initialized.
160:    private void checkIntegrity()
161:    {
162:       if (!integrityOK)
163:          throw new SecurityException("ArrayBag object is corrupt.");
164:    } // end checkIntegrity
165: } // end ArrayBag2