Package cs1302.oracle

Class OracleCustomLinkedUrgencyQueue<Type>

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

public class OracleCustomLinkedUrgencyQueue<Type> extends Object
An "oracle" implementation of UrgencyQueue that uses a linked list of Node objects to maintain its "line" of items and requires a Comparator Constructor Parameter so that it can compare items. Although Type does not have any explicit upper-bound requirements, this class is still able to determine the relative level of urgency between two items using the ordering imposed by the comparator supplied to the constructor (i.e., it uses comparator's compare(Type, Type) method).
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

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

    Methods inherited from class java.lang.Object

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

    • OracleCustomLinkedUrgencyQueue

      public OracleCustomLinkedUrgencyQueue(Comparator<Type> comparator)
  • Method Details

    • enqueue

      public boolean enqueue(Type value)
      Description copied from interface: UrgencyQueue
      Inserts the specified item into this urgency queue. The inserted item is placed such that it appears after any existing items with the same or greater level of urgency and before any existing items with a lesser level of urgency.
      Parameters:
      value - the item to insert
      Returns:
      true if this urgency queue was modified as a result of this call
    • dequeueMany

      public UrgencyQueue<Type> dequeueMany(int num)
      Description copied from interface: UrgencyQueue
      Builds and returns a new urgency queue that contains the most urgent num items dequeued from this UrgencyQueue.
      Parameters:
      num - the number of items to remove and return.
      Returns:
      a new urgency queue object containing the most urgent num items dequeued from this queue
    • filter

      public UrgencyQueue<Type> filter(Predicate<Type> cond)
      Description copied from interface: UrgencyQueue
      Builds and returns a new urgency queue consisting of the items of this urgency 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 urgency queue containing all items e in this queue such that cond.test(e) returns true.
      Parameters:
      cond - the predicate used to test items of this queue.
      Returns:
      a reference to the filtered queue.
      See Also:
    • size

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

      public Type peek()
      Description copied from interface: UrgencyQueue
      Retrieves, but does not remove, the most urgent item in this urgency queue.
      Specified by:
      peek in interface Queue<Type,UrgencyQueue<Type>>
      Specified by:
      peek in interface UrgencyQueue<Type>
      Returns:
      the most urgent item in the queue.
    • toString

      public String toString()
      Description copied from interface: UrgencyQueue
      Returns a string representation of this urgency queue. The string representation consists of each item in the queue, from most urgent to least urgent, in the descending urgency order, separated by the characters ", " (comma and space) with opening and closing square ([, ]) at the beginning and end, respectively. Each item of the queue is represented by the string value returned by its toString() method. Here is an example, assuming queue refers to an empty UrgencyQueue<Person> object:
      queue.enqueue(new Person(22, "Sally"));
      queue.enqueue(new Person(20, "Billy"));
      queue.enqueue(new Person(23, "Kuma"));
      System.out.println(queue);
      The output would be:
      [Person[age=23, name=Kuma], Person[age=22, name=Sally], Person[age=20, name=Billy]]
      Specified by:
      toString in interface Queue<Type,UrgencyQueue<Type>>
      Specified by:
      toString in interface UrgencyQueue<Type>
      Overrides:
      toString in class Object
      Returns:
      the string representation of this urgency queue
    • dequeue

      public Type dequeue()
      Description copied from interface: UrgencyQueue
      Retrieves and removes the most urgent item in this urgency queue.
      Specified by:
      dequeue in interface Queue<Type,UrgencyQueue<Type>>
      Specified by:
      dequeue in interface UrgencyQueue<Type>
      Returns:
      the most urgent item in the queue.
    • dequeue

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

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

      public <SubType extends Type> boolean enqueueAll(Iterable<SubType> values)
      Description copied from interface: UrgencyQueue
      Enqueues the items contained in the specified Iterable into this urgency queue. This method should return false if the specified Iterable is empty. Otherwise, this method either returns true or throws an exception upon failure.
      Specified by:
      enqueueAll in interface Queue<Type,UrgencyQueue<Type>>
      Specified by:
      enqueueAll in interface UrgencyQueue<Type>
      Type Parameters:
      SubType - the type of the items to be added.
      Parameters:
      values - the items to add to this queue.
      Returns:
      true if this queue is changed as a result of the call; false otherwise.
    • toArray

      public Type[] toArray(IntFunction<Type[]> generator)
      Description copied from interface: UrgencyQueue
      Returns an array containing all of the objects in this UrgencyQueue 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.

      Here is an example, assuming queue refers to a nonempty UrgencyQueue<Person> object:

      // print an existing, nonempty urgency queue
      System.out.print("urgency queue: " + queue);
      
      // setup an object that can generate a Person array
      IntFunction<Person[]> newPersonArray = (int value) -> {
          return new Person[value];
      };
      
      // get the urgency queue's items as an array
      Person[] persons = queue.toArray(newPersonArray);
      System.out.println("array length: " + persons.length);
      System.out.println("first array element: " + persons[0]);
      The output would be:
      urgency queue: [Person[age=23, name=Kuma], Person[age=22, name=Sally]]
      array length: 2
      first array element: Person[age=23, name=Kuma]
      Specified by:
      toArray in interface Queue<Type,UrgencyQueue<Type>>
      Specified by:
      toArray in interface UrgencyQueue<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.