diff options
author | BJ Hargrave | 2020-05-04 20:45:23 +0000 |
---|---|---|
committer | BJ Hargrave | 2020-05-04 20:48:08 +0000 |
commit | e7d5b67069343efeed172f91b30082b6e9a996f5 (patch) | |
tree | 5836ab734efd7c72da5ca97f51eb1fe1df6befbc | |
parent | 15857fa80132b34246c62480198d3684b2a129d3 (diff) | |
download | rt.equinox.framework-e7d5b67069343efeed172f91b30082b6e9a996f5.tar.gz rt.equinox.framework-e7d5b67069343efeed172f91b30082b6e9a996f5.tar.xz rt.equinox.framework-e7d5b67069343efeed172f91b30082b6e9a996f5.zip |
Bug 562796: filter: Cache certain immutable values
We cache Long, Float, Double, Boolean, and Version objects created from
the attribute value string in the filter expression to avoid
repetitive parsing.
Change-Id: I0f573db828b21a2472eddc831ac5e595a0c30d2b
-rw-r--r-- | bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/FilterImpl.java | 87 |
1 files changed, 23 insertions, 64 deletions
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/FilterImpl.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/FilterImpl.java index d0ccb3637..528eba60f 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/FilterImpl.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/FilterImpl.java @@ -32,6 +32,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Function; import org.eclipse.osgi.framework.util.CaseInsensitiveDictionaryMap; import org.eclipse.osgi.internal.debug.Debug; import org.eclipse.osgi.internal.messages.Msg; @@ -514,17 +515,9 @@ public abstract class FilterImpl implements Filter { if (value1 instanceof Collection<?>) { return compare_Collection((Collection<?>) value1); } - if (value1 instanceof Integer) { - return compare_Integer(((Integer) value1).intValue()); - } - if (value1 instanceof Long) { - return compare_Long(((Long) value1).longValue()); - } - if (value1 instanceof Byte) { - return compare_Byte(((Byte) value1).byteValue()); - } - if (value1 instanceof Short) { - return compare_Short(((Short) value1).shortValue()); + if (value1 instanceof Integer || value1 instanceof Long || value1 instanceof Byte + || value1 instanceof Short) { + return compare_Long(((Number) value1).longValue()); } if (value1 instanceof Character) { return compare_Character(((Character) value1).charValue()); @@ -571,7 +564,7 @@ public abstract class FilterImpl implements Filter { if (debug) { Debug.println(operation() + "(" + value1 + "," + value() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } - if (compare_Integer(value1)) { + if (compare_Long(value1)) { return true; } } @@ -595,7 +588,7 @@ public abstract class FilterImpl implements Filter { if (debug) { Debug.println(operation() + "(" + value1 + "," + value() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } - if (compare_Byte(value1)) { + if (compare_Long(value1)) { return true; } } @@ -607,7 +600,7 @@ public abstract class FilterImpl implements Filter { if (debug) { Debug.println(operation() + "(" + value1 + "," + value() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } - if (compare_Short(value1)) { + if (compare_Long(value1)) { return true; } } @@ -684,10 +677,6 @@ public abstract class FilterImpl implements Filter { return false; } - boolean compare_Byte(byte byteval) { - return false; - } - boolean compare_Character(char charval) { return false; } @@ -700,18 +689,10 @@ public abstract class FilterImpl implements Filter { return false; } - boolean compare_Integer(int intval) { - return false; - } - boolean compare_Long(long longval) { return false; } - boolean compare_Short(short shortval) { - return false; - } - /** * Encode the value string such that '(', '*', ')' and '\' are escaped. * @@ -864,12 +845,23 @@ public abstract class FilterImpl implements Filter { static class Equal extends Item { final String value; + private Object cached; Equal(String attr, String value, boolean debug) { super(attr, debug); this.value = value; } + private <T> T convert(Class<T> type, Function<String, ? extends T> converter) { + @SuppressWarnings("unchecked") + T converted = (T) cached; + if ((converted != null) && type.isInstance(converted)) { + return converted; + } + cached = converted = converter.apply(value.trim()); + return converted; + } + @Override String operation() { return "EQUAL"; //$NON-NLS-1$ @@ -892,7 +884,7 @@ public abstract class FilterImpl implements Filter { @Override boolean compare_Version(Version value1) { try { - Version version2 = Version.valueOf(value); + Version version2 = convert(Version.class, Version::valueOf); return comparison(value1.compareTo(version2)); } catch (Exception e) { // if the valueOf or compareTo method throws an exception @@ -902,22 +894,11 @@ public abstract class FilterImpl implements Filter { @Override boolean compare_Boolean(boolean boolval) { - boolean boolval2 = Boolean.parseBoolean(value.trim()); + boolean boolval2 = convert(Boolean.class, Boolean::valueOf).booleanValue(); return comparison(Boolean.compare(boolval, boolval2)); } @Override - boolean compare_Byte(byte byteval) { - byte byteval2; - try { - byteval2 = Byte.parseByte(value.trim()); - } catch (IllegalArgumentException e) { - return false; - } - return comparison(Byte.compare(byteval, byteval2)); - } - - @Override boolean compare_Character(char charval) { char charval2; try { @@ -932,7 +913,7 @@ public abstract class FilterImpl implements Filter { boolean compare_Double(double doubleval) { double doubleval2; try { - doubleval2 = Double.parseDouble(value.trim()); + doubleval2 = convert(Double.class, Double::valueOf).doubleValue(); } catch (IllegalArgumentException e) { return false; } @@ -943,7 +924,7 @@ public abstract class FilterImpl implements Filter { boolean compare_Float(float floatval) { float floatval2; try { - floatval2 = Float.parseFloat(value.trim()); + floatval2 = convert(Float.class, Float::valueOf).floatValue(); } catch (IllegalArgumentException e) { return false; } @@ -951,21 +932,10 @@ public abstract class FilterImpl implements Filter { } @Override - boolean compare_Integer(int intval) { - int intval2; - try { - intval2 = Integer.parseInt(value.trim()); - } catch (IllegalArgumentException e) { - return false; - } - return comparison(Integer.compare(intval, intval2)); - } - - @Override boolean compare_Long(long longval) { long longval2; try { - longval2 = Long.parseLong(value.trim()); + longval2 = convert(Long.class, Long::valueOf).longValue(); } catch (IllegalArgumentException e) { return false; } @@ -973,17 +943,6 @@ public abstract class FilterImpl implements Filter { } @Override - boolean compare_Short(short shortval) { - short shortval2; - try { - shortval2 = Short.parseShort(value.trim()); - } catch (IllegalArgumentException e) { - return false; - } - return comparison(Short.compare(shortval, shortval2)); - } - - @Override boolean compare_Comparable(Comparable<Object> value1) { Object value2 = valueOf(value1.getClass()); if (value2 == null) { |