Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarsten Thoms2017-04-06 17:30:04 -0400
committerKarsten Thoms2017-06-27 04:09:46 -0400
commitff12ff963d3657c29884b15baa1f105c8a09ac77 (patch)
tree18ddbb6b9b6f80a98598846e55ce8577f466c0aa /bundles/org.eclipse.equinox.p2.metadata
parentb1b14d5061eb7a9ae592fbd71a2437bfb5d86ba3 (diff)
downloadrt.equinox.p2-ff12ff963d3657c29884b15baa1f105c8a09ac77.tar.gz
rt.equinox.p2-ff12ff963d3657c29884b15baa1f105c8a09ac77.tar.xz
rt.equinox.p2-ff12ff963d3657c29884b15baa1f105c8a09ac77.zip
Bug 514885 Pooling Version and VersionRange
- Introduce weak object pool for Verson and VersionRange - Use VersionRange.create() for getting pooled instances - JavaDoc & check input for empty string Change-Id: Ie42bdc1059d8c667af312038c26c035a6ef674af Signed-off-by: Karsten Thoms <karsten.thoms@itemis.de>
Diffstat (limited to 'bundles/org.eclipse.equinox.p2.metadata')
-rw-r--r--bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/internal/p2/metadata/expression/RangeFunction.java3
-rw-r--r--bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/metadata/Version.java22
-rw-r--r--bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/metadata/VersionRange.java36
3 files changed, 58 insertions, 3 deletions
diff --git a/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/internal/p2/metadata/expression/RangeFunction.java b/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/internal/p2/metadata/expression/RangeFunction.java
index fecd7d209..09d241253 100644
--- a/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/internal/p2/metadata/expression/RangeFunction.java
+++ b/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/internal/p2/metadata/expression/RangeFunction.java
@@ -10,7 +10,6 @@
*******************************************************************************/
package org.eclipse.equinox.internal.p2.metadata.expression;
-
import org.eclipse.equinox.p2.metadata.VersionRange;
/**
@@ -27,7 +26,7 @@ public final class RangeFunction extends Function {
}
Object createInstance(Object arg) {
- return new VersionRange((String) arg);
+ return VersionRange.create((String) arg);
}
public String getOperator() {
diff --git a/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/metadata/Version.java b/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/metadata/Version.java
index 282637572..c24b8cb2f 100644
--- a/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/metadata/Version.java
+++ b/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/metadata/Version.java
@@ -12,6 +12,8 @@
package org.eclipse.equinox.p2.metadata;
import java.io.Serializable;
+import java.lang.ref.SoftReference;
+import java.util.WeakHashMap;
import org.eclipse.equinox.internal.p2.metadata.*;
/**
@@ -40,6 +42,7 @@ import org.eclipse.equinox.internal.p2.metadata.*;
*/
public abstract class Version implements Comparable<Version>, Serializable {
public static final String RAW_PREFIX = "raw:"; //$NON-NLS-1$
+ private static WeakHashMap<String, SoftReference<Version>> POOL = new WeakHashMap<String, SoftReference<Version>>();
/**
* The version that is semantically greater then all other versions.
@@ -66,6 +69,12 @@ public abstract class Version implements Comparable<Version>, Serializable {
/**
* Parses a version identifier from the specified string.
+ * <p>
+ * Note that this method performs a non thread-safe object pooling. Instances are
+ * stored in a weak cache, i.e. for multiple calls with the same input it is likely
+ * but not guaranteed that the same instance is retrieved. Same holds for concurrent
+ * access on this method. Clients must not assume to get the same instance for
+ * subsequent calls.
*
* @param version String representation of the version identifier. Leading
* and trailing whitespace will be ignored.
@@ -76,7 +85,18 @@ public abstract class Version implements Comparable<Version>, Serializable {
* formatted.
*/
public static Version create(String version) {
- return version == null ? null : VersionParser.parse(version, 0, version.length());
+ Version v = null;
+ if (version != null && version.length() > 0) {
+ SoftReference<Version> vRef = POOL.get(version);
+ v = vRef != null ? vRef.get() : null;
+ if (v == null) {
+ v = VersionParser.parse(version, 0, version.length());
+ synchronized (POOL) {
+ POOL.put(version, new SoftReference<Version>(v));
+ }
+ }
+ }
+ return v;
}
/**
diff --git a/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/metadata/VersionRange.java b/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/metadata/VersionRange.java
index 34d3d81b5..6b017245d 100644
--- a/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/metadata/VersionRange.java
+++ b/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/metadata/VersionRange.java
@@ -12,6 +12,8 @@
package org.eclipse.equinox.p2.metadata;
import java.io.Serializable;
+import java.lang.ref.SoftReference;
+import java.util.WeakHashMap;
import org.eclipse.equinox.internal.p2.metadata.*;
import org.eclipse.osgi.util.NLS;
@@ -34,6 +36,7 @@ public class VersionRange implements Serializable {
* An empty OSGi Version range.
*/
public static final VersionRange emptyRange = new VersionRange(Version.emptyVersion, true, Version.MAX_VERSION, true);
+ private static WeakHashMap<String, SoftReference<VersionRange>> POOL = new WeakHashMap<String, SoftReference<VersionRange>>();
private final Version minVersion;
private final boolean includeMin;
@@ -252,6 +255,39 @@ public class VersionRange implements Serializable {
validateRange();
}
+ /**
+ * Parses a version range from the specified string.
+ * <p>
+ * Note that this method performs a non thread-safe object pooling. Instances are
+ * stored in a weak cache, i.e. for multiple calls with the same input it is likely
+ * but not guaranteed that the same instance is retrieved. Same holds for concurrent
+ * access on this method. Clients must not assume to get the same instance for
+ * subsequent calls.
+ *
+ * @param versionRange String representation of the version range. Leading
+ * and trailing whitespace will be ignored.
+ * @return A <code>VersionRange</code> object representing the version range
+ * or <code>null</code> if <code>versionRange</code> is <code>null</code> or
+ * an empty string.
+ * @throws IllegalArgumentException If <code>versionRange</code> is improperly
+ * formatted.
+ * @since 2.3
+ */
+ public static VersionRange create(String versionRange) {
+ VersionRange v = null;
+ if (versionRange != null && versionRange.length() > 0) {
+ SoftReference<VersionRange> vRef = POOL.get(versionRange);
+ v = vRef != null ? vRef.get() : null;
+ if (v == null) {
+ v = new VersionRange(versionRange);
+ synchronized (POOL) {
+ POOL.put(versionRange, new SoftReference<VersionRange>(v));
+ }
+ }
+ }
+ return v;
+ }
+
private static IVersionFormat parseFormat(String versionRange, int[] position) {
int pos = VersionParser.skipWhite(versionRange, position[0]);
if (!versionRange.startsWith("format(", pos)) //$NON-NLS-1$

Back to the top