查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2019 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.netty.handler.codec.http2;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  import java.util.Queue;
21  
22  final class MaxCapacityQueue<E> implements Queue<E> {
23      private final Queue<E> queue;
24      private final int maxCapacity;
25  
26      MaxCapacityQueue(Queue<E> queue, int maxCapacity) {
27          this.queue = queue;
28          this.maxCapacity = maxCapacity;
29      }
30  
31      @Override
32      public boolean add(E element) {
33          if (offer(element)) {
34              return true;
35          }
36          throw new IllegalStateException();
37      }
38  
39      @Override
40      public boolean offer(E element) {
41          if (maxCapacity <= queue.size()) {
42              return false;
43          }
44          return queue.offer(element);
45      }
46  
47      @Override
48      public E remove() {
49          return queue.remove();
50      }
51  
52      @Override
53      public E poll() {
54          return queue.poll();
55      }
56  
57      @Override
58      public E element() {
59          return queue.element();
60      }
61  
62      @Override
63      public E peek() {
64          return queue.peek();
65      }
66  
67      @Override
68      public int size() {
69          return queue.size();
70      }
71  
72      @Override
73      public boolean isEmpty() {
74          return queue.isEmpty();
75      }
76  
77      @Override
78      public boolean contains(Object o) {
79          return queue.contains(o);
80      }
81  
82      @Override
83      public Iterator<E> iterator() {
84          return queue.iterator();
85      }
86  
87      @Override
88      public Object[] toArray() {
89          return queue.toArray();
90      }
91  
92      @Override
93      public <T> T[] toArray(T[] a) {
94          return queue.toArray(a);
95      }
96  
97      @Override
98      public boolean remove(Object o) {
99          return queue.remove(o);
100     }
101 
102     @Override
103     public boolean containsAll(Collection<?> c) {
104         return queue.containsAll(c);
105     }
106 
107     @Override
108     public boolean addAll(Collection<? extends E> c) {
109         if (maxCapacity >= size() + c.size()) {
110             return queue.addAll(c);
111         }
112         throw new IllegalStateException();
113     }
114 
115     @Override
116     public boolean removeAll(Collection<?> c) {
117         return queue.removeAll(c);
118     }
119 
120     @Override
121     public boolean retainAll(Collection<?> c) {
122         return queue.retainAll(c);
123     }
124 
125     @Override
126     public void clear() {
127         queue.clear();
128     }
129 }