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    *   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.util.concurrent;
17  
18  import java.util.Arrays;
19  
20  final class DefaultFutureListeners {
21  
22      private GenericFutureListener<? extends Future<?>>[] listeners;
23      private int size;
24      private int progressiveSize; // the number of progressive listeners
25  
26      @SuppressWarnings("unchecked")
27      DefaultFutureListeners(
28              GenericFutureListener<? extends Future<?>> first, GenericFutureListener<? extends Future<?>> second) {
29          listeners = new GenericFutureListener[2];
30          listeners[0] = first;
31          listeners[1] = second;
32          size = 2;
33          if (first instanceof GenericProgressiveFutureListener) {
34              progressiveSize ++;
35          }
36          if (second instanceof GenericProgressiveFutureListener) {
37              progressiveSize ++;
38          }
39      }
40  
41      public void add(GenericFutureListener<? extends Future<?>> l) {
42          GenericFutureListener<? extends Future<?>>[] listeners = this.listeners;
43          final int size = this.size;
44          if (size == listeners.length) {
45              this.listeners = listeners = Arrays.copyOf(listeners, size << 1);
46          }
47          listeners[size] = l;
48          this.size = size + 1;
49  
50          if (l instanceof GenericProgressiveFutureListener) {
51              progressiveSize ++;
52          }
53      }
54  
55      public void remove(GenericFutureListener<? extends Future<?>> l) {
56          final GenericFutureListener<? extends Future<?>>[] listeners = this.listeners;
57          int size = this.size;
58          for (int i = 0; i < size; i ++) {
59              if (listeners[i] == l) {
60                  int listenersToMove = size - i - 1;
61                  if (listenersToMove > 0) {
62                      System.arraycopy(listeners, i + 1, listeners, i, listenersToMove);
63                  }
64                  listeners[-- size] = null;
65                  this.size = size;
66  
67                  if (l instanceof GenericProgressiveFutureListener) {
68                      progressiveSize --;
69                  }
70                  return;
71              }
72          }
73      }
74  
75      public GenericFutureListener<? extends Future<?>>[] listeners() {
76          return listeners;
77      }
78  
79      public int size() {
80          return size;
81      }
82  
83      public int progressiveSize() {
84          return progressiveSize;
85      }
86  }