View Javadoc

1   /*
2    * Copyright 2015 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 static io.netty.util.internal.ObjectUtil.checkNotNull;
19  
20  /**
21   * Result of detecting a protocol.
22   *
23   * @param <T> the type of the protocol
24   */
25  public final class ProtocolDetectionResult<T> {
26  
27      @SuppressWarnings({ "rawtypes", "unchecked" })
28      private static final ProtocolDetectionResult NEEDS_MORE_DATE =
29              new ProtocolDetectionResult(ProtocolDetectionState.NEEDS_MORE_DATA, null);
30      @SuppressWarnings({ "rawtypes", "unchecked" })
31      private static final ProtocolDetectionResult INVALID =
32              new ProtocolDetectionResult(ProtocolDetectionState.INVALID, null);
33  
34      private final ProtocolDetectionState state;
35      private final T result;
36  
37      /**
38       * Returns a {@link ProtocolDetectionResult} that signals that more data is needed to detect the protocol.
39       */
40      @SuppressWarnings("unchecked")
41      public static <T> ProtocolDetectionResult<T> needsMoreData() {
42          return NEEDS_MORE_DATE;
43      }
44  
45      /**
46       * Returns a {@link ProtocolDetectionResult} that signals the data was invalid for the protocol.
47       */
48      @SuppressWarnings("unchecked")
49      public static <T> ProtocolDetectionResult<T> invalid() {
50          return INVALID;
51      }
52  
53      /**
54       * Returns a {@link ProtocolDetectionResult} which holds the detected protocol.
55       */
56      @SuppressWarnings("unchecked")
57      public static <T> ProtocolDetectionResult<T> detected(T protocol) {
58          return new ProtocolDetectionResult<T>(ProtocolDetectionState.DETECTED, checkNotNull(protocol, "protocol"));
59      }
60  
61      private ProtocolDetectionResult(ProtocolDetectionState state, T result) {
62          this.state = state;
63          this.result = result;
64      }
65  
66      /**
67       * Return the {@link ProtocolDetectionState}. If the state is {@link ProtocolDetectionState#DETECTED} you
68       * can retrieve the protocol via {@link #detectedProtocol()}.
69       */
70      public ProtocolDetectionState state() {
71          return state;
72      }
73  
74      /**
75       * Returns the protocol if {@link #state()} returns {@link ProtocolDetectionState#DETECTED}, otherwise {@code null}.
76       */
77      public T detectedProtocol() {
78          return result;
79      }
80  }