Package cs1302.oracle

Class OracleQueue<Type>

java.lang.Object
cs1302.oracle.OracleQueue<Type>
Type Parameters:
Type - the item type
All Implemented Interfaces:
Queue<Type,OracleQueue<Type>>

public class OracleQueue<Type> extends Object implements Queue<Type,OracleQueue<Type>>
An "oracle" implementation of Queue with FIFO semantics that uses a linked list of Node objects to maintain its "line" of items.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct an empty OracleQueue<T>.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all items from this queue.
    Removes and examines the first item in this queue.
    void
    Removes the first item in this queue, then performs the given action on that item.
    dequeueMany(int num)
    Builds and returns a new queue that contains the first num items dequeued from this queue.
    void
    dequeueMany(int num, Consumer<Type> action)
    Removes the most urgent num items in this queue and performs the given action (using action.accept) on each item.
    boolean
    enqueue(Type item)
    Inserts the specified item into the back of this queue, making it the last item.
    <SubType extends Type>
    boolean
    enqueueAll(Iterable<SubType> items)
    Enqueues the items contained in the specified Iterable into this queue.
    Builds and returns a new queue consisting of the items of this queue that pass the test specified by the given predicate.
    Examines the first item in this queue.
    int
    Return the number of items in this queue.
    toArray(IntFunction<Type[]> generator)
    Returns an array containing all of the objects in this queue in proper sequence (from first to last element, by urgency).
    Returns a string representation of this queue.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • OracleQueue

      public OracleQueue()
      Construct an empty OracleQueue<T>.
  • Method Details

    • size

      public int size()
      Description copied from interface: Queue
      Return the number of items in this queue.
      Specified by:
      size in interface Queue<Type,OracleQueue<Type>>
      Returns:
      the number of items in this queue
    • clear

      public void clear()
      Description copied from interface: Queue
      Removes all items from this queue.
      Specified by:
      clear in interface Queue<Type,OracleQueue<Type>>
    • enqueue

      public boolean enqueue(Type item)
      Inserts the specified item into the back of this queue, making it the last item.
      Specified by:
      enqueue in interface Queue<Type,OracleQueue<Type>>
      Parameters:
      item - the item to insert
      Returns:
      true if this queue was modified as a result of this call; false otherwise
    • enqueueAll

      public <SubType extends Type> boolean enqueueAll(Iterable<SubType> items)
      Description copied from interface: Queue
      Enqueues the items contained in the specified Iterable into this queue. This method should return false if the specified Iterable is empty. Otherwise, this method either returns true or throws an exception upon failure.

      Here is an example, assuming queue refers to an empty queue with FIFO semantics:

      import java.util.List;
      import java.util.Iterable;
      // create an iterable
      Iterable<String> items = List.<String>of("a", "b", "c");
      
      // confirm "before" state
      System.out.println(queue);        // []
      System.out.println(queue.size()); // 0
      
      // enqueue the items
      var newq = queue.enqueueAll(items);
      
      // confirm "after" state
      System.out.println(queue);        // [a, b, c]
      System.out.println(queue.size()); // 3
      Specified by:
      enqueueAll in interface Queue<Type,OracleQueue<Type>>
      Type Parameters:
      SubType - the type of the items to be added.
      Parameters:
      items - the items to add to this queue.
      Returns:
      true if this queue is changed as a result of the call; false otherwise.
    • peek

      public Type peek()
      Description copied from interface: Queue
      Examines the first item in this queue. This method returns, but does not remove, the first item. If FIFO semantics apply, then users can safely assume that this item was inserted into the queue before any other items in this queue.
      Specified by:
      peek in interface Queue<Type,OracleQueue<Type>>
      Returns:
      the first item in this queue
    • dequeue

      public Type dequeue()
      Description copied from interface: Queue
      Removes and examines the first item in this queue. This method returns the item that it removes. If FIFO semantics apply, then users can safely assume that this item was inserted into the queue before any remaining items in this queue.
      Specified by:
      dequeue in interface Queue<Type,OracleQueue<Type>>
      Returns:
      the first item in this queue
    • dequeue

      public void dequeue(Consumer<Type> action)
      Description copied from interface: Queue
      Removes the first item in this queue, then performs the given action on that item.

      Here is some example code that illustrates how this method behaves, assuming FIFO semantics — you should assume that queue already exists:

      // confirm state of queue
      System.out.println(queue);        // [a, b, c, d]
      System.out.println(queue.size()); // 4
      Consumer<String> printer = (String item) -> {
          System.out.println(item);
      };
      
      queue.dequeue(printer); // prints: a
      queue.dequeue(printer); // prints: b
      List<String> list = new ArrayList<String>();
      Consumer<String> append = (String item) -> {
          list.add(list);
      };
      
      action.accept("world");  // appends: world
      queue.dequeue(append);   // appends: c
      queue.dequeue(append);   // appends: d
      
      System.out.println(list); // [c, d]
      Specified by:
      dequeue in interface Queue<Type,OracleQueue<Type>>
      Parameters:
      action - the action to be performed on the removed item.
      See Also:
    • dequeueMany

      public void dequeueMany(int num, Consumer<Type> action)
      Description copied from interface: Queue
      Removes the most urgent num items in this queue and performs the given action (using action.accept) on each item.
      Specified by:
      dequeueMany in interface Queue<Type,OracleQueue<Type>>
      Parameters:
      num - the number of items to remove.
      action - the action to be performed on the removed items.
      See Also:
    • dequeueMany

      public OracleQueue<Type> dequeueMany(int num)
      Description copied from interface: Queue
      Builds and returns a new queue that contains the first num items dequeued from this queue.

      Here is some example code that illustrates how this method behaves, assuming FIFO semantics — you should assume that oldq already exists:

      // confirm state of old queue
      System.out.println(oldq);        // [a, b, c, d]
      System.out.println(oldq.size()); // 4
      
      // dequeue two items from oldq
      var newq = oldq.dequeueMany(2);
      
      // confirm state of old queue
      System.out.println(oldq);        // [c, d]
      System.out.println(oldq.size()); // 2
      
      // confirm state of new queue
      System.out.println(newq);        // [a, b]
      System.out.println(newq.size()); // 2
      Specified by:
      dequeueMany in interface Queue<Type,OracleQueue<Type>>
      Parameters:
      num - the number of items to remove and include in the new queue
      Returns:
      a new queue object containing the first num items dequeued from this queue
    • toString

      public String toString()
      Description copied from interface: Queue
      Returns a string representation of this queue. The string representation consists of each item in the queue, from first to last, separated by the characters ", " (comma and space) with opening and closing square brackets ("[", "]") at the beginning and end, respectively. Each item of the queue is represented by the string value returned by its toString() method. If FIFO semantics apply, then the items in the resulting string will appear in the same order they were enqueued.

      Here is an example, assuming queue refers to an empty queue with FIFO semantics:

      queue.enqueue("a");
      queue.enqueue("b");
      queue.enqueue("c");
      
      System.out.println(queue); // [a, b, c]
      Specified by:
      toString in interface Queue<Type,OracleQueue<Type>>
      Overrides:
      toString in class Object
      Returns:
      the string representation of this queue
    • toArray

      public Type[] toArray(IntFunction<Type[]> generator)
      Description copied from interface: Queue
      Returns an array containing all of the objects in this queue in proper sequence (from first to last element, by urgency). The generator parameter accepts a reference to any method that takes an integer, which is the size of the desired array, and produces an array of the desired size.
      Specified by:
      toArray in interface Queue<Type,OracleQueue<Type>>
      Parameters:
      generator - a function which produces a new array of the desired type and the provided size.
      Returns:
      an array containing all of the items in this queue in proper sequence.
    • filter

      public OracleQueue<Type> filter(Predicate<Type> cond)
      Description copied from interface: Queue
      Builds and returns a new queue consisting of the items of this queue that pass the test specified by the given predicate. The term predicate usually refers to some function that returns either true or false. Here, it refers to the parameter cond, which should refer to an object of a class that implements the Predicate<Type> interface. More formally, this method returns a new UrgencyQueue containing all items e in this queue such that cond.test(e) returns true.
      Specified by:
      filter in interface Queue<Type,OracleQueue<Type>>
      Parameters:
      cond - the predicate used to test items of this queue.
      Returns:
      a reference to the filtered queue.
      See Also: