Class QueueUtils
- java.lang.Object
-
- io.microsphere.collection.QueueUtils
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.Deque<?>
EMPTY_DEQUE
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E> java.util.Deque<E>
emptyDeque()
Returns an empty immutable deque instance.static <E> java.util.Queue<E>
emptyQueue()
Returns an empty immutable queue instance.static boolean
isDeque(java.lang.Iterable<?> values)
Checks whether the specifiedIterable
is an instance ofDeque
.static boolean
isQueue(java.lang.Iterable<?> values)
Checks whether the specifiedIterable
is an instance ofQueue
.static <E> java.util.Deque<E>
reversedDeque(java.util.Deque<E> deque)
Returns a reversed view of the specified deque.static <E> java.util.Deque<E>
singletonDeque(E element)
Returns an immutable deque containing only the specified element.static <E> java.util.Queue<E>
singletonQueue(E element)
Returns an immutable queue containing only the specified element.static <E> java.util.Deque<E>
unmodifiableDeque(java.util.Deque<E> deque)
Returns an unmodifiable view of the given deque.static <E> java.util.Queue<E>
unmodifiableQueue(java.util.Queue<E> queue)
Returns an unmodifiable view of the given queue.
-
-
-
Method Detail
-
isQueue
public static boolean isQueue(java.lang.Iterable<?> values)
Checks whether the specifiedIterable
is an instance ofQueue
.Example Usage
Queue<String> queue = new LinkedList<>(); boolean result = isQueue(queue); // returns true List<String> list = new ArrayList<>(); result = isQueue(list); // returns false
- Parameters:
values
- theIterable
to check- Returns:
true
if the givenIterable
is aQueue
,false
otherwise
-
isDeque
public static boolean isDeque(java.lang.Iterable<?> values)
Checks whether the specifiedIterable
is an instance ofDeque
.Example Usage
Deque<String> deque = new LinkedList<>(); boolean result = isDeque(deque); // returns true List<String> list = new ArrayList<>(); result = isDeque(list); // returns false
- Parameters:
values
- theIterable
to check- Returns:
true
if the givenIterable
is aDeque
,false
otherwise
-
emptyQueue
@Nonnull public static <E> java.util.Queue<E> emptyQueue()
Returns an empty immutable queue instance.Example Usage
Queue<String> empty = emptyQueue(); boolean isEmpty = empty.isEmpty(); // returns true int size = empty.size(); // returns 0
- Type Parameters:
E
- the type of elements held in the queue- Returns:
- an empty immutable queue instance
-
emptyDeque
public static <E> java.util.Deque<E> emptyDeque()
Returns an empty immutable deque instance.Example Usage
Deque<String> empty = emptyDeque(); boolean isEmpty = empty.isEmpty(); // returns true int size = empty.size(); // returns 0
- Type Parameters:
E
- the type of elements held in the deque- Returns:
- an empty immutable deque instance
-
unmodifiableQueue
@Nonnull public static <E> java.util.Queue<E> unmodifiableQueue(java.util.Queue<E> queue)
Returns an unmodifiable view of the given queue.This method wraps the provided queue in an
UnmodifiableQueue
, which prevents any modifications to the queue. Any attempt to modify the returned queue will result in anUnsupportedOperationException
.Example Usage
Queue<String> mutableQueue = new LinkedList<>(); mutableQueue.add("Hello"); Queue<String> unmodifiable = unmodifiableQueue(mutableQueue); unmodifiable.add("World"); // throws UnsupportedOperationException
- Type Parameters:
E
- the type of elements held in the queue- Parameters:
queue
- the queue to be made unmodifiable, must not be null- Returns:
- an unmodifiable view of the specified queue
- Throws:
java.lang.NullPointerException
- if the provided queue is null
-
unmodifiableDeque
public static <E> java.util.Deque<E> unmodifiableDeque(java.util.Deque<E> deque)
Returns an unmodifiable view of the given deque.This method wraps the provided deque in an
UnmodifiableDeque
, which prevents any modifications to the deque. Any attempt to modify the returned deque will result in anUnsupportedOperationException
.Example Usage
Deque<String> mutableDeque = new LinkedList<>(); mutableDeque.add("Hello"); Deque<String> unmodifiable = unmodifiableDeque(mutableDeque); unmodifiable.addFirst("World"); // throws UnsupportedOperationException
- Type Parameters:
E
- the type of elements held in the deque- Parameters:
deque
- the deque to be made unmodifiable, must not be null- Returns:
- an unmodifiable view of the specified deque
- Throws:
java.lang.NullPointerException
- if the provided deque is null
-
singletonQueue
public static <E> java.util.Queue<E> singletonQueue(E element)
Returns an immutable queue containing only the specified element.The returned queue is a singleton instance that holds exactly one element. It is immutable, so any attempt to modify the queue will result in an
UnsupportedOperationException
.Example Usage
Queue<String> singleton = singletonQueue("Hello"); boolean isEmpty = singleton.isEmpty(); // returns false int size = singleton.size(); // returns 1 String value = singleton.poll(); // returns "Hello"
- Type Parameters:
E
- the type of the queue's element- Parameters:
element
- the sole element to be stored in the returned queue- Returns:
- a singleton immutable queue containing the specified element
-
singletonDeque
public static <E> java.util.Deque<E> singletonDeque(E element)
Returns an immutable deque containing only the specified element.The returned deque is a singleton instance that holds exactly one element. It is immutable, so any attempt to modify the deque will result in an
UnsupportedOperationException
.Example Usage
Deque<String> singleton = singletonDeque("Hello"); boolean isEmpty = singleton.isEmpty(); // returns false int size = singleton.size(); // returns 1 String value = singleton.pollFirst(); // returns "Hello"
- Type Parameters:
E
- the type of the deque's element- Parameters:
element
- the sole element to be stored in the returned deque- Returns:
- a singleton immutable deque containing the specified element
-
reversedDeque
public static <E> java.util.Deque<E> reversedDeque(java.util.Deque<E> deque)
Returns a reversed view of the specified deque.This method wraps the provided deque in a
ReversedDeque
, which presents the elements in reverse order. Modifications to the original deque are reflected in the reversed view, and vice versa. However, attempts to modify the reversed view may result in anUnsupportedOperationException
depending on the underlying implementation.Example Usage
Deque<String> deque = new LinkedList<>(); deque.addLast("A"); deque.addLast("B"); deque.addLast("C"); Deque<String> reversed = reversedDeque(deque); String first = reversed.peekFirst(); // returns "C" String last = reversed.peekLast(); // returns "A" // Iterating through the reversed view for (String value : reversed) { System.out.println(value); // prints "C", "B", "A" }
- Type Parameters:
E
- the type of elements held in the deque- Parameters:
deque
- the deque to be viewed in reverse order, must not be null- Returns:
- a reversed view of the specified deque
- Throws:
java.lang.NullPointerException
- if the provided deque is null
-
-