View Javadoc
1   /*
2    * Copyright 2013 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    *   http://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  
17  package io.netty.channel.nio;
18  
19  import java.nio.channels.SelectionKey;
20  import java.util.AbstractSet;
21  import java.util.Iterator;
22  
23  final class SelectedSelectionKeySet extends AbstractSet<SelectionKey> {
24  
25      private SelectionKey[] keysA;
26      private int keysASize;
27      private SelectionKey[] keysB;
28      private int keysBSize;
29      private boolean isA = true;
30  
31      SelectedSelectionKeySet() {
32          keysA = new SelectionKey[1024];
33          keysB = keysA.clone();
34      }
35  
36      @Override
37      public boolean add(SelectionKey o) {
38          if (o == null) {
39              return false;
40          }
41  
42          if (isA) {
43              int size = keysASize;
44              keysA[size ++] = o;
45              keysASize = size;
46              if (size == keysA.length) {
47                  doubleCapacityA();
48              }
49          } else {
50              int size = keysBSize;
51              keysB[size ++] = o;
52              keysBSize = size;
53              if (size == keysB.length) {
54                  doubleCapacityB();
55              }
56          }
57  
58          return true;
59      }
60  
61      private void doubleCapacityA() {
62          SelectionKey[] newKeysA = new SelectionKey[keysA.length << 1];
63          System.arraycopy(keysA, 0, newKeysA, 0, keysASize);
64          keysA = newKeysA;
65      }
66  
67      private void doubleCapacityB() {
68          SelectionKey[] newKeysB = new SelectionKey[keysB.length << 1];
69          System.arraycopy(keysB, 0, newKeysB, 0, keysBSize);
70          keysB = newKeysB;
71      }
72  
73      SelectionKey[] flip() {
74          if (isA) {
75              isA = false;
76              keysA[keysASize] = null;
77              keysBSize = 0;
78              return keysA;
79          } else {
80              isA = true;
81              keysB[keysBSize] = null;
82              keysASize = 0;
83              return keysB;
84          }
85      }
86  
87      @Override
88      public int size() {
89          if (isA) {
90              return keysASize;
91          } else {
92              return keysBSize;
93          }
94      }
95  
96      @Override
97      public boolean remove(Object o) {
98          return false;
99      }
100 
101     @Override
102     public boolean contains(Object o) {
103         return false;
104     }
105 
106     @Override
107     public Iterator<SelectionKey> iterator() {
108         throw new UnsupportedOperationException();
109     }
110 }