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