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    *   https://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.netty5.channel.local;
17  
18  import io.netty5.channel.Channel;
19  
20  import java.net.SocketAddress;
21  import java.util.UUID;
22  
23  import static io.netty5.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
24  
25  /**
26   * An endpoint in the local transport.  Each endpoint is identified by a unique
27   * case-insensitive string.
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       * Creates a new ephemeral port based on the ID of the specified channel.
40       * Note that we prepend an upper-case character so that it never conflicts with
41       * the addresses created by a user, which are always lower-cased on construction time.
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       * Creates a new instance with the specified ID.
54       */
55      public LocalAddress(String id) {
56          this.id = checkNonEmptyAfterTrim(id, "id").toLowerCase();
57          this.strVal = "local:" + this.id;
58      }
59  
60      /**
61       * Creates a new instance with a random ID based on the given class.
62       */
63      public LocalAddress(Class<?> cls) {
64          this(cls.getSimpleName() + '/' + UUID.randomUUID());
65      }
66  
67      /**
68       * Returns the ID of this address.
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  }