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 import io.netty.util.internal.ObjectUtil;
20
21 public class DecoderResult {
22
23 protected static final Signal SIGNAL_UNFINISHED = Signal.valueOf(DecoderResult.class, "UNFINISHED");
24 protected static final Signal SIGNAL_SUCCESS = Signal.valueOf(DecoderResult.class, "SUCCESS");
25
26 public static final DecoderResult UNFINISHED = new DecoderResult(SIGNAL_UNFINISHED);
27 public static final DecoderResult SUCCESS = new DecoderResult(SIGNAL_SUCCESS);
28
29 public static DecoderResult failure(Throwable cause) {
30 return new DecoderResult(ObjectUtil.checkNotNull(cause, "cause"));
31 }
32
33 private final Throwable cause;
34
35 protected DecoderResult(Throwable cause) {
36 this.cause = ObjectUtil.checkNotNull(cause, "cause");
37 }
38
39 public boolean isFinished() {
40 return cause != SIGNAL_UNFINISHED;
41 }
42
43 public boolean isSuccess() {
44 return cause == SIGNAL_SUCCESS;
45 }
46
47 public boolean isFailure() {
48 return cause != SIGNAL_SUCCESS && cause != SIGNAL_UNFINISHED;
49 }
50
51 public Throwable cause() {
52 if (isFailure()) {
53 return cause;
54 } else {
55 return null;
56 }
57 }
58
59 @Override
60 public String toString() {
61 if (isFinished()) {
62 if (isSuccess()) {
63 return "success";
64 }
65
66 String cause = cause().toString();
67 return new StringBuilder(cause.length() + 17)
68 .append("failure(")
69 .append(cause)
70 .append(')')
71 .toString();
72 } else {
73 return "unfinished";
74 }
75 }
76 }