1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.channel.local;
17
18 import java.net.SocketAddress;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public final class LocalAddress extends SocketAddress implements Comparable<LocalAddress> {
39
40 private static final long serialVersionUID = -3601961747680808645L;
41
42 public static final String EPHEMERAL = "ephemeral";
43
44 private final String id;
45 private final boolean ephemeral;
46
47
48
49
50 public LocalAddress(int id) {
51 this(String.valueOf(id));
52 }
53
54
55
56
57 public LocalAddress(String id) {
58 if (id == null) {
59 throw new NullPointerException("id");
60 }
61 id = id.trim().toLowerCase();
62 if (id.length() == 0) {
63 throw new IllegalArgumentException("empty id");
64 }
65 this.id = id;
66 ephemeral = id.equals("ephemeral");
67 }
68
69
70
71
72 public String getId() {
73 return id;
74 }
75
76
77
78
79 public boolean isEphemeral() {
80 return ephemeral;
81 }
82
83 @Override
84 public int hashCode() {
85 if (ephemeral) {
86 return System.identityHashCode(this);
87 } else {
88 return id.hashCode();
89 }
90 }
91
92 @Override
93 public boolean equals(Object o) {
94 if (!(o instanceof LocalAddress)) {
95 return false;
96 }
97
98 if (ephemeral) {
99 return this == o;
100 } else {
101 return getId().equals(((LocalAddress) o).getId());
102 }
103 }
104
105
106
107
108
109 public int compareTo(LocalAddress o) {
110 if (ephemeral) {
111 if (o.ephemeral) {
112 if (this == o){
113 return 0;
114 }
115
116 int a = System.identityHashCode(this);
117 int b = System.identityHashCode(this);
118 if (a < b) {
119 return -1;
120 } else if (a > b) {
121 return 1;
122 } else {
123 throw new Error(
124 "Two different ephemeral addresses have " +
125 "same identityHashCode.");
126 }
127 } else {
128 return 1;
129 }
130 } else {
131 if (o.ephemeral) {
132 return -1;
133 } else {
134 return getId().compareTo(o.getId());
135 }
136 }
137 }
138
139 @Override
140 public String toString() {
141 return "local:" + getId();
142 }
143 }