mobile/android/thirdparty/ch/boye/httpclientandroidlib/ProtocolVersion.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/ProtocolVersion.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,276 @@
     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 +
    1.31 +package ch.boye.httpclientandroidlib;
    1.32 +
    1.33 +import java.io.Serializable;
    1.34 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
    1.35 +
    1.36 +/**
    1.37 + * Represents a protocol version. The "major.minor" numbering
    1.38 + * scheme is used to indicate versions of the protocol.
    1.39 + * <p>
    1.40 + * This class defines a protocol version as a combination of
    1.41 + * protocol name, major version number, and minor version number.
    1.42 + * Note that {@link #equals} and {@link #hashCode} are defined as
    1.43 + * final here, they cannot be overridden in derived classes.
    1.44 + * </p>
    1.45 + *
    1.46 + * @since 4.0
    1.47 + */
    1.48 +public class ProtocolVersion implements Serializable, Cloneable {
    1.49 +
    1.50 +    private static final long serialVersionUID = 8950662842175091068L;
    1.51 +
    1.52 +
    1.53 +    /** Name of the protocol. */
    1.54 +    protected final String protocol;
    1.55 +
    1.56 +    /** Major version number of the protocol */
    1.57 +    protected final int major;
    1.58 +
    1.59 +    /** Minor version number of the protocol */
    1.60 +    protected final int minor;
    1.61 +
    1.62 +
    1.63 +    /**
    1.64 +     * Create a protocol version designator.
    1.65 +     *
    1.66 +     * @param protocol   the name of the protocol, for example "HTTP"
    1.67 +     * @param major      the major version number of the protocol
    1.68 +     * @param minor      the minor version number of the protocol
    1.69 +     */
    1.70 +    public ProtocolVersion(String protocol, int major, int minor) {
    1.71 +        if (protocol == null) {
    1.72 +            throw new IllegalArgumentException
    1.73 +                ("Protocol name must not be null.");
    1.74 +        }
    1.75 +        if (major < 0) {
    1.76 +            throw new IllegalArgumentException
    1.77 +                ("Protocol major version number must not be negative.");
    1.78 +        }
    1.79 +        if (minor < 0) {
    1.80 +            throw new IllegalArgumentException
    1.81 +                ("Protocol minor version number may not be negative");
    1.82 +        }
    1.83 +        this.protocol = protocol;
    1.84 +        this.major = major;
    1.85 +        this.minor = minor;
    1.86 +    }
    1.87 +
    1.88 +    /**
    1.89 +     * Returns the name of the protocol.
    1.90 +     *
    1.91 +     * @return the protocol name
    1.92 +     */
    1.93 +    public final String getProtocol() {
    1.94 +        return protocol;
    1.95 +    }
    1.96 +
    1.97 +    /**
    1.98 +     * Returns the major version number of the protocol.
    1.99 +     *
   1.100 +     * @return the major version number.
   1.101 +     */
   1.102 +    public final int getMajor() {
   1.103 +        return major;
   1.104 +    }
   1.105 +
   1.106 +    /**
   1.107 +     * Returns the minor version number of the HTTP protocol.
   1.108 +     *
   1.109 +     * @return the minor version number.
   1.110 +     */
   1.111 +    public final int getMinor() {
   1.112 +        return minor;
   1.113 +    }
   1.114 +
   1.115 +
   1.116 +    /**
   1.117 +     * Obtains a specific version of this protocol.
   1.118 +     * This can be used by derived classes to instantiate themselves instead
   1.119 +     * of the base class, and to define constants for commonly used versions.
   1.120 +     * <br/>
   1.121 +     * The default implementation in this class returns <code>this</code>
   1.122 +     * if the version matches, and creates a new {@link ProtocolVersion}
   1.123 +     * otherwise.
   1.124 +     *
   1.125 +     * @param major     the major version
   1.126 +     * @param minor     the minor version
   1.127 +     *
   1.128 +     * @return  a protocol version with the same protocol name
   1.129 +     *          and the argument version
   1.130 +     */
   1.131 +    public ProtocolVersion forVersion(int major, int minor) {
   1.132 +
   1.133 +        if ((major == this.major) && (minor == this.minor)) {
   1.134 +            return this;
   1.135 +        }
   1.136 +
   1.137 +        // argument checking is done in the constructor
   1.138 +        return new ProtocolVersion(this.protocol, major, minor);
   1.139 +    }
   1.140 +
   1.141 +
   1.142 +    /**
   1.143 +     * Obtains a hash code consistent with {@link #equals}.
   1.144 +     *
   1.145 +     * @return  the hashcode of this protocol version
   1.146 +     */
   1.147 +    public final int hashCode() {
   1.148 +        return this.protocol.hashCode() ^ (this.major * 100000) ^ this.minor;
   1.149 +    }
   1.150 +
   1.151 +
   1.152 +    /**
   1.153 +     * Checks equality of this protocol version with an object.
   1.154 +     * The object is equal if it is a protocl version with the same
   1.155 +     * protocol name, major version number, and minor version number.
   1.156 +     * The specific class of the object is <i>not</i> relevant,
   1.157 +     * instances of derived classes with identical attributes are
   1.158 +     * equal to instances of the base class and vice versa.
   1.159 +     *
   1.160 +     * @param obj       the object to compare with
   1.161 +     *
   1.162 +     * @return  <code>true</code> if the argument is the same protocol version,
   1.163 +     *          <code>false</code> otherwise
   1.164 +     */
   1.165 +    public final boolean equals(Object obj) {
   1.166 +        if (this == obj) {
   1.167 +            return true;
   1.168 +        }
   1.169 +        if (!(obj instanceof ProtocolVersion)) {
   1.170 +            return false;
   1.171 +        }
   1.172 +        ProtocolVersion that = (ProtocolVersion) obj;
   1.173 +
   1.174 +        return ((this.protocol.equals(that.protocol)) &&
   1.175 +                (this.major == that.major) &&
   1.176 +                (this.minor == that.minor));
   1.177 +    }
   1.178 +
   1.179 +
   1.180 +    /**
   1.181 +     * Checks whether this protocol can be compared to another one.
   1.182 +     * Only protocol versions with the same protocol name can be
   1.183 +     * {@link #compareToVersion compared}.
   1.184 +     *
   1.185 +     * @param that      the protocol version to consider
   1.186 +     *
   1.187 +     * @return  <code>true</code> if {@link #compareToVersion compareToVersion}
   1.188 +     *          can be called with the argument, <code>false</code> otherwise
   1.189 +     */
   1.190 +    public boolean isComparable(ProtocolVersion that) {
   1.191 +        return (that != null) && this.protocol.equals(that.protocol);
   1.192 +    }
   1.193 +
   1.194 +
   1.195 +    /**
   1.196 +     * Compares this protocol version with another one.
   1.197 +     * Only protocol versions with the same protocol name can be compared.
   1.198 +     * This method does <i>not</i> define a total ordering, as it would be
   1.199 +     * required for {@link java.lang.Comparable}.
   1.200 +     *
   1.201 +     * @param that      the protocl version to compare with
   1.202 +     *
   1.203 +     * @return   a negative integer, zero, or a positive integer
   1.204 +     *           as this version is less than, equal to, or greater than
   1.205 +     *           the argument version.
   1.206 +     *
   1.207 +     * @throws IllegalArgumentException
   1.208 +     *         if the argument has a different protocol name than this object,
   1.209 +     *         or if the argument is <code>null</code>
   1.210 +     */
   1.211 +    public int compareToVersion(ProtocolVersion that) {
   1.212 +        if (that == null) {
   1.213 +            throw new IllegalArgumentException
   1.214 +                ("Protocol version must not be null.");
   1.215 +        }
   1.216 +        if (!this.protocol.equals(that.protocol)) {
   1.217 +            throw new IllegalArgumentException
   1.218 +                ("Versions for different protocols cannot be compared. " +
   1.219 +                 this + " " + that);
   1.220 +        }
   1.221 +
   1.222 +        int delta = getMajor() - that.getMajor();
   1.223 +        if (delta == 0) {
   1.224 +            delta = getMinor() - that.getMinor();
   1.225 +        }
   1.226 +        return delta;
   1.227 +    }
   1.228 +
   1.229 +
   1.230 +    /**
   1.231 +     * Tests if this protocol version is greater or equal to the given one.
   1.232 +     *
   1.233 +     * @param version   the version against which to check this version
   1.234 +     *
   1.235 +     * @return  <code>true</code> if this protocol version is
   1.236 +     *          {@link #isComparable comparable} to the argument
   1.237 +     *          and {@link #compareToVersion compares} as greater or equal,
   1.238 +     *          <code>false</code> otherwise
   1.239 +     */
   1.240 +    public final boolean greaterEquals(ProtocolVersion version) {
   1.241 +        return isComparable(version) && (compareToVersion(version) >= 0);
   1.242 +    }
   1.243 +
   1.244 +
   1.245 +    /**
   1.246 +     * Tests if this protocol version is less or equal to the given one.
   1.247 +     *
   1.248 +     * @param version   the version against which to check this version
   1.249 +     *
   1.250 +     * @return  <code>true</code> if this protocol version is
   1.251 +     *          {@link #isComparable comparable} to the argument
   1.252 +     *          and {@link #compareToVersion compares} as less or equal,
   1.253 +     *          <code>false</code> otherwise
   1.254 +     */
   1.255 +    public final boolean lessEquals(ProtocolVersion version) {
   1.256 +        return isComparable(version) && (compareToVersion(version) <= 0);
   1.257 +    }
   1.258 +
   1.259 +
   1.260 +    /**
   1.261 +     * Converts this protocol version to a string.
   1.262 +     *
   1.263 +     * @return  a protocol version string, like "HTTP/1.1"
   1.264 +     */
   1.265 +    public String toString() {
   1.266 +        CharArrayBuffer buffer = new CharArrayBuffer(16);
   1.267 +        buffer.append(this.protocol);
   1.268 +        buffer.append('/');
   1.269 +        buffer.append(Integer.toString(this.major));
   1.270 +        buffer.append('.');
   1.271 +        buffer.append(Integer.toString(this.minor));
   1.272 +        return buffer.toString();
   1.273 +    }
   1.274 +
   1.275 +    public Object clone() throws CloneNotSupportedException {
   1.276 +        return super.clone();
   1.277 +    }
   1.278 +
   1.279 +}

mercurial