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.util.version;
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 * <p>
032 * Despite its name, this class is generic in a sense it works with any {@link Version}.
033 */
034public final class GenericVersionConstraint implements VersionConstraint {
035
036    private final VersionRange range;
037
038    private final Version version;
039
040    /**
041     * Creates a version constraint from the specified version range.
042     *
043     * @param range the version range, must not be {@code null}
044     */
045    GenericVersionConstraint(VersionRange range) {
046        this.range = requireNonNull(range, "version range cannot be null");
047        this.version = null;
048    }
049
050    /**
051     * Creates a version constraint from the specified version.
052     *
053     * @param version the version, must not be {@code null}
054     */
055    GenericVersionConstraint(Version version) {
056        this.version = requireNonNull(version, "version cannot be null");
057        this.range = null;
058    }
059
060    @Override
061    public VersionRange getRange() {
062        return range;
063    }
064
065    @Override
066    public Version getVersion() {
067        return version;
068    }
069
070    @Override
071    public boolean containsVersion(Version version) {
072        if (range == null) {
073            return version.equals(this.version);
074        } else {
075            return range.containsVersion(version);
076        }
077    }
078
079    @Override
080    public String toString() {
081        return String.valueOf((range == null) ? version : range);
082    }
083
084    @Override
085    public boolean equals(Object obj) {
086        if (this == obj) {
087            return true;
088        }
089        if (obj == null || !getClass().equals(obj.getClass())) {
090            return false;
091        }
092
093        VersionConstraint that = (VersionConstraint) obj;
094
095        return Objects.equals(range, that.getRange()) && Objects.equals(version, that.getVersion());
096    }
097
098    @Override
099    public int hashCode() {
100        int hash = 17;
101        hash = hash * 31 + hash(getRange());
102        hash = hash * 31 + hash(getVersion());
103        return hash;
104    }
105
106    private static int hash(Object obj) {
107        return obj != null ? obj.hashCode() : 0;
108    }
109}