001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/ProtocolVersion.java $
003: * $Revision: 609106 $
004: * $Date: 2008-01-05 10:15:42 +0100 (Sat, 05 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http;
033:
034: import java.io.Serializable;
035: import org.apache.http.util.CharArrayBuffer;
036:
037: /**
038: * Represents a protocol version, as specified in RFC 2616.
039: * RFC 2616 specifies only HTTP versions, like "HTTP/1.1" and "HTTP/1.0".
040: * RFC 3261 specifies a message format that is identical to HTTP except
041: * for the protocol name. It defines a protocol version "SIP/2.0".
042: * There are some nitty-gritty differences between the interpretation
043: * of versions in HTTP and SIP. In those cases, HTTP takes precedence.
044: * <p>
045: * This class defines a protocol version as a combination of
046: * protocol name, major version number, and minor version number.
047: * Note that {@link #equals} and {@link #hashCode} are defined as
048: * final here, they cannot be overridden in derived classes.
049: *
050: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
051: * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
052: *
053: * @version $Revision: 609106 $
054: */
055: public class ProtocolVersion implements Serializable, Cloneable {
056:
057: private static final long serialVersionUID = 8950662842175091068L;
058:
059: /** Name of the protocol. */
060: protected final String protocol;
061:
062: /** Major version number of the protocol */
063: protected final int major;
064:
065: /** Minor version number of the protocol */
066: protected final int minor;
067:
068: /**
069: * Create a protocol version designator.
070: *
071: * @param protocol the name of the protocol, for example "HTTP"
072: * @param major the major version number of the protocol
073: * @param minor the minor version number of the protocol
074: */
075: public ProtocolVersion(String protocol, int major, int minor) {
076: if (protocol == null) {
077: throw new IllegalArgumentException(
078: "Protocol name must not be null.");
079: }
080: if (major < 0) {
081: throw new IllegalArgumentException(
082: "Protocol major version number must not be negative.");
083: }
084: if (minor < 0) {
085: throw new IllegalArgumentException(
086: "Protocol minor version number may not be negative");
087: }
088: this .protocol = protocol;
089: this .major = major;
090: this .minor = minor;
091: }
092:
093: /**
094: * Returns the name of the protocol.
095: *
096: * @return the protocol name
097: */
098: public final String getProtocol() {
099: return protocol;
100: }
101:
102: /**
103: * Returns the major version number of the protocol.
104: *
105: * @return the major version number.
106: */
107: public final int getMajor() {
108: return major;
109: }
110:
111: /**
112: * Returns the minor version number of the HTTP protocol.
113: *
114: * @return the minor version number.
115: */
116: public final int getMinor() {
117: return minor;
118: }
119:
120: /**
121: * Obtains a specific version of this protocol.
122: * This can be used by derived classes to instantiate themselves instead
123: * of the base class, and to define constants for commonly used versions.
124: * <br/>
125: * The default implementation in this class returns <code>this</code>
126: * if the version matches, and creates a new {@link ProtocolVersion}
127: * otherwise.
128: *
129: * @param major the major version
130: * @param minor the minor version
131: *
132: * @return a protocol version with the same protocol name
133: * and the argument version
134: */
135: public ProtocolVersion forVersion(int major, int minor) {
136:
137: if ((major == this .major) && (minor == this .minor)) {
138: return this ;
139: }
140:
141: // argument checking is done in the constructor
142: return new ProtocolVersion(this .protocol, major, minor);
143: }
144:
145: /**
146: * Obtains a hash code consistent with {@link #equals}.
147: *
148: * @return the hashcode of this protocol version
149: */
150: public final int hashCode() {
151: return this .protocol.hashCode() ^ (this .major * 100000)
152: ^ this .minor;
153: }
154:
155: /**
156: * Checks equality of this protocol version with an object.
157: * The object is equal if it is a protocl version with the same
158: * protocol name, major version number, and minor version number.
159: * The specific class of the object is <i>not</i> relevant,
160: * instances of derived classes with identical attributes are
161: * equal to instances of the base class and vice versa.
162: *
163: * @param obj the object to compare with
164: *
165: * @return <code>true</code> if the argument is the same protocol version,
166: * <code>false</code> otherwise
167: */
168: public final boolean equals(Object obj) {
169: if (this == obj) {
170: return true;
171: }
172: if (!(obj instanceof ProtocolVersion)) {
173: return false;
174: }
175: ProtocolVersion that = (ProtocolVersion) obj;
176:
177: return ((this .protocol.equals(that.protocol))
178: && (this .major == that.major) && (this .minor == that.minor));
179: }
180:
181: /**
182: * Checks whether this protocol can be compared to another one.
183: * Only protocol versions with the same protocol name can be
184: * {@link #compareToVersion compared}.
185: *
186: * @param that the protocol version to consider
187: *
188: * @return <code>true</code> if {@link #compareToVersion compareToVersion}
189: * can be called with the argument, <code>false</code> otherwise
190: */
191: public boolean isComparable(ProtocolVersion that) {
192: return (that != null) && this .protocol.equals(that.protocol);
193: }
194:
195: /**
196: * Compares this protocol version with another one.
197: * Only protocol versions with the same protocol name can be compared.
198: * This method does <i>not</i> define a total ordering, as it would be
199: * required for {@link java.lang.Comparable}.
200: *
201: * @param that the protocl version to compare with
202: *
203: * @return a negative integer, zero, or a positive integer
204: * as this version is less than, equal to, or greater than
205: * the argument version.
206: *
207: * @throws IllegalArgumentException
208: * if the argument has a different protocol name than this object,
209: * or if the argument is <code>null</code>
210: */
211: public int compareToVersion(ProtocolVersion that) {
212: if (that == null) {
213: throw new IllegalArgumentException(
214: "Protocol version must not be null.");
215: }
216: if (!this .protocol.equals(that.protocol)) {
217: throw new IllegalArgumentException(
218: "Versions for different protocols cannot be compared. "
219: + this + " " + that);
220: }
221:
222: int delta = getMajor() - that.getMajor();
223: if (delta == 0) {
224: delta = getMinor() - that.getMinor();
225: }
226: return delta;
227: }
228:
229: /**
230: * Tests if this protocol version is greater or equal to the given one.
231: *
232: * @param version the version against which to check this version
233: *
234: * @return <code>true</code> if this protocol version is
235: * {@link #isComparable comparable} to the argument
236: * and {@link #compareToVersion compares} as greater or equal,
237: * <code>false</code> otherwise
238: */
239: public final boolean greaterEquals(ProtocolVersion version) {
240: return isComparable(version)
241: && (compareToVersion(version) >= 0);
242: }
243:
244: /**
245: * Tests if this protocol version is less or equal to the given one.
246: *
247: * @param version the version against which to check this version
248: *
249: * @return <code>true</code> if this protocol version is
250: * {@link #isComparable comparable} to the argument
251: * and {@link #compareToVersion compares} as less or equal,
252: * <code>false</code> otherwise
253: */
254: public final boolean lessEquals(ProtocolVersion version) {
255: return isComparable(version)
256: && (compareToVersion(version) <= 0);
257: }
258:
259: /**
260: * Converts this protocol version to a string.
261: *
262: * @return a protocol version string, like "HTTP/1.1"
263: */
264: public String toString() {
265: CharArrayBuffer buffer = new CharArrayBuffer(16);
266: buffer.append(this .protocol);
267: buffer.append('/');
268: buffer.append(Integer.toString(this .major));
269: buffer.append('.');
270: buffer.append(Integer.toString(this .minor));
271: return buffer.toString();
272: }
273:
274: public Object clone() throws CloneNotSupportedException {
275: return super.clone();
276: }
277:
278: }
|