View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.client;
20  
21  import java.util.Objects;
22  
23  import org.eclipse.jetty.util.URIUtil;
24  
25  public class Origin
26  {
27      private final String scheme;
28      private final Address address;
29  
30      public Origin(String scheme, String host, int port)
31      {
32          this(scheme, new Address(host, port));
33      }
34  
35      public Origin(String scheme, Address address)
36      {
37          this.scheme = Objects.requireNonNull(scheme);
38          this.address = address;
39      }
40  
41      public String getScheme()
42      {
43          return scheme;
44      }
45  
46      public Address getAddress()
47      {
48          return address;
49      }
50  
51      public String asString()
52      {
53          StringBuilder result = new StringBuilder();
54          URIUtil.appendSchemeHostPort(result, scheme, address.host, address.port);
55          return result.toString();
56      }
57  
58      @Override
59      public boolean equals(Object obj)
60      {
61          if (this == obj) return true;
62          if (obj == null || getClass() != obj.getClass()) return false;
63          Origin that = (Origin)obj;
64          return scheme.equals(that.scheme) && address.equals(that.address);
65      }
66  
67      @Override
68      public int hashCode()
69      {
70          int result = scheme.hashCode();
71          result = 31 * result + address.hashCode();
72          return result;
73      }
74  
75      public static class Address
76      {
77          private final String host;
78          private final int port;
79  
80          public Address(String host, int port)
81          {
82              this.host = Objects.requireNonNull(host);
83              this.port = port;
84          }
85  
86          public String getHost()
87          {
88              return host;
89          }
90  
91          public int getPort()
92          {
93              return port;
94          }
95  
96          @Override
97          public boolean equals(Object obj)
98          {
99              if (this == obj) return true;
100             if (obj == null || getClass() != obj.getClass()) return false;
101             Address that = (Address)obj;
102             return host.equals(that.host) && port == that.port;
103         }
104 
105         @Override
106         public int hashCode()
107         {
108             int result = host.hashCode();
109             result = 31 * result + port;
110             return result;
111         }
112 
113         public String asString()
114         {
115             return String.format("%s:%d", host, port);
116         }
117 
118         @Override
119         public String toString()
120         {
121             return asString();
122         }
123     }
124 }