1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/ProtocolVersion.java $
3 * $Revision: 609106 $
4 * $Date: 2008-01-05 01:15:42 -0800 (Sat, 05 Jan 2008) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31
32package org.apache.http;
33
34import java.io.Serializable;
35import org.apache.http.util.CharArrayBuffer;
36
37
38/**
39 * Represents a protocol version, as specified in RFC 2616.
40 * RFC 2616 specifies only HTTP versions, like "HTTP/1.1" and "HTTP/1.0".
41 * RFC 3261 specifies a message format that is identical to HTTP except
42 * for the protocol name. It defines a protocol version "SIP/2.0".
43 * There are some nitty-gritty differences between the interpretation
44 * of versions in HTTP and SIP. In those cases, HTTP takes precedence.
45 * <p>
46 * This class defines a protocol version as a combination of
47 * protocol name, major version number, and minor version number.
48 * Note that {@link #equals} and {@link #hashCode} are defined as
49 * final here, they cannot be overridden in derived classes.
50 *
51 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
52 * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
53 *
54 * @version $Revision: 609106 $
55 *
56 * @deprecated Please use {@link java.net.URL#openConnection} instead.
57 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
58 *     for further details.
59 */
60@Deprecated
61public class ProtocolVersion implements Serializable, Cloneable {
62
63    private static final long serialVersionUID = 8950662842175091068L;
64
65
66    /** Name of the protocol. */
67    protected final String protocol;
68
69    /** Major version number of the protocol */
70    protected final int major;
71
72    /** Minor version number of the protocol */
73    protected final int minor;
74
75
76    /**
77     * Create a protocol version designator.
78     *
79     * @param protocol   the name of the protocol, for example "HTTP"
80     * @param major      the major version number of the protocol
81     * @param minor      the minor version number of the protocol
82     */
83    public ProtocolVersion(String protocol, int major, int minor) {
84        if (protocol == null) {
85            throw new IllegalArgumentException
86                ("Protocol name must not be null.");
87        }
88        if (major < 0) {
89            throw new IllegalArgumentException
90                ("Protocol major version number must not be negative.");
91        }
92        if (minor < 0) {
93            throw new IllegalArgumentException
94                ("Protocol minor version number may not be negative");
95        }
96        this.protocol = protocol;
97        this.major = major;
98        this.minor = minor;
99    }
100
101    /**
102     * Returns the name of the protocol.
103     *
104     * @return the protocol name
105     */
106    public final String getProtocol() {
107        return protocol;
108    }
109
110    /**
111     * Returns the major version number of the protocol.
112     *
113     * @return the major version number.
114     */
115    public final int getMajor() {
116        return major;
117    }
118
119    /**
120     * Returns the minor version number of the HTTP protocol.
121     *
122     * @return the minor version number.
123     */
124    public final int getMinor() {
125        return minor;
126    }
127
128
129    /**
130     * Obtains a specific version of this protocol.
131     * This can be used by derived classes to instantiate themselves instead
132     * of the base class, and to define constants for commonly used versions.
133     * <br/>
134     * The default implementation in this class returns <code>this</code>
135     * if the version matches, and creates a new {@link ProtocolVersion}
136     * otherwise.
137     *
138     * @param major     the major version
139     * @param minor     the minor version
140     *
141     * @return  a protocol version with the same protocol name
142     *          and the argument version
143     */
144    public ProtocolVersion forVersion(int major, int minor) {
145
146        if ((major == this.major) && (minor == this.minor)) {
147            return this;
148        }
149
150        // argument checking is done in the constructor
151        return new ProtocolVersion(this.protocol, major, minor);
152    }
153
154
155    /**
156     * Obtains a hash code consistent with {@link #equals}.
157     *
158     * @return  the hashcode of this protocol version
159     */
160    public final int hashCode() {
161        return this.protocol.hashCode() ^ (this.major * 100000) ^ this.minor;
162    }
163
164
165    /**
166     * Checks equality of this protocol version with an object.
167     * The object is equal if it is a protocl version with the same
168     * protocol name, major version number, and minor version number.
169     * The specific class of the object is <i>not</i> relevant,
170     * instances of derived classes with identical attributes are
171     * equal to instances of the base class and vice versa.
172     *
173     * @param obj       the object to compare with
174     *
175     * @return  <code>true</code> if the argument is the same protocol version,
176     *          <code>false</code> otherwise
177     */
178    public final boolean equals(Object obj) {
179        if (this == obj) {
180            return true;
181        }
182        if (!(obj instanceof ProtocolVersion)) {
183            return false;
184        }
185        ProtocolVersion that = (ProtocolVersion) obj;
186
187        return ((this.protocol.equals(that.protocol)) &&
188                (this.major == that.major) &&
189                (this.minor == that.minor));
190    }
191
192
193    /**
194     * Checks whether this protocol can be compared to another one.
195     * Only protocol versions with the same protocol name can be
196     * {@link #compareToVersion compared}.
197     *
198     * @param that      the protocol version to consider
199     *
200     * @return  <code>true</code> if {@link #compareToVersion compareToVersion}
201     *          can be called with the argument, <code>false</code> otherwise
202     */
203    public boolean isComparable(ProtocolVersion that) {
204        return (that != null) && this.protocol.equals(that.protocol);
205    }
206
207
208    /**
209     * Compares this protocol version with another one.
210     * Only protocol versions with the same protocol name can be compared.
211     * This method does <i>not</i> define a total ordering, as it would be
212     * required for {@link java.lang.Comparable}.
213     *
214     * @param that      the protocl version to compare with
215     *
216     * @return   a negative integer, zero, or a positive integer
217     *           as this version is less than, equal to, or greater than
218     *           the argument version.
219     *
220     * @throws IllegalArgumentException
221     *         if the argument has a different protocol name than this object,
222     *         or if the argument is <code>null</code>
223     */
224    public int compareToVersion(ProtocolVersion that) {
225        if (that == null) {
226            throw new IllegalArgumentException
227                ("Protocol version must not be null.");
228        }
229        if (!this.protocol.equals(that.protocol)) {
230            throw new IllegalArgumentException
231                ("Versions for different protocols cannot be compared. " +
232                 this + " " + that);
233        }
234
235        int delta = getMajor() - that.getMajor();
236        if (delta == 0) {
237            delta = getMinor() - that.getMinor();
238        }
239        return delta;
240    }
241
242
243    /**
244     * Tests if this protocol version is greater or equal to the given one.
245     *
246     * @param version   the version against which to check this version
247     *
248     * @return  <code>true</code> if this protocol version is
249     *          {@link #isComparable comparable} to the argument
250     *          and {@link #compareToVersion compares} as greater or equal,
251     *          <code>false</code> otherwise
252     */
253    public final boolean greaterEquals(ProtocolVersion version) {
254        return isComparable(version) && (compareToVersion(version) >= 0);
255    }
256
257
258    /**
259     * Tests if this protocol version is less or equal to the given one.
260     *
261     * @param version   the version against which to check this version
262     *
263     * @return  <code>true</code> if this protocol version is
264     *          {@link #isComparable comparable} to the argument
265     *          and {@link #compareToVersion compares} as less or equal,
266     *          <code>false</code> otherwise
267     */
268    public final boolean lessEquals(ProtocolVersion version) {
269        return isComparable(version) && (compareToVersion(version) <= 0);
270    }
271
272
273    /**
274     * Converts this protocol version to a string.
275     *
276     * @return  a protocol version string, like "HTTP/1.1"
277     */
278    public String toString() {
279        CharArrayBuffer buffer = new CharArrayBuffer(16);
280        buffer.append(this.protocol);
281        buffer.append('/');
282        buffer.append(Integer.toString(this.major));
283        buffer.append('.');
284        buffer.append(Integer.toString(this.minor));
285        return buffer.toString();
286    }
287
288    public Object clone() throws CloneNotSupportedException {
289        return super.clone();
290    }
291
292}
293