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