001package org.apache.maven.artifact.versioning;
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 * Describes a restriction in versioning.
024 *
025 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
026 */
027public class Restriction
028{
029    private final ArtifactVersion lowerBound;
030
031    private final boolean lowerBoundInclusive;
032
033    private final ArtifactVersion upperBound;
034
035    private final boolean upperBoundInclusive;
036
037    public static final Restriction EVERYTHING = new Restriction( null, false, null, false );
038
039    public Restriction( ArtifactVersion lowerBound, boolean lowerBoundInclusive, ArtifactVersion upperBound,
040                        boolean upperBoundInclusive )
041    {
042        this.lowerBound = lowerBound;
043        this.lowerBoundInclusive = lowerBoundInclusive;
044        this.upperBound = upperBound;
045        this.upperBoundInclusive = upperBoundInclusive;
046    }
047
048    public ArtifactVersion getLowerBound()
049    {
050        return lowerBound;
051    }
052
053    public boolean isLowerBoundInclusive()
054    {
055        return lowerBoundInclusive;
056    }
057
058    public ArtifactVersion getUpperBound()
059    {
060        return upperBound;
061    }
062
063    public boolean isUpperBoundInclusive()
064    {
065        return upperBoundInclusive;
066    }
067
068    public boolean containsVersion( ArtifactVersion version )
069    {
070        if ( lowerBound != null )
071        {
072            int comparison = lowerBound.compareTo( version );
073
074            if ( ( comparison == 0 ) && !lowerBoundInclusive )
075            {
076                return false;
077            }
078            if ( comparison > 0 )
079            {
080                return false;
081            }
082        }
083        if ( upperBound != null )
084        {
085            int comparison = upperBound.compareTo( version );
086
087            if ( ( comparison == 0 ) && !upperBoundInclusive )
088            {
089                return false;
090            }
091            if ( comparison < 0 )
092            {
093                return false;
094            }
095        }
096
097        return true;
098    }
099
100    @Override
101    public int hashCode()
102    {
103        int result = 13;
104
105        if ( lowerBound == null )
106        {
107            result += 1;
108        }
109        else
110        {
111            result += lowerBound.hashCode();
112        }
113
114        result *= lowerBoundInclusive ? 1 : 2;
115
116        if ( upperBound == null )
117        {
118            result -= 3;
119        }
120        else
121        {
122            result -= upperBound.hashCode();
123        }
124
125        result *= upperBoundInclusive ? 2 : 3;
126
127        return result;
128    }
129
130    @Override
131    public boolean equals( Object other )
132    {
133        if ( this == other )
134        {
135            return true;
136        }
137
138        if ( !( other instanceof Restriction ) )
139        {
140            return false;
141        }
142
143        Restriction restriction = (Restriction) other;
144        if ( lowerBound != null )
145        {
146            if ( !lowerBound.equals( restriction.lowerBound ) )
147            {
148                return false;
149            }
150        }
151        else if ( restriction.lowerBound != null )
152        {
153            return false;
154        }
155
156        if ( lowerBoundInclusive != restriction.lowerBoundInclusive )
157        {
158            return false;
159        }
160
161        if ( upperBound != null )
162        {
163            if ( !upperBound.equals( restriction.upperBound ) )
164            {
165                return false;
166            }
167        }
168        else if ( restriction.upperBound != null )
169        {
170            return false;
171        }
172
173        return upperBoundInclusive == restriction.upperBoundInclusive;
174
175    }
176
177    public String toString()
178    {
179        StringBuilder buf = new StringBuilder();
180
181        buf.append( isLowerBoundInclusive() ? "[" : "(" );
182        if ( getLowerBound() != null )
183        {
184            buf.append( getLowerBound().toString() );
185        }
186        buf.append( "," );
187        if ( getUpperBound() != null )
188        {
189            buf.append( getUpperBound().toString() );
190        }
191        buf.append( isUpperBoundInclusive() ? "]" : ")" );
192
193        return buf.toString();
194    }
195}