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