Add an empty Deque implementation
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / EmptyDeque.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.util.AbstractQueue;
13 import java.util.Collections;
14 import java.util.Deque;
15 import java.util.Iterator;
16 import java.util.Spliterator;
17 import java.util.Spliterators;
18 import org.opendaylight.yangtools.concepts.Immutable;
19
20 /**
21  * A specialized always-empty implementation of {@link java.util.Deque}. This implementation will always refuse new
22  * elements in its {@link #offer(Object)} method.
23
24  * @author Robert Varga
25  *
26  * @param <E> the type of elements held in this collection
27  */
28 @Beta
29 public final class EmptyDeque<E> extends AbstractQueue<E> implements Deque<E>, Immutable {
30     private static final EmptyDeque<?> INSTANCE = new EmptyDeque<>();
31     private static final Object[] EMPTY_ARRAY = new Object[0];
32
33     private EmptyDeque() {
34         // No instances
35     }
36
37     @SuppressWarnings("unchecked")
38     public static <T> EmptyDeque<T> instance() {
39         return (EmptyDeque<T>) INSTANCE;
40     }
41
42     @Override
43     public boolean offer(final E entry) {
44         return false;
45     }
46
47     @Override
48     public boolean offerFirst(final E e) {
49         return false;
50     }
51
52     @Override
53     public boolean offerLast(final E e) {
54         return false;
55     }
56
57     @Override
58     public E poll() {
59         return null;
60     }
61
62     @Override
63     public E pollFirst() {
64         return null;
65     }
66
67     @Override
68     public E pollLast() {
69         return null;
70     }
71
72     @Override
73     public E peek() {
74         return null;
75     }
76
77     @Override
78     public E peekFirst() {
79         return null;
80     }
81
82     @Override
83     public E peekLast() {
84         return null;
85     }
86
87     @Override
88     public Iterator<E> iterator() {
89         return Collections.emptyIterator();
90     }
91
92     @Override
93     public Spliterator<E> spliterator() {
94         return Spliterators.emptySpliterator();
95     }
96
97     @Override
98     public int size() {
99         return 0;
100     }
101
102     @Override
103     public Object[] toArray() {
104         return EMPTY_ARRAY;
105     }
106
107     @Override
108     public <T> T[] toArray(final T[] a) {
109         return Preconditions.checkNotNull(a);
110     }
111
112     @Override
113     public void addFirst(final E e) {
114         add(e);
115     }
116
117     @Override
118     public void addLast(final E e) {
119         add(e);
120     }
121
122     @Override
123     public E removeFirst() {
124         return remove();
125     }
126
127     @Override
128     public E removeLast() {
129         return remove();
130     }
131
132     @Override
133     public E getFirst() {
134         return element();
135     }
136
137     @Override
138     public E getLast() {
139         return element();
140     }
141
142     @Override
143     public boolean removeFirstOccurrence(final Object o) {
144         return false;
145     }
146
147     @Override
148     public boolean removeLastOccurrence(final Object o) {
149         return false;
150     }
151
152     @Override
153     public void push(final E e) {
154         addFirst(e);
155     }
156
157     @Override
158     public E pop() {
159         return removeFirst();
160     }
161
162     @Override
163     public Iterator<E> descendingIterator() {
164         return Collections.emptyIterator();
165     }
166 }