View Javadoc
1   /*
2    * Copyright 2017 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.util.internal;
17  
18  import java.util.Collection;
19  import java.util.Collections;
20  import java.util.Iterator;
21  import java.util.NoSuchElementException;
22  
23  public final class EmptyPriorityQueue<T> implements PriorityQueue<T> {
24      private static final PriorityQueue<Object> INSTANCE = new EmptyPriorityQueue<>();
25  
26      private EmptyPriorityQueue() {
27      }
28  
29      /**
30       * Returns an unmodifiable empty {@link PriorityQueue}.
31       */
32      @SuppressWarnings("unchecked")
33      public static <V> EmptyPriorityQueue<V> instance() {
34          return (EmptyPriorityQueue<V>) INSTANCE;
35      }
36  
37      @Override
38      public boolean removeTyped(T node) {
39          return false;
40      }
41  
42      @Override
43      public boolean containsTyped(T node) {
44          return false;
45      }
46  
47      @Override
48      public void priorityChanged(T node) {
49      }
50  
51      @Override
52      public int size() {
53          return 0;
54      }
55  
56      @Override
57      public boolean isEmpty() {
58          return true;
59      }
60  
61      @Override
62      public boolean contains(Object o) {
63          return false;
64      }
65  
66      @Override
67      public Iterator<T> iterator() {
68          return Collections.<T>emptyList().iterator();
69      }
70  
71      @Override
72      public Object[] toArray() {
73          return EmptyArrays.EMPTY_OBJECTS;
74      }
75  
76      @Override
77      public <T1> T1[] toArray(T1[] a) {
78          if (a.length > 0) {
79              a[0] = null;
80          }
81          return a;
82      }
83  
84      @Override
85      public boolean add(T t) {
86          return false;
87      }
88  
89      @Override
90      public boolean remove(Object o) {
91          return false;
92      }
93  
94      @Override
95      public boolean containsAll(Collection<?> c) {
96          return false;
97      }
98  
99      @Override
100     public boolean addAll(Collection<? extends T> c) {
101         return false;
102     }
103 
104     @Override
105     public boolean removeAll(Collection<?> c) {
106         return false;
107     }
108 
109     @Override
110     public boolean retainAll(Collection<?> c) {
111         return false;
112     }
113 
114     @Override
115     public void clear() {
116     }
117 
118     @Override
119     public void clearIgnoringIndexes() {
120     }
121 
122     @Override
123     public boolean equals(Object o) {
124         return o instanceof PriorityQueue && ((PriorityQueue) o).isEmpty();
125     }
126 
127     @Override
128     public int hashCode() {
129         return 0;
130     }
131 
132     @Override
133     public boolean offer(T t) {
134         return false;
135     }
136 
137     @Override
138     public T remove() {
139         throw new NoSuchElementException();
140     }
141 
142     @Override
143     public T poll() {
144         return null;
145     }
146 
147     @Override
148     public T element() {
149         throw new NoSuchElementException();
150     }
151 
152     @Override
153     public T peek() {
154         return null;
155     }
156 
157     @Override
158     public String toString() {
159         return EmptyPriorityQueue.class.getSimpleName();
160     }
161 }