001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.version;
020
021import static java.util.Objects.requireNonNull;
022
023/**
024 * A range of versions.
025 */
026public interface VersionRange {
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    final class Bound {
056
057        private final Version version;
058
059        private final boolean inclusive;
060
061        /**
062         * Creates a new bound with the specified properties.
063         *
064         * @param version The bounding version, must not be {@code null}.
065         * @param inclusive A flag whether the specified version is included in the range or not.
066         */
067        public Bound(Version version, boolean inclusive) {
068            this.version = requireNonNull(version, "version cannot be null");
069            this.inclusive = inclusive;
070        }
071
072        /**
073         * Gets the bounding version.
074         *
075         * @return The bounding version, never {@code null}.
076         */
077        public Version getVersion() {
078            return version;
079        }
080
081        /**
082         * Indicates whether the bounding version is included in the range or not.
083         *
084         * @return {@code true} if the bounding version is included in the range, {@code false} if not.
085         */
086        public boolean isInclusive() {
087            return inclusive;
088        }
089
090        @Override
091        public boolean equals(Object obj) {
092            if (obj == this) {
093                return true;
094            } else if (obj == null || !getClass().equals(obj.getClass())) {
095                return false;
096            }
097
098            Bound that = (Bound) obj;
099            return inclusive == that.inclusive && version.equals(that.version);
100        }
101
102        @Override
103        public int hashCode() {
104            int hash = 17;
105            hash = hash * 31 + version.hashCode();
106            hash = hash * 31 + (inclusive ? 1 : 0);
107            return hash;
108        }
109
110        @Override
111        public String toString() {
112            return String.valueOf(version);
113        }
114    }
115}