[MNG-6571] cache VersionRange instances, they are immutable

This commit is contained in:
Hervé Boutemy 2019-01-23 11:01:18 +01:00
parent b7249aff22
commit 8f9075d3ad
2 changed files with 27 additions and 4 deletions

View File

@ -138,7 +138,7 @@ public final class ArtifactUtils
range = VersionRange.createFromVersion( artifact.getVersion() );
}
DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(),
DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range,
artifact.getScope(), artifact.getType(), artifact.getClassifier(),
artifact.getArtifactHandler(), artifact.isOptional() );
clone.setRelease( artifact.isRelease() );

View File

@ -23,6 +23,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import org.apache.maven.artifact.Artifact;
@ -33,6 +35,9 @@ import org.apache.maven.artifact.Artifact;
*/
public class VersionRange
{
private static final Map<String, VersionRange> CACHE =
Collections.<String, VersionRange>synchronizedMap( new WeakHashMap<String, VersionRange>() );
private final ArtifactVersion recommendedVersion;
private final List<Restriction> restrictions;
@ -54,6 +59,11 @@ public class VersionRange
return restrictions;
}
/**
* @deprecated VersionRange is immutable, cloning is not useful and even more an issue against the cache
* @return a clone
*/
@Deprecated
public VersionRange cloneOf()
{
List<Restriction> copiedRestrictions = null;
@ -97,6 +107,12 @@ public class VersionRange
return null;
}
VersionRange cached = CACHE.get( spec );
if ( cached != null )
{
return cached;
}
List<Restriction> restrictions = new ArrayList<>();
String process = spec;
ArtifactVersion version = null;
@ -159,7 +175,9 @@ public class VersionRange
}
}
return new VersionRange( version, restrictions );
cached = new VersionRange( version, restrictions );
CACHE.put( spec, cached );
return cached;
}
private static Restriction parseRestriction( String spec )
@ -218,8 +236,13 @@ public class VersionRange
public static VersionRange createFromVersion( String version )
{
List<Restriction> restrictions = Collections.emptyList();
return new VersionRange( new DefaultArtifactVersion( version ), restrictions );
VersionRange cached = CACHE.get( version );
if ( cached == null )
{
List<Restriction> restrictions = Collections.emptyList();
cached = new VersionRange( new DefaultArtifactVersion( version ), restrictions );
}
return cached;
}
/**