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
022/**
023 * A range of versions.
024 */
025public interface VersionRange
026{
027
028    /**
029     * Determines whether the specified version is contained within this range.
030     * 
031     * @param version The version to test, must not be {@code null}.
032     * @return {@code true} if this range contains the specified version, {@code false} otherwise.
033     */
034    boolean containsVersion( Version version );
035
036    /**
037     * Gets a lower bound (if any) for this range. If existent, this range does not contain any version smaller than its
038     * lower bound. Note that complex version ranges might exclude some versions even within their bounds.
039     * 
040     * @return A lower bound for this range or {@code null} is there is none.
041     */
042    Bound getLowerBound();
043
044    /**
045     * Gets an upper bound (if any) for this range. If existent, this range does not contain any version greater than
046     * its upper bound. Note that complex version ranges might exclude some versions even within their bounds.
047     * 
048     * @return An upper bound for this range or {@code null} is there is none.
049     */
050    Bound getUpperBound();
051
052    /**
053     * A bound of a version range.
054     */
055    static final class Bound
056    {
057
058        private final Version version;
059
060        private final boolean inclusive;
061
062        /**
063         * Creates a new bound with the specified properties.
064         * 
065         * @param version The bounding version, must not be {@code null}.
066         * @param inclusive A flag whether the specified version is included in the range or not.
067         */
068        public Bound( Version version, boolean inclusive )
069        {
070            if ( version == null )
071            {
072                throw new IllegalArgumentException( "version missing" );
073            }
074            this.version = version;
075            this.inclusive = inclusive;
076        }
077
078        /**
079         * Gets the bounding version.
080         * 
081         * @return The bounding version, never {@code null}.
082         */
083        public Version getVersion()
084        {
085            return version;
086        }
087
088        /**
089         * Indicates whether the bounding version is included in the range or not.
090         * 
091         * @return {@code true} if the bounding version is included in the range, {@code false} if not.
092         */
093        public boolean isInclusive()
094        {
095            return inclusive;
096        }
097
098        @Override
099        public boolean equals( Object obj )
100        {
101            if ( obj == this )
102            {
103                return true;
104            }
105            else if ( obj == null || !getClass().equals( obj.getClass() ) )
106            {
107                return false;
108            }
109
110            Bound that = (Bound) obj;
111            return inclusive == that.inclusive && version.equals( that.version );
112        }
113
114        @Override
115        public int hashCode()
116        {
117            int hash = 17;
118            hash = hash * 31 + version.hashCode();
119            hash = hash * 31 + ( inclusive ? 1 : 0 );
120            return hash;
121        }
122
123        @Override
124        public String toString()
125        {
126            return String.valueOf( version );
127        }
128
129    }
130
131}