Merge branch 'master' of ../controller
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.util.AbstractQueue;
14 import java.util.Collections;
15 import java.util.Deque;
16 import java.util.Iterator;
17 import java.util.Spliterator;
18 import java.util.Spliterators;
19 import org.opendaylight.yangtools.concepts.Immutable;
20
21 /**
22  * A specialized always-empty implementation of {@link java.util.Deque}. This implementation will always refuse new
23  * elements in its {@link #offer(Object)} method.
24
25  * @author Robert Varga
26  *
27  * @param <E> the type of elements held in this collection
28  */
29 @Beta
30 public final class EmptyDeque<E> extends AbstractQueue<E> implements Deque<E>, Immutable {
31     private static final EmptyDeque<?> INSTANCE = new EmptyDeque<>();
32     private static final Object[] EMPTY_ARRAY = new Object[0];
33
34     private EmptyDeque() {
35         // No instances
36     }
37
38     @SuppressWarnings("unchecked")
39     public static <T> EmptyDeque<T> instance() {
40         return (EmptyDeque<T>) INSTANCE;
41     }
42
43     @Override
44     @SuppressWarnings("checkstyle:parameterName")
45     public boolean offer(final E e) {
46         return false;
47     }
48
49     @Override
50     @SuppressWarnings("checkstyle:parameterName")
51     public boolean offerFirst(final E e) {
52         return false;
53     }
54
55     @Override
56     @SuppressWarnings("checkstyle:parameterName")
57     public boolean offerLast(final E e) {
58         return false;
59     }
60
61     @Override
62     public E poll() {
63         return null;
64     }
65
66     @Override
67     public E pollFirst() {
68         return null;
69     }
70
71     @Override
72     public E pollLast() {
73         return null;
74     }
75
76     @Override
77     public E peek() {
78         return null;
79     }
80
81     @Override
82     public E peekFirst() {
83         return null;
84     }
85
86     @Override
87     public E peekLast() {
88         return null;
89     }
90
91     @Override
92     public Iterator<E> iterator() {
93         return Collections.emptyIterator();
94     }
95
96     @Override
97     public Spliterator<E> spliterator() {
98         return Spliterators.emptySpliterator();
99     }
100
101     @Override
102     public int size() {
103         return 0;
104     }
105
106     @Override
107     public Object[] toArray() {
108         return EMPTY_ARRAY;
109     }
110
111     @Override
112     @SuppressWarnings("checkstyle:parameterName")
113     public <T> T[] toArray(final T[] a) {
114         return requireNonNull(a);
115     }
116
117     @Override
118     @SuppressWarnings("checkstyle:parameterName")
119     public void addFirst(final E e) {
120         add(e);
121     }
122
123     @Override
124     @SuppressWarnings("checkstyle:parameterName")
125     public void addLast(final E e) {
126         add(e);
127     }
128
129     @Override
130     public E removeFirst() {
131         return remove();
132     }
133
134     @Override
135     public E removeLast() {
136         return remove();
137     }
138
139     @Override
140     public E getFirst() {
141         return element();
142     }
143
144     @Override
145     public E getLast() {
146         return element();
147     }
148
149     @Override
150     @SuppressWarnings("checkstyle:parameterName")
151     public boolean removeFirstOccurrence(final Object o) {
152         return false;
153     }
154
155     @Override
156     @SuppressWarnings("checkstyle:parameterName")
157     public boolean removeLastOccurrence(final Object o) {
158         return false;
159     }
160
161     @Override
162     @SuppressWarnings("checkstyle:parameterName")
163     public void push(final E e) {
164         addFirst(e);
165     }
166
167     @Override
168     public E pop() {
169         return removeFirst();
170     }
171
172     @Override
173     public Iterator<E> descendingIterator() {
174         return Collections.emptyIterator();
175     }
176 }