All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.microsphere.collection.QueueUtils Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.microsphere.collection;

import io.microsphere.util.BaseUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.Queue;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static io.microsphere.collection.CollectionUtils.singletonIterator;
import static io.microsphere.collection.CollectionUtils.unmodifiableIterator;
import static java.util.Collections.emptyIterator;

/**
 * The utilities class for Java {@link Queue}
 *
 * @author Mercy
 * @see Queue
 * @since 1.0.0
 */
public abstract class QueueUtils extends BaseUtils {

    private static final Deque EMPTY_DEQUE = new EmptyDeque();

    public static boolean isQueue(Iterable values) {
        return values instanceof Queue;
    }

    public static boolean isDeque(Iterable values) {
        return values instanceof Deque;
    }

    public static  Queue emptyQueue() {
        return (Queue) EMPTY_DEQUE;
    }

    public static  Deque emptyDeque() {
        return (Deque) EMPTY_DEQUE;
    }

    public static  Queue unmodifiableQueue(Queue queue) {
        return new UnmodifiableQueue(queue);
    }

    public static  Queue singletonQueue(E element) {
        return new SingletonDeque<>(element);
    }

    public static  Deque singletonDeque(E element) {
        return new SingletonDeque<>(element);
    }

    static class EmptyDeque extends AbstractDeque implements Serializable {

        private static final long serialVersionUID = -1L;

        @Override
        public Iterator iterator() {
            return emptyIterator();
        }

        @Override
        public Iterator descendingIterator() {
            return emptyIterator();
        }

        @Override
        public boolean offerFirst(E e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean offerLast(E e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public E pollFirst() {
            throw new UnsupportedOperationException();
        }

        @Override
        public E pollLast() {
            throw new UnsupportedOperationException();
        }

        @Override
        public E getFirst() {
            throw new UnsupportedOperationException();
        }

        @Override
        public E getLast() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean removeLastOccurrence(Object o) {
            return false;
        }

        @Override
        public int size() {
            return 0;
        }
    }

    static class UnmodifiableQueue implements Queue, Serializable {

        private static final long serialVersionUID = -1L;

        private final Queue delegate;

        UnmodifiableQueue(Queue queue) {
            this.delegate = queue;
        }

        @Override
        public int size() {
            return delegate.size();
        }

        @Override
        public boolean isEmpty() {
            return delegate.isEmpty();
        }

        @Override
        public boolean contains(Object o) {
            return delegate.contains(o);
        }

        @Override
        public Iterator iterator() {
            return unmodifiableIterator(delegate.iterator());
        }

        @Override
        public Object[] toArray() {
            return delegate.toArray();
        }

        @Override
        public  T[] toArray(T[] a) {
            return delegate.toArray(a);
        }

        @Override
        public boolean add(E e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean remove(Object o) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean offer(E e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public E remove() {
            throw new UnsupportedOperationException();
        }

        @Override
        public E poll() {
            throw new UnsupportedOperationException();
        }

        @Override
        public E element() {
            return delegate.element();
        }

        @Override
        public E peek() {
            return delegate.peek();
        }

        @Override
        public boolean containsAll(Collection c) {
            return delegate.containsAll(c);
        }

        @Override
        public boolean addAll(Collection c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean removeAll(Collection c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean removeIf(Predicate filter) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean retainAll(Collection c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void clear() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean equals(Object o) {
            return delegate.equals(o);
        }

        @Override
        public int hashCode() {
            return delegate.hashCode();
        }

        @Override
        public Spliterator spliterator() {
            return delegate.spliterator();
        }

        @Override
        public Stream stream() {
            return delegate.stream();
        }

        @Override
        public Stream parallelStream() {
            return delegate.parallelStream();
        }

        @Override
        public void forEach(Consumer action) {
            delegate.forEach(action);
        }
    }

    static class UnmodifiableDeque extends UnmodifiableQueue implements Deque, Serializable {

        private final Deque delegate;

        UnmodifiableDeque(Deque deque) {
            super(deque);
            this.delegate = deque;
        }

        @Override
        public void addFirst(E e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void addLast(E e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean offerFirst(E e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean offerLast(E e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public E removeFirst() {
            throw new UnsupportedOperationException();
        }

        @Override
        public E removeLast() {
            throw new UnsupportedOperationException();
        }

        @Override
        public E pollFirst() {
            throw new UnsupportedOperationException();
        }

        @Override
        public E pollLast() {
            throw new UnsupportedOperationException();
        }

        @Override
        public E getFirst() {
            return delegate.getFirst();
        }

        @Override
        public E getLast() {
            return delegate.getLast();
        }

        @Override
        public E peekFirst() {
            return getFirst();
        }

        @Override
        public E peekLast() {
            return getLast();
        }

        @Override
        public boolean removeFirstOccurrence(Object o) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean removeLastOccurrence(Object o) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void push(E e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public E pop() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Iterator descendingIterator() {
            return unmodifiableIterator(delegate.descendingIterator());
        }
    }

    static class SingletonDeque extends AbstractDeque implements Serializable {

        private static final long serialVersionUID = -1L;

        private final E element;

        private final Iterator iterator;

        SingletonDeque(E element) {
            this.element = element;
            this.iterator = singletonIterator(element);
        }

        @Override
        public Iterator iterator() {
            return iterator;
        }

        @Override
        public Iterator descendingIterator() {
            return iterator;
        }

        @Override
        public boolean offerFirst(E e) {
            return false;
        }

        @Override
        public boolean offerLast(E e) {
            return false;
        }

        @Override
        public E pollFirst() {
            throw new UnsupportedOperationException();
        }

        @Override
        public E pollLast() {
            throw new UnsupportedOperationException();
        }

        @Override
        public E getFirst() {
            return element;
        }

        @Override
        public E getLast() {
            return element;
        }

        @Override
        public boolean removeLastOccurrence(Object o) {
            throw new UnsupportedOperationException();
        }

        @Override
        public int size() {
            return 1;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy