Class ByteQueue

java.lang.Object
com.semedy.reasoner.utils.ByteQueue
All Implemented Interfaces:
Cloneable

public class ByteQueue extends Object implements Cloneable
A ByteQueue is a queue of byte values. Limitations: (1) The capacity of one of these queues can change after it's created, but the maximum capacity is limited by the amount of free memory on the machine. The constructor, add, clone, and union will result in an OutOfMemoryError when free memory is exhausted. (2) A queue's capacity cannot exceed the maximum integer 2,147,483,647 (Integer.MAX_VALUE). Any attempt to create a larger capacity results in a failure due to an arithmetic overflow. Java Source Code for this class: http://www.cs.colorado.edu/~main/edu/colorado/collections/ByteQueue.java
Version:
Feb 10, 2016
Author:
Michael Main (main@colorado.edu)
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initialize an empty queue with an initial capacity of 10.
    ByteQueue(int initialCapacity)
    Initialize an empty queue with a specified initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    Generate a copy of this queue.
    void
    ensureCapacity(int minimumCapacity)
    Change the current capacity of this queue.
    int
    Accessor method to get the current capacity of this queue.
    byte
    Get the front item, without removing it from this queue.
    byte
    Get the front item, removing it from this queue.
    void
    insert(byte item)
    Insert a new item in this queue.
    boolean
    Determine whether this queue is empty.
    int
    Accessor method to determine the number of items in this queue.
    void
    Reduce the current capacity of this queue to its actual size (i.e., the number of items it contains).

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ByteQueue

      public ByteQueue()
      Initialize an empty queue with an initial capacity of 10. Note that the insert method works efficiently (without needing more memory) until this capacity is reached. Postcondition: This queue is empty and has an initial capacity of 10.
      Throws:
      OutOfMemoryError - Indicates insufficient memory for: new byte[10].
    • ByteQueue

      public ByteQueue(int initialCapacity)
      Initialize an empty queue with a specified initial capacity. Note that the insert method works efficiently (without needing more memory) until this capacity is reached.
      Parameters:
      initialCapacity - the initial capacity of this queue Precondition: initialCapacity is non-negative. Postcondition: This queue is empty and has the given initial capacity.
      Throws:
      IllegalArgumentException - Indicates that initialCapacity is negative.
      OutOfMemoryError - Indicates insufficient memory for: new byte[initialCapacity].
  • Method Details

    • clone

      public Object clone()
      Generate a copy of this queue.
      Returns:
      The return value is a copy of this queue. Subsequent changes to the copy will not affect the original, nor vice versa. Note that the return value must be type cast to a ByteQueue before it can be used.
      Throws:
      OutOfMemoryError - Indicates insufficient memory for creating the clone.
    • ensureCapacity

      public void ensureCapacity(int minimumCapacity)
      Change the current capacity of this queue.
      Parameters:
      minimumCapacity - the new capacity for this queue Postcondition: This queue's capacity has been changed to at least minimumCapacity. If the capacity was already at or greater than minimumCapacity, then the capacity is left unchanged.
      Throws:
      OutOfMemoryError - Indicates insufficient memory for: new byte[minimumCapacity].
    • getCapacity

      public int getCapacity()
      Accessor method to get the current capacity of this queue. The insert method works efficiently (without needing more memory) until this capacity is reached.
      Returns:
      the current capacity of this queue
    • getFrontNRemove

      public byte getFrontNRemove()
      Get the front item, removing it from this queue. Precondition: This queue is not empty.
      Returns:
      The return value is the front item of this queue, and the item has been removed.
      Throws:
      NoSuchElementException - Indicates that this queue is empty.
    • getFront

      public byte getFront()
      Get the front item, without removing it from this queue. Precondition: This queue is not empty.
      Returns:
      The return value is the front item of this queue, and the item has been removed.
      Throws:
      NoSuchElementException - Indicates that this queue is empty.
    • insert

      public void insert(byte item)
      Insert a new item in this queue. If the addition would take this queue beyond its current capacity, then the capacity is increased before adding the new item. The new item may be the null reference.
      Parameters:
      item - the item to be pushed onto this queue Postcondition: The item has been pushed onto this queue.
      Throws:
      OutOfMemoryError - Indicates insufficient memory for increasing the queue's capacity. Note: An attempt to increase the capacity beyond Integer.MAX_VALUE will cause the queue to fail with an arithmetic overflow.
    • isEmpty

      public boolean isEmpty()
      Determine whether this queue is empty.
      Returns:
      true if this queue is empty; false otherwise.
    • size

      public int size()
      Accessor method to determine the number of items in this queue.
      Returns:
      the number of items in this queue
    • trimToSize

      public void trimToSize()
      Reduce the current capacity of this queue to its actual size (i.e., the number of items it contains). Postcondition: This queue's capacity has been changed to its current size.
      Throws:
      OutOfMemoryError - Indicates insufficient memory for altering the capacity.