ProtocolVersion.java revision d42abb2fd917184764daf22f5f299e848b8701d7
11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)/* 21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/ProtocolVersion.java $ 31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * $Revision: 609106 $ 41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * $Date: 2008-01-05 01:15:42 -0800 (Sat, 05 Jan 2008) $ 51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * ==================================================================== 71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Licensed to the Apache Software Foundation (ASF) under one 81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * or more contributor license agreements. See the NOTICE file 91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * distributed with this work for additional information 101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * regarding copyright ownership. The ASF licenses this file 111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * to you under the Apache License, Version 2.0 (the 121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * "License"); you may not use this file except in compliance 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * with the License. You may obtain a copy of the License at 141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * http://www.apache.org/licenses/LICENSE-2.0 161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * Unless required by applicable law or agreed to in writing, 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * software distributed under the License is distributed on an 191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 2046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) * KIND, either express or implied. See the License for the 211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * specific language governing permissions and limitations 221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * under the License. 231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * ==================================================================== 241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * This software consists of voluntary contributions made by many 261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * individuals on behalf of the Apache Software Foundation. For more 271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * information on the Apache Software Foundation, please see 281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * <http://www.apache.org/>. 291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)package org.apache.http; 331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import java.io.Serializable; 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import org.apache.http.util.CharArrayBuffer; 361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/** 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Represents a protocol version, as specified in RFC 2616. 401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * RFC 2616 specifies only HTTP versions, like "HTTP/1.1" and "HTTP/1.0". 411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * RFC 3261 specifies a message format that is identical to HTTP except 421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * for the protocol name. It defines a protocol version "SIP/2.0". 431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * There are some nitty-gritty differences between the interpretation 441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * of versions in HTTP and SIP. In those cases, HTTP takes precedence. 451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * <p> 461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * This class defines a protocol version as a combination of 471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * protocol name, major version number, and minor version number. 481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Note that {@link #equals} and {@link #hashCode} are defined as 491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * final here, they cannot be overridden in derived classes. 501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @author <a href="mailto:rolandw at apache.org">Roland Weber</a> 531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @version $Revision: 609106 $ 551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @deprecated Please use {@link java.net.URL#openConnection} instead. 57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * for further details. 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */ 601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)@Deprecated 611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)public class ProtocolVersion implements Serializable, Cloneable { 621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) private static final long serialVersionUID = 8950662842175091068L; 641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** Name of the protocol. */ 671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) protected final String protocol; 681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** Major version number of the protocol */ 701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) protected final int major; 711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** Minor version number of the protocol */ 731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) protected final int minor; 741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Create a protocol version designator. 781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @param protocol the name of the protocol, for example "HTTP" 801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @param major the major version number of the protocol 811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @param minor the minor version number of the protocol 821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public ProtocolVersion(String protocol, int major, int minor) { 840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch if (protocol == null) { 851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) throw new IllegalArgumentException 861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) ("Protocol name must not be null."); 871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (major < 0) { 891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) throw new IllegalArgumentException 901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) ("Protocol major version number must not be negative."); 911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (minor < 0) { 931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) throw new IllegalArgumentException 9446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) ("Protocol minor version number may not be negative"); 951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) this.protocol = protocol; 971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) this.major = major; 981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) this.minor = minor; 991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Returns the name of the protocol. 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * @return the protocol name 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */ 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public final String getProtocol() { 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return protocol; 1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /** 111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch * Returns the major version number of the protocol. 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 113effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch * @return the major version number. 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */ 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public final int getMajor() { 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return major; 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /** 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Returns the minor version number of the HTTP protocol. 1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * @return the minor version number. 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */ 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public final int getMinor() { 1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return minor; 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /** 1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Obtains a specific version of this protocol. 1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * This can be used by derived classes to instantiate themselves instead 1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * of the base class, and to define constants for commonly used versions. 1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * <br/> 1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * The default implementation in this class returns <code>this</code> 1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * if the version matches, and creates a new {@link ProtocolVersion} 1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * otherwise. 1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * @param major the major version 1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * @param minor the minor version 1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * @return a protocol version with the same protocol name 1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * and the argument version 1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */ 1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public ProtocolVersion forVersion(int major, int minor) { 1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if ((major == this.major) && (minor == this.minor)) { 1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return this; 1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // argument checking is done in the constructor 1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return new ProtocolVersion(this.protocol, major, minor); 1521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /** 1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Obtains a hash code consistent with {@link #equals}. 1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * @return the hashcode of this protocol version 1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */ 1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public final int hashCode() { 1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return this.protocol.hashCode() ^ (this.major * 100000) ^ this.minor; 1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /** 1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Checks equality of this protocol version with an object. 1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * The object is equal if it is a protocl version with the same 1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * protocol name, major version number, and minor version number. 1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * The specific class of the object is <i>not</i> relevant, 1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * instances of derived classes with identical attributes are 1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * equal to instances of the base class and vice versa. 1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * @param obj the object to compare with 1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 1751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @return <code>true</code> if the argument is the same protocol version, 1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * <code>false</code> otherwise 1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */ 1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public final boolean equals(Object obj) { 1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (this == obj) { 1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return true; 1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 182effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch if (!(obj instanceof ProtocolVersion)) { 1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return false; 1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ProtocolVersion that = (ProtocolVersion) obj; 1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return ((this.protocol.equals(that.protocol)) && 1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) (this.major == that.major) && 1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) (this.minor == that.minor)); 1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /** 1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Checks whether this protocol can be compared to another one. 1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Only protocol versions with the same protocol name can be 1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * {@link #compareToVersion compared}. 1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * @param that the protocol version to consider 1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * @return <code>true</code> if {@link #compareToVersion compareToVersion} 2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * can be called with the argument, <code>false</code> otherwise 2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */ 2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public boolean isComparable(ProtocolVersion that) { 2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return (that != null) && this.protocol.equals(that.protocol); 2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 208010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) /** 209effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch * Compares this protocol version with another one. 2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Only protocol versions with the same protocol name can be compared. 2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * This method does <i>not</i> define a total ordering, as it would be 2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * required for {@link java.lang.Comparable}. 2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * @param that the protocl version to compare with 2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * @return a negative integer, zero, or a positive integer 2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * as this version is less than, equal to, or greater than 2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * the argument version. 219effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch * 2201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @throws IllegalArgumentException 2211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * if the argument has a different protocol name than this object, 2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * or if the argument is <code>null</code> 2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */ 2241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public int compareToVersion(ProtocolVersion that) { 2251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (that == null) { 2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) throw new IllegalArgumentException 2271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) ("Protocol version must not be null."); 2281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 2291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (!this.protocol.equals(that.protocol)) { 2301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) throw new IllegalArgumentException 2311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) ("Versions for different protocols cannot be compared. " + 2321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) this + " " + that); 2331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 2341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 2351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) int delta = getMajor() - that.getMajor(); 2361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (delta == 0) { 237010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) delta = getMinor() - that.getMinor(); 2381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 2391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return delta; 2401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /** 2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Tests if this protocol version is greater or equal to the given one. 2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 2461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @param version the version against which to check this version 2471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 2481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @return <code>true</code> if this protocol version is 2491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * {@link #isComparable comparable} to the argument 2501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * and {@link #compareToVersion compares} as greater or equal, 2511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * <code>false</code> otherwise 252010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) */ 253effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch public final boolean greaterEquals(ProtocolVersion version) { 2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return isComparable(version) && (compareToVersion(version) >= 0); 2551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 2561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 2571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 2581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 2591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Tests if this protocol version is less or equal to the given one. 2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 2611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @param version the version against which to check this version 2621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 2631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @return <code>true</code> if this protocol version is 2641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * {@link #isComparable comparable} to the argument 265effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch * and {@link #compareToVersion compares} as less or equal, 2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * <code>false</code> otherwise 2671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 2681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public final boolean lessEquals(ProtocolVersion version) { 2691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return isComparable(version) && (compareToVersion(version) <= 0); 2701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 2711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 2721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 2731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) /** 2741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * Converts this protocol version to a string. 2751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * 2761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) * @return a protocol version string, like "HTTP/1.1" 2771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) */ 2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public String toString() { 2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) CharArrayBuffer buffer = new CharArrayBuffer(16); 2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) buffer.append(this.protocol); 2811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) buffer.append('/'); 2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) buffer.append(Integer.toString(this.major)); 2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) buffer.append('.'); 2841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) buffer.append(Integer.toString(this.minor)); 2851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return buffer.toString(); 2861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public Object clone() throws CloneNotSupportedException { 289effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch return super.clone(); 2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} 2931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)