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    *   https://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  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  }