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  
17  package io.netty.handler.codec.socksx.v5;
18  
19  /**
20   * The status of {@link Socks5PasswordAuthResponse}.
21   */
22  public class Socks5PasswordAuthStatus implements Comparable<Socks5PasswordAuthStatus> {
23  
24      public static final Socks5PasswordAuthStatus SUCCESS = new Socks5PasswordAuthStatus(0x00, "SUCCESS");
25      public static final Socks5PasswordAuthStatus FAILURE = new Socks5PasswordAuthStatus(0xFF, "FAILURE");
26  
27      public static Socks5PasswordAuthStatus valueOf(byte b) {
28          switch (b) {
29          case 0x00:
30              return SUCCESS;
31          case (byte) 0xFF:
32              return FAILURE;
33          }
34  
35          return new Socks5PasswordAuthStatus(b);
36      }
37  
38      private final byte byteValue;
39      private final String name;
40      private String text;
41  
42      public Socks5PasswordAuthStatus(int byteValue) {
43          this(byteValue, "UNKNOWN");
44      }
45  
46      public Socks5PasswordAuthStatus(int byteValue, String name) {
47          if (name == null) {
48              throw new NullPointerException("name");
49          }
50  
51          this.byteValue = (byte) byteValue;
52          this.name = name;
53      }
54  
55      public byte byteValue() {
56          return byteValue;
57      }
58  
59      public boolean isSuccess() {
60          return byteValue == 0;
61      }
62  
63      @Override
64      public int hashCode() {
65          return byteValue;
66      }
67  
68      @Override
69      public boolean equals(Object obj) {
70          if (!(obj instanceof Socks5PasswordAuthStatus)) {
71              return false;
72          }
73  
74          return byteValue == ((Socks5PasswordAuthStatus) obj).byteValue;
75      }
76  
77      @Override
78      public int compareTo(Socks5PasswordAuthStatus o) {
79          return byteValue - o.byteValue;
80      }
81  
82      @Override
83      public String toString() {
84          String text = this.text;
85          if (text == null) {
86              this.text = text = name + '(' + (byteValue & 0xFF) + ')';
87          }
88          return text;
89      }
90  }