java.lang.Object
cs1302.oracle.OracleQueue<Type>
- Type Parameters:
Type
- the item type
- All Implemented Interfaces:
Queue<Type,
OracleQueue<Type>>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all items from this queue.dequeue()
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 firstnum
items dequeued from this queue.void
dequeueMany
(int num, Consumer<Type> action) Removes the most urgentnum
items in this queue and performs the given action (usingaction.accept
) on each item.boolean
Inserts the specified item into the back of this queue, making it the last item.<SubType extends Type>
booleanenqueueAll
(Iterable<SubType> items) Enqueues the items contained in the specifiedIterable
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.peek()
Examines the first item in this queue.int
size()
Return the number of items in this queue.Type[]
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).toString()
Returns a string representation of this queue.
-
Constructor Details
-
OracleQueue
public OracleQueue()Construct an emptyOracleQueue<T>
.
-
-
Method Details
-
size
public int size()Description copied from interface:Queue
Return the number of items in this queue.- Specified by:
size
in interfaceQueue<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 interfaceQueue<Type,
OracleQueue<Type>>
-
enqueue
Inserts the specified item into the back of this queue, making it the last item.- Specified by:
enqueue
in interfaceQueue<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
Description copied from interface:Queue
Enqueues the items contained in the specifiedIterable
into this queue. This method should returnfalse
if the specifiedIterable
is empty. Otherwise, this method either returnstrue
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 interfaceQueue<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
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 interfaceQueue<Type,
OracleQueue<Type>> - Returns:
- the first item in this queue
-
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 interfaceQueue<Type,
OracleQueue<Type>> - Returns:
- the first item in this queue
-
dequeue
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 interfaceQueue<Type,
OracleQueue<Type>> - Parameters:
action
- the action to be performed on the removed item.- See Also:
-
dequeueMany
Description copied from interface:Queue
Removes the most urgentnum
items in this queue and performs the given action (usingaction.accept
) on each item.- Specified by:
dequeueMany
in interfaceQueue<Type,
OracleQueue<Type>> - Parameters:
num
- the number of items to remove.action
- the action to be performed on the removed items.- See Also:
-
dequeueMany
Description copied from interface:Queue
Builds and returns a new queue that contains the firstnum
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 interfaceQueue<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
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 itstoString()
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]
-
toArray
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). 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.- Specified by:
toArray
in interfaceQueue<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
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 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 newUrgencyQueue
containing all itemse
in this queue such thatcond.test(e)
returnstrue
.- Specified by:
filter
in interfaceQueue<Type,
OracleQueue<Type>> - Parameters:
cond
- the predicate used to test items of this queue.- Returns:
- a reference to the filtered queue.
- See Also:
-