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