001package org.eclipse.aether.version;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static java.util.Objects.requireNonNull;
023
024/**
025 * A range of versions.
026 */
027public interface VersionRange
028{
029
030    /**
031     * Determines whether the specified version is contained within this range.
032     * 
033     * @param version The version to test, must not be {@code null}.
034     * @return {@code true} if this range contains the specified version, {@code false} otherwise.
035     */
036    boolean containsVersion( Version version );
037
038    /**
039     * Gets a lower bound (if any) for this range. If existent, this range does not contain any version smaller than its
040     * lower bound. Note that complex version ranges might exclude some versions even within their bounds.
041     * 
042     * @return A lower bound for this range or {@code null} is there is none.
043     */
044    Bound getLowerBound();
045
046    /**
047     * Gets an upper bound (if any) for this range. If existent, this range does not contain any version greater than
048     * its upper bound. Note that complex version ranges might exclude some versions even within their bounds.
049     * 
050     * @return An upper bound for this range or {@code null} is there is none.
051     */
052    Bound getUpperBound();
053
054    /**
055     * A bound of a version range.
056     */
057    static final class Bound
058    {
059
060        private final Version version;
061
062        private final boolean inclusive;
063
064        /**
065         * Creates a new bound with the specified properties.
066         *
067         * @param version The bounding version, must not be {@code null}.
068         * @param inclusive A flag whether the specified version is included in the range or not.
069         */
070        public Bound( Version version, boolean inclusive )
071        {
072            this.version = requireNonNull( version, "version cannot be null" );
073            this.inclusive = inclusive;
074        }
075
076        /**
077         * Gets the bounding version.
078         *
079         * @return The bounding version, never {@code null}.
080         */
081        public Version getVersion()
082        {
083            return version;
084        }
085
086        /**
087         * Indicates whether the bounding version is included in the range or not.
088         * 
089         * @return {@code true} if the bounding version is included in the range, {@code false} if not.
090         */
091        public boolean isInclusive()
092        {
093            return inclusive;
094        }
095
096        @Override
097        public boolean equals( Object obj )
098        {
099            if ( obj == this )
100            {
101                return true;
102            }
103            else if ( obj == null || !getClass().equals( obj.getClass() ) )
104            {
105                return false;
106            }
107
108            Bound that = (Bound) obj;
109            return inclusive == that.inclusive && version.equals( that.version );
110        }
111
112        @Override
113        public int hashCode()
114        {
115            int hash = 17;
116            hash = hash * 31 + version.hashCode();
117            hash = hash * 31 + ( inclusive ? 1 : 0 );
118            return hash;
119        }
120
121        @Override
122        public String toString()
123        {
124            return String.valueOf( version );
125        }
126
127    }
128
129}