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