1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.epoll;
17
18 import io.netty.channel.IoOps;
19
20
21
22
23 public final class EpollIoOps implements IoOps {
24
25 static {
26
27
28 Epoll.ensureAvailability();
29 }
30
31
32
33
34
35 public static final EpollIoOps EPOLLOUT = new EpollIoOps(Native.EPOLLOUT);
36
37
38
39
40 public static final EpollIoOps EPOLLIN = new EpollIoOps(Native.EPOLLIN);
41
42
43
44
45 public static final EpollIoOps EPOLLERR = new EpollIoOps(Native.EPOLLERR);
46
47
48
49
50 public static final EpollIoOps EPOLLRDHUP = new EpollIoOps(Native.EPOLLRDHUP);
51
52 public static final EpollIoOps EPOLLET = new EpollIoOps(Native.EPOLLET);
53
54
55 private static final EpollIoEvent[] EVENTS;
56
57 static {
58 EpollIoOps all = new EpollIoOps(EPOLLOUT.value | EPOLLIN.value | EPOLLERR.value | EPOLLRDHUP.value);
59 EVENTS = new EpollIoEvent[all.value + 1];
60 addToArray(EVENTS, EPOLLOUT);
61 addToArray(EVENTS, EPOLLIN);
62 addToArray(EVENTS, EPOLLERR);
63 addToArray(EVENTS, EPOLLRDHUP);
64 addToArray(EVENTS, all);
65 }
66
67 private static void addToArray(EpollIoEvent[] array, EpollIoOps ops) {
68 array[ops.value] = new DefaultEpollIoEvent(ops);
69 }
70
71 final int value;
72
73 private EpollIoOps(int value) {
74 this.value = value;
75 }
76
77
78
79
80
81
82 public boolean contains(EpollIoOps ops) {
83 return (value & ops.value) != 0;
84 }
85
86
87
88
89
90
91
92 public EpollIoOps with(EpollIoOps ops) {
93 if (contains(ops)) {
94 return this;
95 }
96 return valueOf(value | ops.value());
97 }
98
99
100
101
102
103
104
105 public EpollIoOps without(EpollIoOps ops) {
106 if (!contains(ops)) {
107 return this;
108 }
109 return valueOf(value & ~ops.value());
110 }
111
112
113
114
115
116
117 public int value() {
118 return value;
119 }
120
121 @Override
122 public boolean equals(Object o) {
123 if (this == o) {
124 return true;
125 }
126 if (o == null || getClass() != o.getClass()) {
127 return false;
128 }
129 EpollIoOps nioOps = (EpollIoOps) o;
130 return value == nioOps.value;
131 }
132
133 @Override
134 public int hashCode() {
135 return value;
136 }
137
138
139
140
141
142
143
144 public static EpollIoOps valueOf(int value) {
145 return eventOf(value).ops();
146 }
147
148 @Override
149 public String toString() {
150 return "EpollIoOps{" +
151 "value=" + value +
152 '}';
153 }
154
155 static EpollIoEvent eventOf(int value) {
156 if (value > 0 && value < EVENTS.length) {
157 EpollIoEvent event = EVENTS[value];
158 if (event != null) {
159 return event;
160 }
161 }
162 return new DefaultEpollIoEvent(new EpollIoOps(value));
163 }
164
165 private static final class DefaultEpollIoEvent implements EpollIoEvent {
166 private final EpollIoOps ops;
167
168 DefaultEpollIoEvent(EpollIoOps ops) {
169 this.ops = ops;
170 }
171
172 @Override
173 public EpollIoOps ops() {
174 return ops;
175 }
176
177 @Override
178 public boolean equals(Object o) {
179 if (this == o) {
180 return true;
181 }
182 if (o == null || getClass() != o.getClass()) {
183 return false;
184 }
185 EpollIoEvent event = (EpollIoEvent) o;
186 return event.ops().equals(ops());
187 }
188
189 @Override
190 public int hashCode() {
191 return ops().hashCode();
192 }
193
194 @Override
195 public String toString() {
196 return "DefaultEpollIoEvent{" +
197 "ops=" + ops +
198 '}';
199 }
200 }
201 }