java.lang.Object
cs1302.oracle.OracleCustomLinkedUrgencyQueue<Type>
- Type Parameters:
Type
- the item type
- All Implemented Interfaces:
Queue<Type,
,UrgencyQueue<Type>> UrgencyQueue<Type>
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 -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all items from this urgency queue.dequeue()
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 urgentnum
items dequeued from thisUrgencyQueue
.void
dequeueMany
(int num, Consumer<Type> action) Removes the most urgentnum
-many items in this urgency queue and performs the given action (usingaction.accept
) on each item, in the urgency queue.boolean
Inserts the specified item into this urgency queue.<SubType extends Type>
booleanenqueueAll
(Iterable<SubType> values) Enqueues the items contained in the specifiedIterable
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.peek()
Retrieves, but does not remove, the most urgent item in this urgency queue.int
size()
Returns the number of items in this urgency queue.Type[]
toArray
(IntFunction<Type[]> generator) Returns an array containing all of the objects in thisUrgencyQueue
in proper sequence (from first to last element, by urgency).toString()
Returns a string representation of this urgency queue.
-
Constructor Details
-
OracleCustomLinkedUrgencyQueue
-
-
Method Details
-
enqueue
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
Description copied from interface:UrgencyQueue
Builds and returns a new urgency queue that contains the most urgentnum
items dequeued from thisUrgencyQueue
.- 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
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 eithertrue
orfalse
. Here, it refers to the parametercond
, which should refer to an object of a class that implements thePredicate<Type>
interface. More formally, this method returns a new urgency queue containing all itemse
in this queue such thatcond.test(e)
returnstrue
.- 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 interfaceQueue<Type,
UrgencyQueue<Type>> - Specified by:
size
in interfaceUrgencyQueue<Type>
- Returns:
- the number of items in this urgency queue.
-
peek
Description copied from interface:UrgencyQueue
Retrieves, but does not remove, the most urgent item in this urgency queue.- Specified by:
peek
in interfaceQueue<Type,
UrgencyQueue<Type>> - Specified by:
peek
in interfaceUrgencyQueue<Type>
- Returns:
- the most urgent item in the queue.
-
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 itstoString()
method. Here is an example, assumingqueue
refers to an emptyUrgencyQueue<Person>
object:
The output would be:queue.enqueue(new Person(22, "Sally")); queue.enqueue(new Person(20, "Billy")); queue.enqueue(new Person(23, "Kuma")); System.out.println(queue);
[Person[age=23, name=Kuma], Person[age=22, name=Sally], Person[age=20, name=Billy]]
- Specified by:
toString
in interfaceQueue<Type,
UrgencyQueue<Type>> - Specified by:
toString
in interfaceUrgencyQueue<Type>
- Overrides:
toString
in classObject
- Returns:
- the string representation of this urgency queue
-
dequeue
Description copied from interface:UrgencyQueue
Retrieves and removes the most urgent item in this urgency queue.- Specified by:
dequeue
in interfaceQueue<Type,
UrgencyQueue<Type>> - Specified by:
dequeue
in interfaceUrgencyQueue<Type>
- Returns:
- the most urgent item in the queue.
-
dequeue
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 interfaceQueue<Type,
UrgencyQueue<Type>> - Specified by:
dequeue
in interfaceUrgencyQueue<Type>
- Parameters:
action
- the action to be performed on the removed item.- See Also:
-
dequeueMany
Description copied from interface:UrgencyQueue
Removes the most urgentnum
-many items in this urgency queue and performs the given action (usingaction.accept
) on each item, in the urgency queue.- Specified by:
dequeueMany
in interfaceQueue<Type,
UrgencyQueue<Type>> - Specified by:
dequeueMany
in interfaceUrgencyQueue<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 interfaceQueue<Type,
UrgencyQueue<Type>> - Specified by:
clear
in interfaceUrgencyQueue<Type>
-
enqueueAll
Description copied from interface:UrgencyQueue
Enqueues the items contained in the specifiedIterable
into this urgency queue. This method should returnfalse
if the specifiedIterable
is empty. Otherwise, this method either returnstrue
or throws an exception upon failure.- Specified by:
enqueueAll
in interfaceQueue<Type,
UrgencyQueue<Type>> - Specified by:
enqueueAll
in interfaceUrgencyQueue<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
Description copied from interface:UrgencyQueue
Returns an array containing all of the objects in thisUrgencyQueue
in proper sequence (from first to last element, by urgency). Thegenerator
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 nonemptyUrgencyQueue<Person>
object:
The output would be:// 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]);
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 interfaceQueue<Type,
UrgencyQueue<Type>> - Specified by:
toArray
in interfaceUrgencyQueue<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.
-