1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec;
17
18 import io.netty.util.Signal;
19
20 public class DecoderResult {
21
22 protected static final Signal SIGNAL_UNFINISHED = Signal.valueOf(DecoderResult.class.getName() + ".UNFINISHED");
23 protected static final Signal SIGNAL_SUCCESS = Signal.valueOf(DecoderResult.class.getName() + ".SUCCESS");
24
25 public static final DecoderResult UNFINISHED = new DecoderResult(SIGNAL_UNFINISHED);
26 public static final DecoderResult SUCCESS = new DecoderResult(SIGNAL_SUCCESS);
27
28 public static DecoderResult failure(Throwable cause) {
29 if (cause == null) {
30 throw new NullPointerException("cause");
31 }
32 return new DecoderResult(cause);
33 }
34
35 private final Throwable cause;
36
37 protected DecoderResult(Throwable cause) {
38 if (cause == null) {
39 throw new NullPointerException("cause");
40 }
41 this.cause = cause;
42 }
43
44 public boolean isFinished() {
45 return cause != SIGNAL_UNFINISHED;
46 }
47
48 public boolean isSuccess() {
49 return cause == SIGNAL_SUCCESS;
50 }
51
52 public boolean isFailure() {
53 return cause != SIGNAL_SUCCESS && cause != SIGNAL_UNFINISHED;
54 }
55
56 public Throwable cause() {
57 if (isFailure()) {
58 return cause;
59 } else {
60 return null;
61 }
62 }
63
64 @Override
65 public String toString() {
66 if (isFinished()) {
67 if (isSuccess()) {
68 return "success";
69 }
70
71 String cause = cause().toString();
72 return new StringBuilder(cause.length() + 17)
73 .append("failure(")
74 .append(cause)
75 .append(')')
76 .toString();
77 } else {
78 return "unfinished";
79 }
80 }
81 }