View Javadoc

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