Skip to content

Commit

Permalink
FURNACE-73: Cached SingleVersion instances and introduced valueOf
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 21, 2015
1 parent 89ea0b5 commit 150a007
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ public static AddonId from(String name, String version, String apiVersion)
AddonId id = new AddonId();

id.name = name;
id.version = new SingleVersion(version);
id.version = SingleVersion.valueOf(version);
if (apiVersion == null || apiVersion.trim().isEmpty())
id.apiVersion = EmptyVersion.getInstance();
else
id.apiVersion = new SingleVersion(apiVersion);
id.apiVersion = SingleVersion.valueOf(apiVersion);

return id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
public class EmptyVersion extends SingleVersion implements Version
{
private static final EmptyVersion INSTANCE = new EmptyVersion();


@SuppressWarnings("deprecation")
private EmptyVersion()
{
super("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public boolean isExact()
@Override
public Version getMin()
{
return new SingleVersion("");
return EmptyVersion.getInstance();
}

@Override
public Version getMax()
{
return new SingleVersion("");
return EmptyVersion.getInstance();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.jboss.forge.furnace.util.Assert;

Expand All @@ -42,6 +45,24 @@ public class SingleVersion implements Version

private ComparableVersion comparable;

private static final Map<String, SingleVersion> CACHE = Collections
.<String, SingleVersion> synchronizedMap(new WeakHashMap<String, SingleVersion>());

public static final SingleVersion valueOf(String version)
{
SingleVersion singleVersion = CACHE.get(version);
if (singleVersion == null)
{
singleVersion = new SingleVersion(version);
CACHE.put(version, singleVersion);
}
return singleVersion;
}

/**
* @deprecated use {@link SingleVersion#valueOf(String)} instead
*/
@Deprecated
public SingleVersion(String version)
{
parseVersion(version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static VersionRange parseVersionRange(String range) throws VersionExcepti
throw new VersionException("Single version must be surrounded by []: " + range);
}

Version version = new SingleVersion(process);
Version version = SingleVersion.valueOf(process);
result = new DefaultVersionRange(version, lowerBoundInclusive, version, upperBoundInclusive);
}
else
Expand All @@ -95,12 +95,12 @@ public static VersionRange parseVersionRange(String range) throws VersionExcepti
Version lowerVersion = null;
if (lowerBound.length() > 0)
{
lowerVersion = new SingleVersion(lowerBound);
lowerVersion = SingleVersion.valueOf(lowerBound);
}
Version upperVersion = null;
if (upperBound.length() > 0)
{
upperVersion = new SingleVersion(upperBound);
upperVersion = SingleVersion.valueOf(upperBound);
}

if (upperVersion != null && lowerVersion != null && upperVersion.compareTo(lowerVersion) < 0)
Expand Down Expand Up @@ -196,12 +196,12 @@ public static MultipleVersionRange parseMultipleVersionRange(String intersection
if (version.startsWith("[") || version.startsWith("("))
ranges.add(parseVersionRange(version));
else
ranges.add(new SingleVersionRange(new SingleVersion(version)));
ranges.add(new SingleVersionRange(SingleVersion.valueOf(version)));
}
}
else
{
ranges.add(new SingleVersionRange(new SingleVersion(process)));
ranges.add(new SingleVersionRange(SingleVersion.valueOf(process)));
}
}

Expand Down Expand Up @@ -287,7 +287,7 @@ public static Version getSpecificationVersionFor(Class<?> type)
}
else
{
result = new SingleVersion(version);
result = SingleVersion.valueOf(version);
}
}
return result;
Expand Down Expand Up @@ -318,7 +318,7 @@ public static Version getImplementationVersionFor(Class<?> type)
}
else
{
result = new SingleVersion(version);
result = SingleVersion.valueOf(version);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void testFromCoordinatesMissingAPIVersion() throws Exception
AddonId addon = AddonId.fromCoordinates("org.jboss.forge.addon:resources,2.0.0-SNAPSHOT");
Assert.assertEquals(EmptyVersion.getInstance(), addon.getApiVersion());
Assert.assertEquals("org.jboss.forge.addon:resources", addon.getName());
Assert.assertEquals(new SingleVersion("2.0.0-SNAPSHOT"), addon.getVersion());
Assert.assertEquals(SingleVersion.valueOf("2.0.0-SNAPSHOT"), addon.getVersion());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public class SingleVersionTest
@Test(expected = IllegalArgumentException.class)
public void testVersionMustNotBeNull()
{
new SingleVersion(null);
SingleVersion.valueOf(null);
}

@Test
public void testIrregularVersion()
{
Version version = new SingleVersion("asdfa[23()_2345");
Version version = SingleVersion.valueOf("asdfa[23()_2345");
Assert.assertEquals(0, version.getMajorVersion());
Assert.assertEquals(0, version.getMinorVersion());
Assert.assertEquals(0, version.getIncrementalVersion());
Expand All @@ -35,7 +35,7 @@ public void testIrregularVersion()
@Test
public void testIrregularVersion2()
{
Version version = new SingleVersion("2.16.asdf.4.adsf");
Version version = SingleVersion.valueOf("2.16.asdf.4.adsf");
Assert.assertEquals(0, version.getMajorVersion());
Assert.assertEquals(0, version.getMinorVersion());
Assert.assertEquals(0, version.getIncrementalVersion());
Expand All @@ -47,7 +47,7 @@ public void testIrregularVersion2()
@Test
public void testSnapshot() throws Exception
{
Version version = new SingleVersion("2.18.2-SNAPSHOT");
Version version = SingleVersion.valueOf("2.18.2-SNAPSHOT");
Assert.assertEquals(2, version.getMajorVersion());
Assert.assertEquals(18, version.getMinorVersion());
Assert.assertEquals(2, version.getIncrementalVersion());
Expand All @@ -59,7 +59,7 @@ public void testSnapshot() throws Exception
@Test
public void testSnapshotBuildNumber() throws Exception
{
Version version = new SingleVersion("2.18.2-SNAPSHOT-2");
Version version = SingleVersion.valueOf("2.18.2-SNAPSHOT-2");
Assert.assertEquals(2, version.getMajorVersion());
Assert.assertEquals(18, version.getMinorVersion());
Assert.assertEquals(2, version.getIncrementalVersion());
Expand All @@ -71,7 +71,7 @@ public void testSnapshotBuildNumber() throws Exception
@Test
public void testFinal() throws Exception
{
Version version = new SingleVersion("2.18.2.Final");
Version version = SingleVersion.valueOf("2.18.2.Final");
Assert.assertEquals(2, version.getMajorVersion());
Assert.assertEquals(18, version.getMinorVersion());
Assert.assertEquals(2, version.getIncrementalVersion());
Expand All @@ -83,7 +83,7 @@ public void testFinal() throws Exception
@Test
public void testFinalBuildNumber() throws Exception
{
Version version = new SingleVersion("2.18.2.Final-01");
Version version = SingleVersion.valueOf("2.18.2.Final-01");
Assert.assertEquals(2, version.getMajorVersion());
Assert.assertEquals(18, version.getMinorVersion());
Assert.assertEquals(2, version.getIncrementalVersion());
Expand All @@ -95,7 +95,7 @@ public void testFinalBuildNumber() throws Exception
@Test
public void testBare() throws Exception
{
Version version = new SingleVersion("2.18.2");
Version version = SingleVersion.valueOf("2.18.2");
Assert.assertEquals(2, version.getMajorVersion());
Assert.assertEquals(18, version.getMinorVersion());
Assert.assertEquals(2, version.getIncrementalVersion());
Expand All @@ -107,7 +107,7 @@ public void testBare() throws Exception
@Test
public void testBareBuildNumber() throws Exception
{
Version version = new SingleVersion("2.18.2-4");
Version version = SingleVersion.valueOf("2.18.2-4");
Assert.assertEquals(2, version.getMajorVersion());
Assert.assertEquals(18, version.getMinorVersion());
Assert.assertEquals(2, version.getIncrementalVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ public class VersionsTest
@Test
public void testAreEqual()
{
Assert.assertEquals(new SingleVersion("1"), new SingleVersion("1"));
Assert.assertEquals(new SingleVersion("1.1"), new SingleVersion("1.1"));
Assert.assertEquals(new SingleVersion("1.1.1"), new SingleVersion("1.1.1"));
Assert.assertEquals(new SingleVersion("1.1.1-SNAPSHOT"), new SingleVersion("1.1.1-SNAPSHOT"));
Assert.assertNotEquals(new SingleVersion("1"), new SingleVersion("2"));
Assert.assertNotEquals(new SingleVersion("1.1"), new SingleVersion("1.1.1"));
Assert.assertNotEquals(new SingleVersion("1.1.1-SNAPSHOT"), new SingleVersion("1.1.1"));
Assert.assertEquals(SingleVersion.valueOf("1"), SingleVersion.valueOf("1"));
Assert.assertEquals(SingleVersion.valueOf("1.1"), SingleVersion.valueOf("1.1"));
Assert.assertEquals(SingleVersion.valueOf("1.1.1"), SingleVersion.valueOf("1.1.1"));
Assert.assertEquals(SingleVersion.valueOf("1.1.1-SNAPSHOT"), SingleVersion.valueOf("1.1.1-SNAPSHOT"));
Assert.assertNotEquals(SingleVersion.valueOf("1"), SingleVersion.valueOf("2"));
Assert.assertNotEquals(SingleVersion.valueOf("1.1"), SingleVersion.valueOf("1.1.1"));
Assert.assertNotEquals(SingleVersion.valueOf("1.1.1-SNAPSHOT"), SingleVersion.valueOf("1.1.1"));
}

@Test
public void testParseVersionRange() throws Exception
{
VersionRange range = Versions.parseVersionRange("[0,15]");
Assert.assertEquals(new SingleVersion("0"), range.getMin());
Assert.assertEquals(new SingleVersion("15"), range.getMax());
Assert.assertEquals(SingleVersion.valueOf("0"), range.getMin());
Assert.assertEquals(SingleVersion.valueOf("15"), range.getMax());
Assert.assertTrue(range.isMinInclusive());
Assert.assertTrue(range.isMaxInclusive());
}
Expand All @@ -45,26 +45,26 @@ public void testVersionRangeIntersection() throws Exception
VersionRange subset = Versions.parseVersionRange("[3,7)");

VersionRange intersection = Versions.intersection(set, subset);
Assert.assertEquals(new SingleVersion("3"), intersection.getMin());
Assert.assertEquals(new SingleVersion("7"), intersection.getMax());
Assert.assertEquals(SingleVersion.valueOf("3"), intersection.getMin());
Assert.assertEquals(SingleVersion.valueOf("7"), intersection.getMax());
Assert.assertTrue(intersection.isMinInclusive());
Assert.assertFalse(intersection.isMaxInclusive());
}

@Test
public void testVersionSnapshot() throws Exception
{
Version nonSnapshot = new SingleVersion("1.1.1");
Version nonSnapshot = SingleVersion.valueOf("1.1.1");
Assert.assertFalse(Versions.isSnapshot(nonSnapshot));
Version snapshot = new SingleVersion("1.1.1-SNAPSHOT");
Version snapshot = SingleVersion.valueOf("1.1.1-SNAPSHOT");
Assert.assertTrue(Versions.isSnapshot(snapshot));
}

@Test
public void testSnapshotLowerThanRelease() throws Exception
{
Version nonSnapshot = new SingleVersion("2.2.0-Final");
Version snapshot = new SingleVersion("2.1.2-SNAPSHOT");
Version nonSnapshot = SingleVersion.valueOf("2.2.0-Final");
Version snapshot = SingleVersion.valueOf("2.1.2-SNAPSHOT");
Assert.assertTrue(nonSnapshot.compareTo(snapshot) >= 0);
Assert.assertTrue(snapshot.compareTo(nonSnapshot) < 0);
}
Expand All @@ -73,17 +73,15 @@ public void testSnapshotLowerThanRelease() throws Exception
public void testIsApiCompatible0() throws Exception
{
Assert.assertTrue(Versions.isApiCompatible(
new SingleVersion("2.18.2-SNAPSHOT"),
new SingleVersion("2.16.1.Final"))
);
SingleVersion.valueOf("2.18.2-SNAPSHOT"),
SingleVersion.valueOf("2.16.1.Final")));
}

@Test
public void testIsApiCompatible1() throws Exception
{
Assert.assertTrue(Versions.isApiCompatible(
new SingleVersion("2.18.2.Final"),
new SingleVersion("2.16.1.Final"))
);
SingleVersion.valueOf("2.18.2.Final"),
SingleVersion.valueOf("2.16.1.Final")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static Version getRuntimeAPIVersion()
String versionOverride = System.getProperty("furnace.version.override");
if (versionOverride != null)
{
return new SingleVersion(versionOverride);
return SingleVersion.valueOf(versionOverride);
}
return Versions.getImplementationVersionFor(AddonRepository.class);
}
Expand Down Expand Up @@ -312,8 +312,7 @@ public Set<AddonDependencyEntry> call() throws Exception
child.getAttribute(ATTR_NAME),
Versions.parseMultipleVersionRange(child.getAttribute(ATTR_VERSION)),
Boolean.valueOf(child.getAttribute(ATTR_EXPORT)),
Boolean.valueOf(child.getAttribute(ATTR_OPTIONAL)))
);
Boolean.valueOf(child.getAttribute(ATTR_OPTIONAL))));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,38 @@ public class AddonRepositoryImplTest
public void testMinorVersionCompatible() throws Exception
{
AddonId entry = AddonId.fromCoordinates("com.example.plugin,40,1.0.0-SNAPSHOT");
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.1.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.2.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.2000.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.2-SNAPSHOT"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.1000-SNAPSHOT"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.1000-adsfasfsd"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.1.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.1.1.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.2.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.2.1.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(new SingleVersion("2.0.0.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(new SingleVersion("s1.0.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.1.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.2.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.2000.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.2-SNAPSHOT"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.1000-SNAPSHOT"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.1000-adsfasfsd"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.1.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.1.1.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.2.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.2.1.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("2.0.0.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("s1.0.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(null, entry));
}

@Test
public void testMinorVersionCompatibleBackwards() throws Exception
{
AddonId entry = AddonId.fromCoordinates("com.example.plugin,20.0i,1.1.0-SNAPSHOT");
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.1.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.2.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.2000.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.2-SNAPSHOT"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.1000-SNAPSHOT"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.0.1000-adsfasfsd"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.1.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.1.1.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.2.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion("1.2.1.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(new SingleVersion("2.0.0.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(new SingleVersion("s1.0.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(new SingleVersion(""), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.1.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.2.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.2000.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.2-SNAPSHOT"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.1000-SNAPSHOT"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.0.1000-adsfasfsd"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.1.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.1.1.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.2.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("1.2.1.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("2.0.0.Final"), entry));
Assert.assertFalse(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf("s1.0.0.Final"), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(SingleVersion.valueOf(""), entry));
Assert.assertTrue(AddonRepositoryImpl.isApiCompatible(null, entry));
}

Expand Down

0 comments on commit 150a007

Please sign in to comment.