View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * 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 distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  
16  package io.netty.handler.codec.redis;
17  
18  import io.netty.util.internal.UnstableApi;
19  
20  /**
21   * Type of <a href="http://redis.io/topics/protocol">RESP (REdis Serialization Protocol)</a>.
22   */
23  @UnstableApi
24  public enum RedisMessageType {
25  
26      SIMPLE_STRING((byte) '+', true),
27      ERROR((byte) '-', true),
28      INTEGER((byte) ':', true),
29      BULK_STRING((byte) '$', false),
30      ARRAY_HEADER((byte) '*', false),
31      ARRAY((byte) '*', false); // for aggregated
32  
33      private final byte value;
34      private final boolean inline;
35  
36      RedisMessageType(byte value, boolean inline) {
37          this.value = value;
38          this.inline = inline;
39      }
40  
41      /**
42       * Returns prefix {@code byte} for this type.
43       */
44      public byte value() {
45          return value;
46      }
47  
48      /**
49       * Returns {@code true} if this type is inline type, or returns {@code false}. If this is {@code true},
50       * this type doesn't have length field.
51       */
52      public boolean isInline() {
53          return inline;
54      }
55  
56      /**
57       * Return {@link RedisMessageType} for this type prefix {@code byte}.
58       */
59      public static RedisMessageType valueOf(byte value) {
60          switch (value) {
61          case '+':
62              return SIMPLE_STRING;
63          case '-':
64              return ERROR;
65          case ':':
66              return INTEGER;
67          case '$':
68              return BULK_STRING;
69          case '*':
70              return ARRAY_HEADER;
71          default:
72              throw new RedisCodecException("Unknown RedisMessageType: " + value);
73          }
74      }
75  }