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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/scheme/SchemeRegistry.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,173 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +package ch.boye.httpclientandroidlib.conn.scheme;
    1.31 +
    1.32 +import java.util.ArrayList;
    1.33 +import java.util.List;
    1.34 +import java.util.Map;
    1.35 +import java.util.concurrent.ConcurrentHashMap;
    1.36 +
    1.37 +import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
    1.38 +
    1.39 +import ch.boye.httpclientandroidlib.HttpHost;
    1.40 +
    1.41 +/**
    1.42 + * A set of supported protocol {@link Scheme}s.
    1.43 + * Schemes are identified by lowercase names.
    1.44 + *
    1.45 + * @since 4.0
    1.46 + */
    1.47 +@ThreadSafe
    1.48 +public final class SchemeRegistry {
    1.49 +
    1.50 +    /** The available schemes in this registry. */
    1.51 +    private final ConcurrentHashMap<String,Scheme> registeredSchemes;
    1.52 +
    1.53 +    /**
    1.54 +     * Creates a new, empty scheme registry.
    1.55 +     */
    1.56 +    public SchemeRegistry() {
    1.57 +        super();
    1.58 +        registeredSchemes = new ConcurrentHashMap<String,Scheme>();
    1.59 +    }
    1.60 +
    1.61 +    /**
    1.62 +     * Obtains a scheme by name.
    1.63 +     *
    1.64 +     * @param name      the name of the scheme to look up (in lowercase)
    1.65 +     *
    1.66 +     * @return  the scheme, never <code>null</code>
    1.67 +     *
    1.68 +     * @throws IllegalStateException
    1.69 +     *          if the scheme with the given name is not registered
    1.70 +     */
    1.71 +    public final Scheme getScheme(String name) {
    1.72 +        Scheme found = get(name);
    1.73 +        if (found == null) {
    1.74 +            throw new IllegalStateException
    1.75 +                ("Scheme '"+name+"' not registered.");
    1.76 +        }
    1.77 +        return found;
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * Obtains the scheme for a host.
    1.82 +     * Convenience method for <code>getScheme(host.getSchemeName())</pre>
    1.83 +     *
    1.84 +     * @param host      the host for which to obtain the scheme
    1.85 +     *
    1.86 +     * @return  the scheme for the given host, never <code>null</code>
    1.87 +     *
    1.88 +     * @throws IllegalStateException
    1.89 +     *          if a scheme with the respective name is not registered
    1.90 +     */
    1.91 +    public final Scheme getScheme(HttpHost host) {
    1.92 +        if (host == null) {
    1.93 +            throw new IllegalArgumentException("Host must not be null.");
    1.94 +        }
    1.95 +        return getScheme(host.getSchemeName());
    1.96 +    }
    1.97 +
    1.98 +    /**
    1.99 +     * Obtains a scheme by name, if registered.
   1.100 +     *
   1.101 +     * @param name      the name of the scheme to look up (in lowercase)
   1.102 +     *
   1.103 +     * @return  the scheme, or
   1.104 +     *          <code>null</code> if there is none by this name
   1.105 +     */
   1.106 +    public final Scheme get(String name) {
   1.107 +        if (name == null)
   1.108 +            throw new IllegalArgumentException("Name must not be null.");
   1.109 +
   1.110 +        // leave it to the caller to use the correct name - all lowercase
   1.111 +        //name = name.toLowerCase();
   1.112 +        Scheme found = registeredSchemes.get(name);
   1.113 +        return found;
   1.114 +    }
   1.115 +
   1.116 +    /**
   1.117 +     * Registers a scheme.
   1.118 +     * The scheme can later be retrieved by its name
   1.119 +     * using {@link #getScheme(String) getScheme} or {@link #get get}.
   1.120 +     *
   1.121 +     * @param sch       the scheme to register
   1.122 +     *
   1.123 +     * @return  the scheme previously registered with that name, or
   1.124 +     *          <code>null</code> if none was registered
   1.125 +     */
   1.126 +    public final Scheme register(Scheme sch) {
   1.127 +        if (sch == null)
   1.128 +            throw new IllegalArgumentException("Scheme must not be null.");
   1.129 +
   1.130 +        Scheme old = registeredSchemes.put(sch.getName(), sch);
   1.131 +        return old;
   1.132 +    }
   1.133 +
   1.134 +    /**
   1.135 +     * Unregisters a scheme.
   1.136 +     *
   1.137 +     * @param name      the name of the scheme to unregister (in lowercase)
   1.138 +     *
   1.139 +     * @return  the unregistered scheme, or
   1.140 +     *          <code>null</code> if there was none
   1.141 +     */
   1.142 +    public final Scheme unregister(String name) {
   1.143 +        if (name == null)
   1.144 +            throw new IllegalArgumentException("Name must not be null.");
   1.145 +
   1.146 +        // leave it to the caller to use the correct name - all lowercase
   1.147 +        //name = name.toLowerCase();
   1.148 +        Scheme gone = registeredSchemes.remove(name);
   1.149 +        return gone;
   1.150 +    }
   1.151 +
   1.152 +    /**
   1.153 +     * Obtains the names of the registered schemes.
   1.154 +     *
   1.155 +     * @return  List containing registered scheme names.
   1.156 +     */
   1.157 +    public final List<String> getSchemeNames() {
   1.158 +        return new ArrayList<String>(registeredSchemes.keySet());
   1.159 +    }
   1.160 +
   1.161 +    /**
   1.162 +     * Populates the internal collection of registered {@link Scheme protocol schemes}
   1.163 +     * with the content of the map passed as a parameter.
   1.164 +     *
   1.165 +     * @param map protocol schemes
   1.166 +     */
   1.167 +    public void setItems(final Map<String, Scheme> map) {
   1.168 +        if (map == null) {
   1.169 +            return;
   1.170 +        }
   1.171 +        registeredSchemes.clear();
   1.172 +        registeredSchemes.putAll(map);
   1.173 +    }
   1.174 +
   1.175 +}
   1.176 +

mercurial