Use ParameterizedMetricKeys again
This commit is contained in:
@ -16,8 +16,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.QualifiedName;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.ClassMetric;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.ClassMetricKey;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.MemoKey;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.Metric.Version;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.MetricVersion;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.OperationMetricKey;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.ResultOption;
|
||||
@ -42,7 +40,7 @@ import net.sourceforge.pmd.lang.java.oom.signature.OperationSignature;
|
||||
private Map<FieldSignature, Set<String>> fields = new HashMap<>();
|
||||
private Map<String, ClassStats> nestedClasses = new HashMap<>();
|
||||
|
||||
private Map<MemoKey, Double> memo = new HashMap<>();
|
||||
private Map<ParameterizedMetricKey, Double> memo = new HashMap<>();
|
||||
|
||||
// References to the hierarchy
|
||||
// TODO:cf useful?
|
||||
@ -252,7 +250,7 @@ import net.sourceforge.pmd.lang.java.oom.signature.OperationSignature;
|
||||
*/
|
||||
/* default */ double compute(ClassMetricKey key, ASTAnyTypeDeclaration node, boolean force, MetricVersion version) {
|
||||
|
||||
MemoKey paramKey = version == Version.STANDARD ? key : version;
|
||||
ParameterizedMetricKey paramKey = ParameterizedMetricKey.build(key, version);
|
||||
// if memo.get(key) == null then the metric has never been computed. NaN is a valid value.
|
||||
Double prev = memo.get(paramKey);
|
||||
if (!force && prev != null) {
|
||||
|
@ -8,8 +8,6 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.MemoKey;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.Metric.Version;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.MetricVersion;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.OperationMetric;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.OperationMetricKey;
|
||||
@ -23,7 +21,7 @@ import net.sourceforge.pmd.lang.java.oom.api.OperationMetricKey;
|
||||
/* default */ class OperationStats {
|
||||
|
||||
private final String name;
|
||||
private final Map<MemoKey, Double> memo = new HashMap<>();
|
||||
private final Map<ParameterizedMetricKey, Double> memo = new HashMap<>();
|
||||
|
||||
|
||||
/* default */ OperationStats(String name) {
|
||||
@ -48,7 +46,7 @@ import net.sourceforge.pmd.lang.java.oom.api.OperationMetricKey;
|
||||
/* default */ double compute(OperationMetricKey key, ASTMethodOrConstructorDeclaration node, boolean force,
|
||||
MetricVersion version) {
|
||||
|
||||
MemoKey paramKey = version == Version.STANDARD ? key : version;
|
||||
ParameterizedMetricKey paramKey = ParameterizedMetricKey.build(key, version);
|
||||
Double prev = memo.get(paramKey);
|
||||
if (!force && prev != null) {
|
||||
return prev;
|
||||
|
@ -0,0 +1,83 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.java.oom;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.oom.api.Metric;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.MetricKey;
|
||||
import net.sourceforge.pmd.lang.java.oom.api.MetricVersion;
|
||||
|
||||
/**
|
||||
* Represents a key parameterized with its version. Used to index memoization maps.
|
||||
*
|
||||
* @author Clément Fournier
|
||||
*/
|
||||
public final class ParameterizedMetricKey {
|
||||
|
||||
private static final Map<Integer, ParameterizedMetricKey> POOL = new HashMap<>();
|
||||
|
||||
/** The metric key. */
|
||||
public final MetricKey<? extends Metric> key;
|
||||
/** The version of the metric. */
|
||||
public final MetricVersion version;
|
||||
|
||||
/** Used internally by the pooler. */
|
||||
private ParameterizedMetricKey(MetricKey<? extends Metric> key, MetricVersion version) {
|
||||
this.key = key;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
/** Builds a parameterized metric key. */
|
||||
public static ParameterizedMetricKey build(MetricKey<? extends Metric> key, MetricVersion version) {
|
||||
int code = code(key, version);
|
||||
ParameterizedMetricKey paramKey = POOL.get(code);
|
||||
if (paramKey == null) {
|
||||
POOL.put(code, new ParameterizedMetricKey(key, version));
|
||||
}
|
||||
return POOL.get(code);
|
||||
}
|
||||
|
||||
/** Used by the pooler. */
|
||||
private static int code(MetricKey key, MetricVersion version) {
|
||||
int result = key.hashCode();
|
||||
result = 31 * result + version.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ParameterizedMetricKey{"
|
||||
+ "key=" + key
|
||||
+ ", version=" + version
|
||||
+ '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ParameterizedMetricKey that = (ParameterizedMetricKey) o;
|
||||
|
||||
if (!key.equals(that.key)) {
|
||||
return false;
|
||||
}
|
||||
return version.equals(that.version);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = key.hashCode();
|
||||
result = 31 * result + version.hashCode();
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.java.oom.api;
|
||||
|
||||
/**
|
||||
* Keys for the memoization maps. By default, MetricKeys are used, but if the version is not
|
||||
* {@link Metric.Version#STANDARD}, then the version is used instead. This is why versions cannot be shared across
|
||||
* metrics.
|
||||
*
|
||||
* @author Clément Fournier
|
||||
*/
|
||||
public interface MemoKey {
|
||||
}
|
@ -9,7 +9,7 @@ package net.sourceforge.pmd.lang.java.oom.api;
|
||||
*
|
||||
* @param <T> Type of the metric to identify (ClassMetric or OperationMetric).
|
||||
*/
|
||||
public interface MetricKey<T extends Metric> extends MemoKey {
|
||||
public interface MetricKey<T extends Metric> {
|
||||
|
||||
/**
|
||||
* Returns the name of the metric.
|
||||
|
@ -13,7 +13,7 @@ package net.sourceforge.pmd.lang.java.oom.api;
|
||||
*
|
||||
* @author Clément Fournier
|
||||
*/
|
||||
public interface MetricVersion extends MemoKey {
|
||||
public interface MetricVersion {
|
||||
|
||||
/**
|
||||
* Returns the name of the version.
|
||||
|
Reference in New Issue
Block a user