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.internal.test.util;
020
021import java.util.Objects;
022
023import org.eclipse.aether.version.Version;
024import org.eclipse.aether.version.VersionConstraint;
025import org.eclipse.aether.version.VersionRange;
026
027import static java.util.Objects.requireNonNull;
028
029/**
030 * A constraint on versions for a dependency.
031 */
032public final class TestVersionConstraint implements VersionConstraint {
033
034    private final VersionRange range;
035
036    private final Version version;
037
038    /**
039     * Creates a version constraint from the specified version range.
040     *
041     * @param range The version range, must not be {@code null}.
042     */
043    public TestVersionConstraint(VersionRange range) {
044        this.range = requireNonNull(range, "version range cannot be null");
045        this.version = null;
046    }
047
048    /**
049     * Creates a version constraint from the specified version.
050     *
051     * @param version The version, must not be {@code null}.
052     */
053    public TestVersionConstraint(Version version) {
054        this.version = requireNonNull(version, "version cannot be null");
055        this.range = null;
056    }
057
058    public VersionRange getRange() {
059        return range;
060    }
061
062    public Version getVersion() {
063        return version;
064    }
065
066    public boolean containsVersion(Version version) {
067        if (range == null) {
068            return version.equals(this.version);
069        } else {
070            return range.containsVersion(version);
071        }
072    }
073
074    @Override
075    public String toString() {
076        return String.valueOf((range == null) ? version : range);
077    }
078
079    @Override
080    public boolean equals(Object obj) {
081        if (this == obj) {
082            return true;
083        }
084        if (obj == null || !getClass().equals(obj.getClass())) {
085            return false;
086        }
087
088        TestVersionConstraint that = (TestVersionConstraint) obj;
089
090        return Objects.equals(range, that.range) && Objects.equals(version, that.getVersion());
091    }
092
093    @Override
094    public int hashCode() {
095        int hash = 17;
096        hash = hash * 31 + hash(getRange());
097        hash = hash * 31 + hash(getVersion());
098        return hash;
099    }
100
101    private static int hash(Object obj) {
102        return obj != null ? obj.hashCode() : 0;
103    }
104}