FileDocCategorySizeDatePackage
ProtocolVersion.javaAPI DocAndroid 1.5 API9889Wed May 06 22:41:10 BST 2009org.apache.http

ProtocolVersion

public class ProtocolVersion extends Object implements Serializable, Cloneable
Represents a protocol version, as specified in RFC 2616. RFC 2616 specifies only HTTP versions, like "HTTP/1.1" and "HTTP/1.0". RFC 3261 specifies a message format that is identical to HTTP except for the protocol name. It defines a protocol version "SIP/2.0". There are some nitty-gritty differences between the interpretation of versions in HTTP and SIP. In those cases, HTTP takes precedence.

This class defines a protocol version as a combination of protocol name, major version number, and minor version number. Note that {@link #equals} and {@link #hashCode} are defined as final here, they cannot be overridden in derived classes.

author
Oleg Kalnichevski
author
Roland Weber
version
$Revision: 609106 $

Fields Summary
private static final long
serialVersionUID
protected final String
protocol
Name of the protocol.
protected final int
major
Major version number of the protocol
protected final int
minor
Minor version number of the protocol
Constructors Summary
public ProtocolVersion(String protocol, int major, int minor)
Create a protocol version designator.

param
protocol the name of the protocol, for example "HTTP"
param
major the major version number of the protocol
param
minor the minor version number of the protocol


    
                                                      
           
        if (protocol == null) {
            throw new IllegalArgumentException
                ("Protocol name must not be null.");
        }
        if (major < 0) {
            throw new IllegalArgumentException
                ("Protocol major version number must not be negative.");
        }
        if (minor < 0) {
            throw new IllegalArgumentException
                ("Protocol minor version number may not be negative");
        }
        this.protocol = protocol;
        this.major = major;
        this.minor = minor;
    
Methods Summary
public java.lang.Objectclone()

        return super.clone();
    
public intcompareToVersion(org.apache.http.ProtocolVersion that)
Compares this protocol version with another one. Only protocol versions with the same protocol name can be compared. This method does not define a total ordering, as it would be required for {@link java.lang.Comparable}.

param
that the protocl version to compare with
return
a negative integer, zero, or a positive integer as this version is less than, equal to, or greater than the argument version.
throws
IllegalArgumentException if the argument has a different protocol name than this object, or if the argument is null

        if (that == null) {
            throw new IllegalArgumentException
                ("Protocol version must not be null."); 
        }
        if (!this.protocol.equals(that.protocol)) {
            throw new IllegalArgumentException
                ("Versions for different protocols cannot be compared. " +
                 this + " " + that);
        }

        int delta = getMajor() - that.getMajor();
        if (delta == 0) {
            delta = getMinor() - that.getMinor();
        }
        return delta;
    
public final booleanequals(java.lang.Object obj)
Checks equality of this protocol version with an object. The object is equal if it is a protocl version with the same protocol name, major version number, and minor version number. The specific class of the object is not relevant, instances of derived classes with identical attributes are equal to instances of the base class and vice versa.

param
obj the object to compare with
return
true if the argument is the same protocol version, false otherwise

        if (this == obj) {
            return true;
        }
        if (!(obj instanceof ProtocolVersion)) {
            return false;
        }
        ProtocolVersion that = (ProtocolVersion) obj;

        return ((this.protocol.equals(that.protocol)) &&
                (this.major == that.major) &&
                (this.minor == that.minor));
    
public org.apache.http.ProtocolVersionforVersion(int major, int minor)
Obtains a specific version of this protocol. This can be used by derived classes to instantiate themselves instead of the base class, and to define constants for commonly used versions.
The default implementation in this class returns this if the version matches, and creates a new {@link ProtocolVersion} otherwise.

param
major the major version
param
minor the minor version
return
a protocol version with the same protocol name and the argument version


        if ((major == this.major) && (minor == this.minor)) {
            return this;
        }

        // argument checking is done in the constructor
        return new ProtocolVersion(this.protocol, major, minor);
    
public final intgetMajor()
Returns the major version number of the protocol.

return
the major version number.

        return major;
    
public final intgetMinor()
Returns the minor version number of the HTTP protocol.

return
the minor version number.

        return minor;
    
public final java.lang.StringgetProtocol()
Returns the name of the protocol.

return
the protocol name

        return protocol;
    
public final booleangreaterEquals(org.apache.http.ProtocolVersion version)
Tests if this protocol version is greater or equal to the given one.

param
version the version against which to check this version
return
true if this protocol version is {@link #isComparable comparable} to the argument and {@link #compareToVersion compares} as greater or equal, false otherwise

        return isComparable(version) && (compareToVersion(version) >= 0);
    
public final inthashCode()
Obtains a hash code consistent with {@link #equals}.

return
the hashcode of this protocol version

        return this.protocol.hashCode() ^ (this.major * 100000) ^ this.minor;
    
public booleanisComparable(org.apache.http.ProtocolVersion that)
Checks whether this protocol can be compared to another one. Only protocol versions with the same protocol name can be {@link #compareToVersion compared}.

param
that the protocol version to consider
return
true if {@link #compareToVersion compareToVersion} can be called with the argument, false otherwise

        return (that != null) && this.protocol.equals(that.protocol);
    
public final booleanlessEquals(org.apache.http.ProtocolVersion version)
Tests if this protocol version is less or equal to the given one.

param
version the version against which to check this version
return
true if this protocol version is {@link #isComparable comparable} to the argument and {@link #compareToVersion compares} as less or equal, false otherwise

        return isComparable(version) && (compareToVersion(version) <= 0);
    
public java.lang.StringtoString()
Converts this protocol version to a string.

return
a protocol version string, like "HTTP/1.1"

        CharArrayBuffer buffer = new CharArrayBuffer(16);
        buffer.append(this.protocol); 
        buffer.append('/"); 
        buffer.append(Integer.toString(this.major)); 
        buffer.append('."); 
        buffer.append(Integer.toString(this.minor)); 
        return buffer.toString();