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  package io.netty.channel.nio;
17  
18  import java.nio.channels.SelectionKey;
19  import java.util.AbstractSet;
20  import java.util.Arrays;
21  import java.util.Iterator;
22  
23  final class SelectedSelectionKeySet extends AbstractSet<SelectionKey> {
24  
25      SelectionKey[] keys;
26      int size;
27  
28      SelectedSelectionKeySet() {
29          keys = new SelectionKey[1024];
30      }
31  
32      @Override
33      public boolean add(SelectionKey o) {
34          if (o == null) {
35              return false;
36          }
37  
38          keys[size++] = o;
39          if (size == keys.length) {
40              increaseCapacity();
41          }
42  
43          return true;
44      }
45  
46      @Override
47      public int size() {
48          return size;
49      }
50  
51      @Override
52      public boolean remove(Object o) {
53          return false;
54      }
55  
56      @Override
57      public boolean contains(Object o) {
58          return false;
59      }
60  
61      @Override
62      public Iterator<SelectionKey> iterator() {
63          throw new UnsupportedOperationException();
64      }
65  
66      void reset() {
67          reset(0);
68      }
69  
70      void reset(int start) {
71          Arrays.fill(keys, start, size, null);
72          size = 0;
73      }
74  
75      private void increaseCapacity() {
76          SelectionKey[] newKeys = new SelectionKey[keys.length << 1];
77          System.arraycopy(keys, 0, newKeys, 0, size);
78          keys = newKeys;
79      }
80  }