mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/scheme/SchemeRegistry.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27 package ch.boye.httpclientandroidlib.conn.scheme;
michael@0 28
michael@0 29 import java.util.ArrayList;
michael@0 30 import java.util.List;
michael@0 31 import java.util.Map;
michael@0 32 import java.util.concurrent.ConcurrentHashMap;
michael@0 33
michael@0 34 import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
michael@0 35
michael@0 36 import ch.boye.httpclientandroidlib.HttpHost;
michael@0 37
michael@0 38 /**
michael@0 39 * A set of supported protocol {@link Scheme}s.
michael@0 40 * Schemes are identified by lowercase names.
michael@0 41 *
michael@0 42 * @since 4.0
michael@0 43 */
michael@0 44 @ThreadSafe
michael@0 45 public final class SchemeRegistry {
michael@0 46
michael@0 47 /** The available schemes in this registry. */
michael@0 48 private final ConcurrentHashMap<String,Scheme> registeredSchemes;
michael@0 49
michael@0 50 /**
michael@0 51 * Creates a new, empty scheme registry.
michael@0 52 */
michael@0 53 public SchemeRegistry() {
michael@0 54 super();
michael@0 55 registeredSchemes = new ConcurrentHashMap<String,Scheme>();
michael@0 56 }
michael@0 57
michael@0 58 /**
michael@0 59 * Obtains a scheme by name.
michael@0 60 *
michael@0 61 * @param name the name of the scheme to look up (in lowercase)
michael@0 62 *
michael@0 63 * @return the scheme, never <code>null</code>
michael@0 64 *
michael@0 65 * @throws IllegalStateException
michael@0 66 * if the scheme with the given name is not registered
michael@0 67 */
michael@0 68 public final Scheme getScheme(String name) {
michael@0 69 Scheme found = get(name);
michael@0 70 if (found == null) {
michael@0 71 throw new IllegalStateException
michael@0 72 ("Scheme '"+name+"' not registered.");
michael@0 73 }
michael@0 74 return found;
michael@0 75 }
michael@0 76
michael@0 77 /**
michael@0 78 * Obtains the scheme for a host.
michael@0 79 * Convenience method for <code>getScheme(host.getSchemeName())</pre>
michael@0 80 *
michael@0 81 * @param host the host for which to obtain the scheme
michael@0 82 *
michael@0 83 * @return the scheme for the given host, never <code>null</code>
michael@0 84 *
michael@0 85 * @throws IllegalStateException
michael@0 86 * if a scheme with the respective name is not registered
michael@0 87 */
michael@0 88 public final Scheme getScheme(HttpHost host) {
michael@0 89 if (host == null) {
michael@0 90 throw new IllegalArgumentException("Host must not be null.");
michael@0 91 }
michael@0 92 return getScheme(host.getSchemeName());
michael@0 93 }
michael@0 94
michael@0 95 /**
michael@0 96 * Obtains a scheme by name, if registered.
michael@0 97 *
michael@0 98 * @param name the name of the scheme to look up (in lowercase)
michael@0 99 *
michael@0 100 * @return the scheme, or
michael@0 101 * <code>null</code> if there is none by this name
michael@0 102 */
michael@0 103 public final Scheme get(String name) {
michael@0 104 if (name == null)
michael@0 105 throw new IllegalArgumentException("Name must not be null.");
michael@0 106
michael@0 107 // leave it to the caller to use the correct name - all lowercase
michael@0 108 //name = name.toLowerCase();
michael@0 109 Scheme found = registeredSchemes.get(name);
michael@0 110 return found;
michael@0 111 }
michael@0 112
michael@0 113 /**
michael@0 114 * Registers a scheme.
michael@0 115 * The scheme can later be retrieved by its name
michael@0 116 * using {@link #getScheme(String) getScheme} or {@link #get get}.
michael@0 117 *
michael@0 118 * @param sch the scheme to register
michael@0 119 *
michael@0 120 * @return the scheme previously registered with that name, or
michael@0 121 * <code>null</code> if none was registered
michael@0 122 */
michael@0 123 public final Scheme register(Scheme sch) {
michael@0 124 if (sch == null)
michael@0 125 throw new IllegalArgumentException("Scheme must not be null.");
michael@0 126
michael@0 127 Scheme old = registeredSchemes.put(sch.getName(), sch);
michael@0 128 return old;
michael@0 129 }
michael@0 130
michael@0 131 /**
michael@0 132 * Unregisters a scheme.
michael@0 133 *
michael@0 134 * @param name the name of the scheme to unregister (in lowercase)
michael@0 135 *
michael@0 136 * @return the unregistered scheme, or
michael@0 137 * <code>null</code> if there was none
michael@0 138 */
michael@0 139 public final Scheme unregister(String name) {
michael@0 140 if (name == null)
michael@0 141 throw new IllegalArgumentException("Name must not be null.");
michael@0 142
michael@0 143 // leave it to the caller to use the correct name - all lowercase
michael@0 144 //name = name.toLowerCase();
michael@0 145 Scheme gone = registeredSchemes.remove(name);
michael@0 146 return gone;
michael@0 147 }
michael@0 148
michael@0 149 /**
michael@0 150 * Obtains the names of the registered schemes.
michael@0 151 *
michael@0 152 * @return List containing registered scheme names.
michael@0 153 */
michael@0 154 public final List<String> getSchemeNames() {
michael@0 155 return new ArrayList<String>(registeredSchemes.keySet());
michael@0 156 }
michael@0 157
michael@0 158 /**
michael@0 159 * Populates the internal collection of registered {@link Scheme protocol schemes}
michael@0 160 * with the content of the map passed as a parameter.
michael@0 161 *
michael@0 162 * @param map protocol schemes
michael@0 163 */
michael@0 164 public void setItems(final Map<String, Scheme> map) {
michael@0 165 if (map == null) {
michael@0 166 return;
michael@0 167 }
michael@0 168 registeredSchemes.clear();
michael@0 169 registeredSchemes.putAll(map);
michael@0 170 }
michael@0 171
michael@0 172 }
michael@0 173

mercurial