1 /*
2 * Copyright 2013 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.spdy;
17
18 import io.netty.util.internal.ObjectUtil;
19
20 /**
21 * The SPDY session status code and its description.
22 */
23 public class SpdySessionStatus implements Comparable<SpdySessionStatus> {
24
25 /**
26 * 0 OK
27 */
28 public static final SpdySessionStatus OK =
29 new SpdySessionStatus(0, "OK");
30
31 /**
32 * 1 Protocol Error
33 */
34 public static final SpdySessionStatus PROTOCOL_ERROR =
35 new SpdySessionStatus(1, "PROTOCOL_ERROR");
36
37 /**
38 * 2 Internal Error
39 */
40 public static final SpdySessionStatus INTERNAL_ERROR =
41 new SpdySessionStatus(2, "INTERNAL_ERROR");
42
43 /**
44 * Returns the {@link SpdySessionStatus} represented by the specified code.
45 * If the specified code is a defined SPDY status code, a cached instance
46 * will be returned. Otherwise, a new instance will be returned.
47 */
48 public static SpdySessionStatus valueOf(int code) {
49 switch (code) {
50 case 0:
51 return OK;
52 case 1:
53 return PROTOCOL_ERROR;
54 case 2:
55 return INTERNAL_ERROR;
56 }
57
58 return new SpdySessionStatus(code, "UNKNOWN (" + code + ')');
59 }
60
61 private final int code;
62
63 private final String statusPhrase;
64
65 /**
66 * Creates a new instance with the specified {@code code} and its
67 * {@code statusPhrase}.
68 */
69 public SpdySessionStatus(int code, String statusPhrase) {
70 this.statusPhrase = ObjectUtil.checkNotNull(statusPhrase, "statusPhrase");
71 this.code = code;
72 }
73
74 /**
75 * Returns the code of this status.
76 */
77 public int code() {
78 return code;
79 }
80
81 /**
82 * Returns the status phrase of this status.
83 */
84 public String statusPhrase() {
85 return statusPhrase;
86 }
87
88 @Override
89 public int hashCode() {
90 return code();
91 }
92
93 @Override
94 public boolean equals(Object o) {
95 if (!(o instanceof SpdySessionStatus)) {
96 return false;
97 }
98
99 return code() == ((SpdySessionStatus) o).code();
100 }
101
102 @Override
103 public String toString() {
104 return statusPhrase();
105 }
106
107 @Override
108 public int compareTo(SpdySessionStatus o) {
109 return code() - o.code();
110 }
111 }