PR: MNG-505

get the intersection of ranges

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@219803 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-07-19 23:14:02 +00:00
parent 50eb5f8fdc
commit 8bf61afbd3
2 changed files with 321 additions and 6 deletions

View File

@ -181,14 +181,22 @@ public class VersionRange
List r1 = this.restrictions;
List r2 = restriction.restrictions;
List restrictions = Collections.EMPTY_LIST;
if ( !r1.isEmpty() || !r2.isEmpty() )
List restrictions;
if ( r1.isEmpty() )
{
// TODO: amalgamate
restrictions = new ArrayList( r1.size() + r2.size() );
restrictions.addAll( r1 );
restrictions.addAll( r2 );
restrictions = r2;
}
else if ( r2.isEmpty() )
{
restrictions = r1;
}
else
{
restrictions = intersection( r1, r2 );
}
if ( version != null && restrictions.size() > 0 )
{
boolean found = false;
for ( Iterator i = restrictions.iterator(); i.hasNext() && !found; )
{
@ -209,6 +217,155 @@ public class VersionRange
return new VersionRange( version, restrictions );
}
private List intersection( List r1, List r2 )
{
List restrictions = new ArrayList( Math.min( r1.size(), r2.size() ) );
Iterator i1 = r1.iterator();
Iterator i2 = r2.iterator();
Restriction res1 = (Restriction) i1.next();
Restriction res2 = (Restriction) i2.next();
boolean done = false;
while ( !done )
{
if ( res1.getLowerBound() == null || res2.getUpperBound() == null ||
res1.getLowerBound().compareTo( res2.getUpperBound() ) <= 0 )
{
if ( res1.getUpperBound() == null || res2.getLowerBound() == null ||
res1.getUpperBound().compareTo( res2.getLowerBound() ) >= 0 )
{
ArtifactVersion lower;
ArtifactVersion upper;
boolean lowerInclusive;
boolean upperInclusive;
// overlaps
if ( res1.getLowerBound() == null )
{
lower = res2.getLowerBound();
lowerInclusive = res2.isLowerBoundInclusive();
}
else if ( res2.getLowerBound() == null )
{
lower = res1.getLowerBound();
lowerInclusive = res1.isLowerBoundInclusive();
}
else
{
int comparison = res1.getLowerBound().compareTo( res2.getLowerBound() );
if ( comparison < 0 )
{
lower = res2.getLowerBound();
lowerInclusive = res2.isLowerBoundInclusive();
}
else if ( comparison == 0 )
{
lower = res1.getLowerBound();
lowerInclusive = res1.isLowerBoundInclusive() && res2.isLowerBoundInclusive();
}
else
{
lower = res1.getLowerBound();
lowerInclusive = res1.isLowerBoundInclusive();
}
}
if ( res1.getUpperBound() == null )
{
upper = res2.getUpperBound();
upperInclusive = res2.isUpperBoundInclusive();
}
else if ( res2.getUpperBound() == null )
{
upper = res1.getUpperBound();
upperInclusive = res1.isUpperBoundInclusive();
}
else
{
int comparison = res1.getUpperBound().compareTo( res2.getUpperBound() );
if ( comparison < 0 )
{
upper = res1.getUpperBound();
upperInclusive = res1.isUpperBoundInclusive();
}
else if ( comparison == 0 )
{
upper = res1.getUpperBound();
upperInclusive = res1.isUpperBoundInclusive() && res2.isUpperBoundInclusive();
}
else
{
upper = res2.getUpperBound();
upperInclusive = res2.isUpperBoundInclusive();
}
}
// don't add if they are equal and one is not inclusive
if ( lower == null || upper == null || lower.compareTo( upper ) != 0 )
{
restrictions.add( new Restriction( lower, lowerInclusive, upper, upperInclusive ) );
}
else if ( lowerInclusive && upperInclusive )
{
restrictions.add( new Restriction( lower, lowerInclusive, upper, upperInclusive ) );
}
//noinspection ObjectEquality
if ( upper == res2.getUpperBound() )
{
// advance res2
if ( i2.hasNext() )
{
res2 = (Restriction) i2.next();
}
else
{
done = true;
}
}
else
{
// advance res1
if ( i1.hasNext() )
{
res1 = (Restriction) i1.next();
}
else
{
done = true;
}
}
}
else
{
// move on to next in r1
if ( i1.hasNext() )
{
res1 = (Restriction) i1.next();
}
else
{
done = true;
}
}
}
else
{
// move on to next in r2
if ( i2.hasNext() )
{
res2 = (Restriction) i2.next();
}
else
{
done = true;
}
}
}
return restrictions;
}
private ArtifactVersion max( ArtifactVersion v1, ArtifactVersion v2 )
{
if ( v1 == null )

View File

@ -147,6 +147,7 @@ public class VersionRangeTest
assertEquals( CHECK_VERSION_RECOMMENDATION, "1.1", mergedRange.getRecommendedVersion().toString() );
assertEquals( CHECK_NUM_RESTRICTIONS, 0, mergedRange.getRestrictions().size() );
// TODO: test reversed restrictions on all below
range1 = VersionRange.createFromVersionSpec( "[1.0,)" );
range2 = VersionRange.createFromVersionSpec( "1.1" );
mergedRange = range1.restrict( range2 );
@ -254,6 +255,163 @@ public class VersionRangeTest
assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.0", restriction.getUpperBound().toString() );
assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
range1 = VersionRange.createFromVersionSpec( "(,1.0], [1.1,)" );
range2 = VersionRange.createFromVersionSpec( "1.2" );
mergedRange = range1.restrict( range2 );
assertEquals( CHECK_VERSION_RECOMMENDATION, "1.2", mergedRange.getRecommendedVersion().toString() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 2, restrictions.size() );
restriction = (Restriction) restrictions.get( 0 );
assertNull( CHECK_LOWER_BOUND, restriction.getLowerBound() );
assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.0", restriction.getUpperBound().toString() );
assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
restriction = (Restriction) restrictions.get( 1 );
assertEquals( CHECK_LOWER_BOUND, "1.1", restriction.getLowerBound().toString() );
assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertNull( CHECK_UPPER_BOUND, restriction.getUpperBound() );
assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
range1 = VersionRange.createFromVersionSpec( "(,1.0], [1.1,)" );
range2 = VersionRange.createFromVersionSpec( "1.0.5" );
mergedRange = range1.restrict( range2 );
assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 2, restrictions.size() );
restriction = (Restriction) restrictions.get( 0 );
assertNull( CHECK_LOWER_BOUND, restriction.getLowerBound() );
assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.0", restriction.getUpperBound().toString() );
assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
restriction = (Restriction) restrictions.get( 1 );
assertEquals( CHECK_LOWER_BOUND, "1.1", restriction.getLowerBound().toString() );
assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertNull( CHECK_UPPER_BOUND, restriction.getUpperBound() );
assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
range1 = VersionRange.createFromVersionSpec( "(,1.1), (1.1,)" );
range2 = VersionRange.createFromVersionSpec( "1.1" );
mergedRange = range1.restrict( range2 );
assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 2, restrictions.size() );
restriction = (Restriction) restrictions.get( 0 );
assertNull( CHECK_LOWER_BOUND, restriction.getLowerBound() );
assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.1", restriction.getUpperBound().toString() );
assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
restriction = (Restriction) restrictions.get( 1 );
assertEquals( CHECK_LOWER_BOUND, "1.1", restriction.getLowerBound().toString() );
assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertNull( CHECK_UPPER_BOUND, restriction.getUpperBound() );
assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
range1 = VersionRange.createFromVersionSpec( "[1.1,1.3]" );
range2 = VersionRange.createFromVersionSpec( "(1.1,)" );
mergedRange = range1.restrict( range2 );
assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() );
restriction = (Restriction) restrictions.get( 0 );
assertEquals( CHECK_LOWER_BOUND, "1.1", restriction.getLowerBound().toString() );
assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.3", restriction.getUpperBound().toString() );
assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
range1 = VersionRange.createFromVersionSpec( "(,1.3)" );
range2 = VersionRange.createFromVersionSpec( "[1.2,1.3]" );
mergedRange = range1.restrict( range2 );
assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() );
restriction = (Restriction) restrictions.get( 0 );
assertEquals( CHECK_LOWER_BOUND, "1.2", restriction.getLowerBound().toString() );
assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.3", restriction.getUpperBound().toString() );
assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
range1 = VersionRange.createFromVersionSpec( "[1.1,1.3]" );
range2 = VersionRange.createFromVersionSpec( "[1.2,)" );
mergedRange = range1.restrict( range2 );
assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() );
restriction = (Restriction) restrictions.get( 0 );
assertEquals( CHECK_LOWER_BOUND, "1.2", restriction.getLowerBound().toString() );
assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.3", restriction.getUpperBound().toString() );
assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
range1 = VersionRange.createFromVersionSpec( "(,1.3]" );
range2 = VersionRange.createFromVersionSpec( "[1.2,1.4]" );
mergedRange = range1.restrict( range2 );
assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() );
restriction = (Restriction) restrictions.get( 0 );
assertEquals( CHECK_LOWER_BOUND, "1.2", restriction.getLowerBound().toString() );
assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.3", restriction.getUpperBound().toString() );
assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
range1 = VersionRange.createFromVersionSpec( "(1.2,1.3]" );
range2 = VersionRange.createFromVersionSpec( "[1.1,1.4]" );
mergedRange = range1.restrict( range2 );
assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() );
restriction = (Restriction) restrictions.get( 0 );
assertEquals( CHECK_LOWER_BOUND, "1.2", restriction.getLowerBound().toString() );
assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.3", restriction.getUpperBound().toString() );
assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
range1 = VersionRange.createFromVersionSpec( "(1.2,1.3)" );
range2 = VersionRange.createFromVersionSpec( "[1.1,1.4]" );
mergedRange = range1.restrict( range2 );
assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() );
restriction = (Restriction) restrictions.get( 0 );
assertEquals( CHECK_LOWER_BOUND, "1.2", restriction.getLowerBound().toString() );
assertFalse( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.3", restriction.getUpperBound().toString() );
assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
range1 = VersionRange.createFromVersionSpec( "[1.2,1.3)" );
range2 = VersionRange.createFromVersionSpec( "[1.1,1.4]" );
mergedRange = range1.restrict( range2 );
assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() );
restriction = (Restriction) restrictions.get( 0 );
assertEquals( CHECK_LOWER_BOUND, "1.2", restriction.getLowerBound().toString() );
assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.3", restriction.getUpperBound().toString() );
assertFalse( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
range1 = VersionRange.createFromVersionSpec( "[1.0,1.1]" );
range2 = VersionRange.createFromVersionSpec( "[1.1,1.4]" );
mergedRange = range1.restrict( range2 );
assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() );
restriction = (Restriction) restrictions.get( 0 );
assertEquals( CHECK_LOWER_BOUND, "1.1", restriction.getLowerBound().toString() );
assertTrue( CHECK_LOWER_BOUND_INCLUSIVE, restriction.isLowerBoundInclusive() );
assertEquals( CHECK_UPPER_BOUND, "1.1", restriction.getUpperBound().toString() );
assertTrue( CHECK_UPPER_BOUND_INCLUSIVE, restriction.isUpperBoundInclusive() );
// TODO: specify this differently from no restriction - no restriction should be null, or one restriction with null ends?
range1 = VersionRange.createFromVersionSpec( "[1.0,1.1)" );
range2 = VersionRange.createFromVersionSpec( "[1.1,1.4]" );
mergedRange = range1.restrict( range2 );
assertNull( CHECK_VERSION_RECOMMENDATION, mergedRange.getRecommendedVersion() );
restrictions = mergedRange.getRestrictions();
assertEquals( CHECK_NUM_RESTRICTIONS, 0, restrictions.size() );
// TODO: multiple on one side, multiple on both sides, boundaries
}
private void checkInvalidRange( String version )