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    *   http://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.netty.channel.local;
17  
18  import io.netty.channel.Channel;
19  
20  import java.net.SocketAddress;
21  
22  /**
23   * An endpoint in the local transport.  Each endpoint is identified by a unique
24   * case-insensitive string.
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       * Creates a new ephemeral port based on the ID of the specified channel.
37       * Note that we prepend an upper-case character so that it never conflicts with
38       * the addresses created by a user, which are always lower-cased on construction time.
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       * Creates a new instance with the specified ID.
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       * Returns the ID of this address.
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  }