001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http;
029
030import java.io.Serializable;
031
032import org.apache.http.annotation.ThreadingBehavior;
033import org.apache.http.annotation.Contract;
034import org.apache.http.util.Args;
035
036/**
037 * Represents a protocol version. The "major.minor" numbering
038 * scheme is used to indicate versions of the protocol.
039 * <p>
040 * This class defines a protocol version as a combination of
041 * protocol name, major version number, and minor version number.
042 * Note that {@link #equals} and {@link #hashCode} are defined as
043 * final here, they cannot be overridden in derived classes.
044 * </p>
045 *
046 * @since 4.0
047 */
048@Contract(threading = ThreadingBehavior.IMMUTABLE)
049public class ProtocolVersion implements Serializable, Cloneable {
050
051    private static final long serialVersionUID = 8950662842175091068L;
052
053
054    /** Name of the protocol. */
055    protected final String protocol;
056
057    /** Major version number of the protocol */
058    protected final int major;
059
060    /** Minor version number of the protocol */
061    protected final int minor;
062
063
064    /**
065     * Create a protocol version designator.
066     *
067     * @param protocol   the name of the protocol, for example "HTTP"
068     * @param major      the major version number of the protocol
069     * @param minor      the minor version number of the protocol
070     */
071    public ProtocolVersion(final String protocol, final int major, final int minor) {
072        this.protocol = Args.notNull(protocol, "Protocol name");
073        this.major = Args.notNegative(major, "Protocol minor version");
074        this.minor = Args.notNegative(minor, "Protocol minor version");
075    }
076
077    /**
078     * Returns the name of the protocol.
079     *
080     * @return the protocol name
081     */
082    public final String getProtocol() {
083        return protocol;
084    }
085
086    /**
087     * Returns the major version number of the protocol.
088     *
089     * @return the major version number.
090     */
091    public final int getMajor() {
092        return major;
093    }
094
095    /**
096     * Returns the minor version number of the HTTP protocol.
097     *
098     * @return the minor version number.
099     */
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 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}