1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.local;
17
18 import io.netty.channel.Channel;
19
20 import java.net.SocketAddress;
21
22
23
24
25
26 public final class LocalAddress extends SocketAddress implements Comparable<LocalAddress> {
27
28 private static final long serialVersionUID = 4644331421130916435L;
29
30 public static final LocalAddress ANY = new LocalAddress("ANY");
31
32 private final String id;
33 private final String strVal;
34
35
36
37
38
39
40 LocalAddress(Channel channel) {
41 StringBuilder buf = new StringBuilder(16);
42 buf.append("local:E");
43 buf.append(Long.toHexString(channel.hashCode() & 0xFFFFFFFFL | 0x100000000L));
44 buf.setCharAt(7, ':');
45 id = buf.substring(6);
46 strVal = buf.toString();
47 }
48
49
50
51
52 public LocalAddress(String id) {
53 if (id == null) {
54 throw new NullPointerException("id");
55 }
56 id = id.trim().toLowerCase();
57 if (id.isEmpty()) {
58 throw new IllegalArgumentException("empty id");
59 }
60 this.id = id;
61 strVal = "local:" + id;
62 }
63
64
65
66
67 public String id() {
68 return id;
69 }
70
71 @Override
72 public int hashCode() {
73 return id.hashCode();
74 }
75
76 @Override
77 public boolean equals(Object o) {
78 if (!(o instanceof LocalAddress)) {
79 return false;
80 }
81
82 return id.equals(((LocalAddress) o).id);
83 }
84
85 @Override
86 public int compareTo(LocalAddress o) {
87 return id.compareTo(o.id);
88 }
89
90 @Override
91 public String toString() {
92 return strVal;
93 }
94 }