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 booleanisDeque(java.lang.Iterable<?> values)Checks whether the specifiedIterableis an instance ofDeque.static booleanisQueue(java.lang.Class<?> type)Checks whether the specifiedtypeis an instance ofQueue.static booleanisQueue(java.lang.Object values)Checks whether the specifiedIterableis an instance ofQueue.static <E> java.util.ArrayDeque<E>newArrayDeque()Creates a new emptyArrayDequeinstance.static <E> java.util.ArrayDeque<E>newArrayDeque(int initialCapacity)Creates a newArrayDequeinstance with the specified initial capacity.static <E> java.util.ArrayDeque<E>newArrayDeque(E... elements)Creates a newArrayDequeinstance containing the specified elements.static <E> java.util.Queue<E>ofQueue(E... elements)Creates an immutable queue containing the specified elements.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(@Nullable java.lang.Object values)
Checks whether the specifiedIterableis 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- theIterableto check- Returns:
trueif the givenIterableis aQueue,falseotherwise
-
isQueue
public static boolean isQueue(@Nullable java.lang.Class<?> type)
Checks whether the specifiedtypeis an instance ofQueue.Example Usage
boolean result = isQueue(LinkedList.class); // returns true result = isQueue(ArrayList.class); // returns false- Parameters:
type- thetypeto check- Returns:
trueif the giventypeis aQueue,falseotherwise
-
isDeque
public static boolean isDeque(java.lang.Iterable<?> values)
Checks whether the specifiedIterableis 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- theIterableto check- Returns:
trueif the givenIterableis aDeque,falseotherwise
-
emptyQueue
@Nonnull @Immutable 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
@Nonnull @Immutable 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
@Nonnull @Immutable 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
@Nonnull @Immutable 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
@Nonnull @Immutable 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 anUnsupportedOperationExceptiondepending 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
-
ofQueue
@Nonnull @Immutable public static <E> java.util.Queue<E> ofQueue(E... elements)
Creates an immutable queue containing the specified elements.This method first creates a new
ArrayDequewith the given elements and then wraps it in an unmodifiable view usingunmodifiableQueue(Queue). The resulting queue is immutable, so any attempt to modify it will result in anUnsupportedOperationException.Example Usage
Queue<String> queue = ofQueue("Hello", "World"); System.out.println(queue); // prints [Hello, World] Queue<Integer> emptyQueue = ofQueue(); System.out.println(emptyQueue); // prints [] queue.add("Java"); // throws UnsupportedOperationException- Type Parameters:
E- the type of elements held in the queue- Parameters:
elements- the elements to be added to the queue, can be null or empty- Returns:
- an immutable queue containing the specified elements
-
newArrayDeque
@Nonnull public static <E> java.util.ArrayDeque<E> newArrayDeque()
Creates a new emptyArrayDequeinstance.Example Usage
ArrayDeque<String> deque = newArrayDeque(); deque.add("Hello"); deque.add("World"); System.out.println(deque); // prints [Hello, World]- Type Parameters:
E- the type of elements held in the deque- Returns:
- a new empty
ArrayDequeinstance
-
newArrayDeque
@Nonnull public static <E> java.util.ArrayDeque<E> newArrayDeque(int initialCapacity)
Creates a newArrayDequeinstance with the specified initial capacity.Example Usage
ArrayDeque<String> deque = newArrayDeque(16); deque.add("Hello"); deque.add("World"); System.out.println(deque); // prints [Hello, World]- Type Parameters:
E- the type of elements held in the deque- Parameters:
initialCapacity- the initial capacity of the deque- Returns:
- a new
ArrayDequeinstance with the specified initial capacity - Throws:
java.lang.IllegalArgumentException- if the specified initial capacity is negative
-
newArrayDeque
@Nonnull public static <E> java.util.ArrayDeque<E> newArrayDeque(E... elements)
Creates a newArrayDequeinstance containing the specified elements.Example Usage
ArrayDeque<String> deque = newArrayDeque("Hello", "World"); System.out.println(deque); // prints [Hello, World] ArrayDeque<Integer> emptyDeque = newArrayDeque(); System.out.println(emptyDeque); // prints []- Type Parameters:
E- the type of elements held in the deque- Parameters:
elements- the elements to be added to the deque, can be null or empty- Returns:
- a new
ArrayDequeinstance containing the specified elements
-
-