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 public final class LocalAddress extends SocketAddress implements Comparable<LocalAddress> {
33
34 private static final long serialVersionUID = -3601961747680808645L;
35
36 public static final String EPHEMERAL = "ephemeral";
37
38 private final String id;
39 private final boolean ephemeral;
40
41
42
43
44 public LocalAddress(int id) {
45 this(String.valueOf(id));
46 }
47
48
49
50
51 public LocalAddress(String id) {
52 if (id == null) {
53 throw new NullPointerException("id");
54 }
55 id = id.trim().toLowerCase();
56 if (id.length() == 0) {
57 throw new IllegalArgumentException("empty id");
58 }
59 this.id = id;
60 ephemeral = id.equals("ephemeral");
61 }
62
63
64
65
66 public String getId() {
67 return id;
68 }
69
70
71
72
73 public boolean isEphemeral() {
74 return ephemeral;
75 }
76
77 @Override
78 public int hashCode() {
79 if (ephemeral) {
80 return System.identityHashCode(this);
81 } else {
82 return id.hashCode();
83 }
84 }
85
86 @Override
87 public boolean equals(Object o) {
88 if (!(o instanceof LocalAddress)) {
89 return false;
90 }
91
92 if (ephemeral) {
93 return this == o;
94 } else {
95 return getId().equals(((LocalAddress) o).getId());
96 }
97 }
98
99
100
101
102
103 public int compareTo(LocalAddress o) {
104 if (ephemeral) {
105 if (o.ephemeral) {
106 if (this == o) {
107 return 0;
108 }
109
110 int a = System.identityHashCode(this);
111 int b = System.identityHashCode(o);
112 if (a < b) {
113 return -1;
114 } else if (a > b) {
115 return 1;
116 } else {
117 throw new Error(
118 "Two different ephemeral addresses have " +
119 "same identityHashCode.");
120 }
121 } else {
122 return 1;
123 }
124 } else {
125 if (o.ephemeral) {
126 return -1;
127 } else {
128 return getId().compareTo(o.getId());
129 }
130 }
131 }
132
133 @Override
134 public String toString() {
135 return "local:" + getId();
136 }
137 }