View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.nio.conn.scheme;
28  
29  import java.util.Locale;
30  
31  import org.apache.http.util.Args;
32  import org.apache.http.util.LangUtils;
33  
34  @Deprecated
35  public final class AsyncScheme {
36  
37      /** The name of this scheme, in lowercase. (e.g. http, https) */
38      private final String name;
39  
40      /** The layering strategy for this scheme, if applicable */
41      private final LayeringStrategy strategy;
42  
43      /** The default port for this scheme */
44      private final int defaultPort;
45  
46      /** A string representation, for {@link #toString toString}. */
47      private String stringRep;
48      /*
49       *  This is used to cache the result of the toString() method
50       *  Since the method always generates the same value, there's no
51       *  need to synchronize, and it does not affect immutability.
52      */
53  
54      public AsyncScheme(final String name, final int port, final LayeringStrategy strategy) {
55          Args.notNull(name, "Scheme name");
56          if ((port <= 0) || (port > 0xffff)) {
57              throw new IllegalArgumentException("Port is invalid: " + port);
58          }
59          this.name = name.toLowerCase(Locale.ROOT);
60          this.strategy = strategy;
61          this.defaultPort = port;
62      }
63  
64      public final int getDefaultPort() {
65          return defaultPort;
66      }
67  
68      public final LayeringStrategy getLayeringStrategy() {
69          return this.strategy;
70      }
71  
72      public final String getName() {
73          return name;
74      }
75  
76      public final int resolvePort(final int port) {
77          return port <= 0 ? defaultPort : port;
78      }
79  
80      @Override
81      public final String toString() {
82          if (stringRep == null) {
83              final StringBuilder buffer = new StringBuilder();
84              buffer.append(this.name);
85              buffer.append(':');
86              buffer.append(Integer.toString(this.defaultPort));
87              stringRep = buffer.toString();
88          }
89          return stringRep;
90      }
91  
92      @Override
93      public final boolean equals(final Object obj) {
94          if (this == obj) {
95              return true;
96          }
97          if (obj instanceof AsyncScheme) {
98              final AsyncScheme that = (AsyncScheme) obj;
99              return this.name.equals(that.name)
100                 && this.defaultPort == that.defaultPort
101                 && this.strategy.equals(that.strategy);
102         } else {
103             return false;
104         }
105     }
106 
107     @Override
108     public int hashCode() {
109         int hash = LangUtils.HASH_SEED;
110         hash = LangUtils.hashCode(hash, this.defaultPort);
111         hash = LangUtils.hashCode(hash, this.name);
112         hash = LangUtils.hashCode(hash, this.strategy);
113         return hash;
114     }
115 
116 }