[maven] branch master updated: [MNG-6571] separate caches for create from spec and version

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[maven] branch master updated: [MNG-6571] separate caches for create from spec and version

hboutemy
This is an automated email from the ASF dual-hosted git repository.

hboutemy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 961746f  [MNG-6571] separate caches for create from spec and version
961746f is described below

commit 961746f25fe1d915053e544dc494ef3b9ddecb23
Author: Hervé Boutemy <[hidden email]>
AuthorDate: Wed Feb 6 09:56:33 2019 +0100

    [MNG-6571] separate caches for create from spec and version
---
 .../maven/artifact/versioning/VersionRange.java       | 12 ++++++++----
 .../maven/artifact/versioning/VersionRangeTest.java   | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java
index 5d7b254..c37eabb 100644
--- a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java
+++ b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/VersionRange.java
@@ -35,9 +35,12 @@ import org.apache.maven.artifact.Artifact;
  */
 public class VersionRange
 {
-    private static final Map<String, VersionRange> CACHE =
+    private static final Map<String, VersionRange> CACHE_SPEC =
         Collections.<String, VersionRange>synchronizedMap( new WeakHashMap<String, VersionRange>() );
 
+    private static final Map<String, VersionRange> CACHE_VERSION =
+                    Collections.<String, VersionRange>synchronizedMap( new WeakHashMap<String, VersionRange>() );
+
     private final ArtifactVersion recommendedVersion;
 
     private final List<Restriction> restrictions;
@@ -107,7 +110,7 @@ public class VersionRange
             return null;
         }
 
-        VersionRange cached = CACHE.get( spec );
+        VersionRange cached = CACHE_SPEC.get( spec );
         if ( cached != null )
         {
             return cached;
@@ -176,7 +179,7 @@ public class VersionRange
         }
 
         cached = new VersionRange( version, restrictions );
-        CACHE.put( spec, cached );
+        CACHE_SPEC.put( spec, cached );
         return cached;
     }
 
@@ -236,11 +239,12 @@ public class VersionRange
 
     public static VersionRange createFromVersion( String version )
     {
-        VersionRange cached = CACHE.get( version );
+        VersionRange cached = CACHE_VERSION.get( version );
         if ( cached == null )
         {
             List<Restriction> restrictions = Collections.emptyList();
             cached = new VersionRange( new DefaultArtifactVersion( version ), restrictions );
+            CACHE_VERSION.put( version, cached );
         }
         return cached;
     }
diff --git a/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java b/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java
index dc8d3d5..2cf0fec 100644
--- a/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java
+++ b/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/VersionRangeTest.java
@@ -727,4 +727,23 @@ public class VersionRangeTest
     {
         // assertTrue( new DefaultArtifactVersion( "1.0-alpha10" ).compareTo( new DefaultArtifactVersion( "1.0-alpha1" ) ) > 0 );
     }
+
+    public void testCache()
+        throws InvalidVersionSpecificationException
+    {
+        VersionRange range = VersionRange.createFromVersionSpec( "[1.0,1.2]" );
+        assertSame( range, VersionRange.createFromVersionSpec( "[1.0,1.2]" ) ); // same instance from spec cache
+
+        VersionRange spec = VersionRange.createFromVersionSpec( "1.0" );
+        assertSame( spec, VersionRange.createFromVersionSpec( "1.0" ) ); // same instance from spec cache
+        List<Restriction> restrictions = spec.getRestrictions();
+        assertEquals( CHECK_NUM_RESTRICTIONS, 1, restrictions.size() );
+
+        VersionRange version = VersionRange.createFromVersion( "1.0" );
+        assertSame( version, VersionRange.createFromVersion( "1.0" ) ); // same instance from version cache
+        restrictions = version.getRestrictions();
+        assertEquals( CHECK_NUM_RESTRICTIONS, 0, restrictions.size() );
+
+        assertFalse( "check !VersionRange.createFromVersionSpec(x).equals(VersionRange.createFromVersion(x))", spec.equals( version ) );
+    }
 }