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