1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
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 }