Wed, 31 Dec 2014 07:22:50 +0100
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 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.Serializable; |
michael@0 | 31 | import ch.boye.httpclientandroidlib.util.CharArrayBuffer; |
michael@0 | 32 | |
michael@0 | 33 | /** |
michael@0 | 34 | * Represents a protocol version. The "major.minor" numbering |
michael@0 | 35 | * scheme is used to indicate versions of the protocol. |
michael@0 | 36 | * <p> |
michael@0 | 37 | * This class defines a protocol version as a combination of |
michael@0 | 38 | * protocol name, major version number, and minor version number. |
michael@0 | 39 | * Note that {@link #equals} and {@link #hashCode} are defined as |
michael@0 | 40 | * final here, they cannot be overridden in derived classes. |
michael@0 | 41 | * </p> |
michael@0 | 42 | * |
michael@0 | 43 | * @since 4.0 |
michael@0 | 44 | */ |
michael@0 | 45 | public class ProtocolVersion implements Serializable, Cloneable { |
michael@0 | 46 | |
michael@0 | 47 | private static final long serialVersionUID = 8950662842175091068L; |
michael@0 | 48 | |
michael@0 | 49 | |
michael@0 | 50 | /** Name of the protocol. */ |
michael@0 | 51 | protected final String protocol; |
michael@0 | 52 | |
michael@0 | 53 | /** Major version number of the protocol */ |
michael@0 | 54 | protected final int major; |
michael@0 | 55 | |
michael@0 | 56 | /** Minor version number of the protocol */ |
michael@0 | 57 | protected final int minor; |
michael@0 | 58 | |
michael@0 | 59 | |
michael@0 | 60 | /** |
michael@0 | 61 | * Create a protocol version designator. |
michael@0 | 62 | * |
michael@0 | 63 | * @param protocol the name of the protocol, for example "HTTP" |
michael@0 | 64 | * @param major the major version number of the protocol |
michael@0 | 65 | * @param minor the minor version number of the protocol |
michael@0 | 66 | */ |
michael@0 | 67 | public ProtocolVersion(String protocol, int major, int minor) { |
michael@0 | 68 | if (protocol == null) { |
michael@0 | 69 | throw new IllegalArgumentException |
michael@0 | 70 | ("Protocol name must not be null."); |
michael@0 | 71 | } |
michael@0 | 72 | if (major < 0) { |
michael@0 | 73 | throw new IllegalArgumentException |
michael@0 | 74 | ("Protocol major version number must not be negative."); |
michael@0 | 75 | } |
michael@0 | 76 | if (minor < 0) { |
michael@0 | 77 | throw new IllegalArgumentException |
michael@0 | 78 | ("Protocol minor version number may not be negative"); |
michael@0 | 79 | } |
michael@0 | 80 | this.protocol = protocol; |
michael@0 | 81 | this.major = major; |
michael@0 | 82 | this.minor = minor; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | /** |
michael@0 | 86 | * Returns the name of the protocol. |
michael@0 | 87 | * |
michael@0 | 88 | * @return the protocol name |
michael@0 | 89 | */ |
michael@0 | 90 | public final String getProtocol() { |
michael@0 | 91 | return protocol; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | /** |
michael@0 | 95 | * Returns the major version number of the protocol. |
michael@0 | 96 | * |
michael@0 | 97 | * @return the major version number. |
michael@0 | 98 | */ |
michael@0 | 99 | public final int getMajor() { |
michael@0 | 100 | return major; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | /** |
michael@0 | 104 | * Returns the minor version number of the HTTP protocol. |
michael@0 | 105 | * |
michael@0 | 106 | * @return the minor version number. |
michael@0 | 107 | */ |
michael@0 | 108 | public final int getMinor() { |
michael@0 | 109 | return minor; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | |
michael@0 | 113 | /** |
michael@0 | 114 | * Obtains a specific version of this protocol. |
michael@0 | 115 | * This can be used by derived classes to instantiate themselves instead |
michael@0 | 116 | * of the base class, and to define constants for commonly used versions. |
michael@0 | 117 | * <br/> |
michael@0 | 118 | * The default implementation in this class returns <code>this</code> |
michael@0 | 119 | * if the version matches, and creates a new {@link ProtocolVersion} |
michael@0 | 120 | * otherwise. |
michael@0 | 121 | * |
michael@0 | 122 | * @param major the major version |
michael@0 | 123 | * @param minor the minor version |
michael@0 | 124 | * |
michael@0 | 125 | * @return a protocol version with the same protocol name |
michael@0 | 126 | * and the argument version |
michael@0 | 127 | */ |
michael@0 | 128 | public ProtocolVersion forVersion(int major, int minor) { |
michael@0 | 129 | |
michael@0 | 130 | if ((major == this.major) && (minor == this.minor)) { |
michael@0 | 131 | return this; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | // argument checking is done in the constructor |
michael@0 | 135 | return new ProtocolVersion(this.protocol, major, minor); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | |
michael@0 | 139 | /** |
michael@0 | 140 | * Obtains a hash code consistent with {@link #equals}. |
michael@0 | 141 | * |
michael@0 | 142 | * @return the hashcode of this protocol version |
michael@0 | 143 | */ |
michael@0 | 144 | public final int hashCode() { |
michael@0 | 145 | return this.protocol.hashCode() ^ (this.major * 100000) ^ this.minor; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | |
michael@0 | 149 | /** |
michael@0 | 150 | * Checks equality of this protocol version with an object. |
michael@0 | 151 | * The object is equal if it is a protocl version with the same |
michael@0 | 152 | * protocol name, major version number, and minor version number. |
michael@0 | 153 | * The specific class of the object is <i>not</i> relevant, |
michael@0 | 154 | * instances of derived classes with identical attributes are |
michael@0 | 155 | * equal to instances of the base class and vice versa. |
michael@0 | 156 | * |
michael@0 | 157 | * @param obj the object to compare with |
michael@0 | 158 | * |
michael@0 | 159 | * @return <code>true</code> if the argument is the same protocol version, |
michael@0 | 160 | * <code>false</code> otherwise |
michael@0 | 161 | */ |
michael@0 | 162 | public final boolean equals(Object obj) { |
michael@0 | 163 | if (this == obj) { |
michael@0 | 164 | return true; |
michael@0 | 165 | } |
michael@0 | 166 | if (!(obj instanceof ProtocolVersion)) { |
michael@0 | 167 | return false; |
michael@0 | 168 | } |
michael@0 | 169 | ProtocolVersion that = (ProtocolVersion) obj; |
michael@0 | 170 | |
michael@0 | 171 | return ((this.protocol.equals(that.protocol)) && |
michael@0 | 172 | (this.major == that.major) && |
michael@0 | 173 | (this.minor == that.minor)); |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | |
michael@0 | 177 | /** |
michael@0 | 178 | * Checks whether this protocol can be compared to another one. |
michael@0 | 179 | * Only protocol versions with the same protocol name can be |
michael@0 | 180 | * {@link #compareToVersion compared}. |
michael@0 | 181 | * |
michael@0 | 182 | * @param that the protocol version to consider |
michael@0 | 183 | * |
michael@0 | 184 | * @return <code>true</code> if {@link #compareToVersion compareToVersion} |
michael@0 | 185 | * can be called with the argument, <code>false</code> otherwise |
michael@0 | 186 | */ |
michael@0 | 187 | public boolean isComparable(ProtocolVersion that) { |
michael@0 | 188 | return (that != null) && this.protocol.equals(that.protocol); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | |
michael@0 | 192 | /** |
michael@0 | 193 | * Compares this protocol version with another one. |
michael@0 | 194 | * Only protocol versions with the same protocol name can be compared. |
michael@0 | 195 | * This method does <i>not</i> define a total ordering, as it would be |
michael@0 | 196 | * required for {@link java.lang.Comparable}. |
michael@0 | 197 | * |
michael@0 | 198 | * @param that the protocl version to compare with |
michael@0 | 199 | * |
michael@0 | 200 | * @return a negative integer, zero, or a positive integer |
michael@0 | 201 | * as this version is less than, equal to, or greater than |
michael@0 | 202 | * the argument version. |
michael@0 | 203 | * |
michael@0 | 204 | * @throws IllegalArgumentException |
michael@0 | 205 | * if the argument has a different protocol name than this object, |
michael@0 | 206 | * or if the argument is <code>null</code> |
michael@0 | 207 | */ |
michael@0 | 208 | public int compareToVersion(ProtocolVersion that) { |
michael@0 | 209 | if (that == null) { |
michael@0 | 210 | throw new IllegalArgumentException |
michael@0 | 211 | ("Protocol version must not be null."); |
michael@0 | 212 | } |
michael@0 | 213 | if (!this.protocol.equals(that.protocol)) { |
michael@0 | 214 | throw new IllegalArgumentException |
michael@0 | 215 | ("Versions for different protocols cannot be compared. " + |
michael@0 | 216 | this + " " + that); |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | int delta = getMajor() - that.getMajor(); |
michael@0 | 220 | if (delta == 0) { |
michael@0 | 221 | delta = getMinor() - that.getMinor(); |
michael@0 | 222 | } |
michael@0 | 223 | return delta; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | |
michael@0 | 227 | /** |
michael@0 | 228 | * Tests if this protocol version is greater or equal to the given one. |
michael@0 | 229 | * |
michael@0 | 230 | * @param version the version against which to check this version |
michael@0 | 231 | * |
michael@0 | 232 | * @return <code>true</code> if this protocol version is |
michael@0 | 233 | * {@link #isComparable comparable} to the argument |
michael@0 | 234 | * and {@link #compareToVersion compares} as greater or equal, |
michael@0 | 235 | * <code>false</code> otherwise |
michael@0 | 236 | */ |
michael@0 | 237 | public final boolean greaterEquals(ProtocolVersion version) { |
michael@0 | 238 | return isComparable(version) && (compareToVersion(version) >= 0); |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | |
michael@0 | 242 | /** |
michael@0 | 243 | * Tests if this protocol version is less or equal to the given one. |
michael@0 | 244 | * |
michael@0 | 245 | * @param version the version against which to check this version |
michael@0 | 246 | * |
michael@0 | 247 | * @return <code>true</code> if this protocol version is |
michael@0 | 248 | * {@link #isComparable comparable} to the argument |
michael@0 | 249 | * and {@link #compareToVersion compares} as less or equal, |
michael@0 | 250 | * <code>false</code> otherwise |
michael@0 | 251 | */ |
michael@0 | 252 | public final boolean lessEquals(ProtocolVersion version) { |
michael@0 | 253 | return isComparable(version) && (compareToVersion(version) <= 0); |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | |
michael@0 | 257 | /** |
michael@0 | 258 | * Converts this protocol version to a string. |
michael@0 | 259 | * |
michael@0 | 260 | * @return a protocol version string, like "HTTP/1.1" |
michael@0 | 261 | */ |
michael@0 | 262 | public String toString() { |
michael@0 | 263 | CharArrayBuffer buffer = new CharArrayBuffer(16); |
michael@0 | 264 | buffer.append(this.protocol); |
michael@0 | 265 | buffer.append('/'); |
michael@0 | 266 | buffer.append(Integer.toString(this.major)); |
michael@0 | 267 | buffer.append('.'); |
michael@0 | 268 | buffer.append(Integer.toString(this.minor)); |
michael@0 | 269 | return buffer.toString(); |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | public Object clone() throws CloneNotSupportedException { |
michael@0 | 273 | return super.clone(); |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | } |