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 org.apache.http.annotation.ThreadingBehavior;
031import org.apache.http.annotation.Contract;
032
033import java.io.Serializable;
034
035/**
036 * Represents an HTTP version. HTTP uses a "major.minor" numbering
037 * scheme to indicate versions of the protocol.
038 * <p>
039 * The version of an HTTP message is indicated by an HTTP-Version field
040 * in the first line of the message.
041 * </p>
042 * <pre>
043 *     HTTP-Version   = "HTTP" "/" 1*DIGIT "." 1*DIGIT
044 * </pre>
045 *
046 * @since 4.0
047 */
048@Contract(threading = ThreadingBehavior.IMMUTABLE)
049public final class HttpVersion extends ProtocolVersion implements Serializable {
050
051    private static final long serialVersionUID = -5856653513894415344L;
052
053    /** The protocol name. */
054    public static final String HTTP = "HTTP";
055
056    /** HTTP protocol version 0.9 */
057    public static final HttpVersion HTTP_0_9 = new HttpVersion(0, 9);
058
059    /** HTTP protocol version 1.0 */
060    public static final HttpVersion HTTP_1_0 = new HttpVersion(1, 0);
061
062    /** HTTP protocol version 1.1 */
063    public static final HttpVersion HTTP_1_1 = new HttpVersion(1, 1);
064
065
066    /**
067     * Create an HTTP protocol version designator.
068     *
069     * @param major   the major version number of the HTTP protocol
070     * @param minor   the minor version number of the HTTP protocol
071     *
072     * @throws IllegalArgumentException if either major or minor version number is negative
073     */
074    public HttpVersion(final int major, final int minor) {
075        super(HTTP, major, minor);
076    }
077
078
079    /**
080     * Obtains a specific HTTP version.
081     *
082     * @param major     the major version
083     * @param minor     the minor version
084     *
085     * @return  an instance of {@link HttpVersion} with the argument version
086     */
087    @Override
088    public ProtocolVersion forVersion(final int major, final int minor) {
089
090        if ((major == this.major) && (minor == this.minor)) {
091            return this;
092        }
093
094        if (major == 1) {
095            if (minor == 0) {
096                return HTTP_1_0;
097            }
098            if (minor == 1) {
099                return HTTP_1_1;
100            }
101        }
102        if ((major == 0) && (minor == 9)) {
103            return HTTP_0_9;
104        }
105
106        // argument checking is done in the constructor
107        return new HttpVersion(major, minor);
108    }
109
110}