View Javadoc

1   /*
2    * Copyright 2012 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    *   http://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.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  }