From 9ba03dbb301c568c5eb1bd5cb084b67638b9957e Mon Sep 17 00:00:00 2001 From: lapentafd Date: Tue, 29 Jun 2021 11:31:34 +0100 Subject: Fix Sonar Issues in apex examples-adaptive Replacing local-variable type inference Replaced EqualsAndHashCode ToString methods with lombok annotation in AutoLearn class Issue-ID: POLICY-3093 Change-Id: I3f3911222f0e34ad3c7687d7a1e1aad218448303 Signed-off-by: lapentafd --- .../adaptive/concepts/AnomalyDetection.java | 61 ++-------------------- .../apex/examples/adaptive/concepts/AutoLearn.java | 59 +++------------------ ...alyDetectionPolicyDecideTaskSelectionLogic.java | 22 ++++---- .../AutoLearnPolicyDecideTaskSelectionLogic.java | 11 ++-- 4 files changed, 28 insertions(+), 125 deletions(-) diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AnomalyDetection.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AnomalyDetection.java index 6ff5ebccc..b0cff91d0 100644 --- a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AnomalyDetection.java +++ b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AnomalyDetection.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (c) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,17 +25,15 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import lombok.EqualsAndHashCode; /** * The Class AnomalyDetection is used as a Java context for Adaptive anomaly detection in the adaptive domain. */ +@EqualsAndHashCode public class AnomalyDetection implements Serializable { private static final long serialVersionUID = -823013127095523727L; - private static final int HASH_PRIME_1 = 31; - private static final int HASH_PRIME_2 = 1231; - private static final int HASH_PRIME_3 = 1237; - private boolean firstRound = true; private int frequency = 0; @@ -65,7 +64,7 @@ public class AnomalyDetection implements Serializable { */ public void init(final int incomingFrequency) { frequencyForecasted = new ArrayList<>(incomingFrequency); - for (int i = 0; i < incomingFrequency; i++) { + for (var i = 0; i < incomingFrequency; i++) { frequencyForecasted.add(null); } } @@ -182,56 +181,4 @@ public class AnomalyDetection implements Serializable { return "AnomalyDetection [firstRound=" + firstRound + ", frequency=" + frequency + ", anomalyScores=" + anomalyScores + ", frequencyForecasted=" + frequencyForecasted + "]"; } - - /** - * {@inheritDoc}. - */ - @Override - public int hashCode() { - final int prime = HASH_PRIME_1; - int result = 1; - result = prime * result + ((anomalyScores == null) ? 0 : anomalyScores.hashCode()); - result = prime * result + (firstRound ? HASH_PRIME_2 : HASH_PRIME_3); - result = prime * result + frequency; - result = prime * result + ((frequencyForecasted == null) ? 0 : frequencyForecasted.hashCode()); - return result; - } - - /** - * {@inheritDoc}. - */ - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final AnomalyDetection other = (AnomalyDetection) obj; - if (anomalyScores == null) { - if (other.anomalyScores != null) { - return false; - } - } else if (!anomalyScores.equals(other.anomalyScores)) { - return false; - } - if (firstRound != other.firstRound) { - return false; - } - if (frequency != other.frequency) { - return false; - } - if (frequencyForecasted == null) { - if (other.frequencyForecasted != null) { - return false; - } - } else if (!frequencyForecasted.equals(other.frequencyForecasted)) { - return false; - } - return true; - } } diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AutoLearn.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AutoLearn.java index dd5bf0c45..60c4d96d9 100644 --- a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AutoLearn.java +++ b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AutoLearn.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (c) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,11 +24,15 @@ package org.onap.policy.apex.examples.adaptive.concepts; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import lombok.EqualsAndHashCode; +import lombok.ToString; /** * The Class AutoLearn is used as a Java context for Adaptive auto-learning of trends towards a fixed value in the * adaptive domain. */ +@EqualsAndHashCode +@ToString public class AutoLearn implements Serializable { private static final long serialVersionUID = 3825970380434170754L; @@ -52,14 +57,14 @@ public class AutoLearn implements Serializable { public void init(final int size) { if (avDiffs == null || avDiffs.isEmpty()) { avDiffs = new ArrayList<>(size); - for (int i = 0; i < size; i++) { + for (var i = 0; i < size; i++) { avDiffs.add(i, Double.NaN); } } if (counts == null || counts.isEmpty()) { counts = new ArrayList<>(size); - for (int i = 0; i < size; i++) { + for (var i = 0; i < size; i++) { counts.add(i, 0L); } } @@ -133,55 +138,5 @@ public class AutoLearn implements Serializable { counts = null; } - /** - * {@inheritDoc}. - */ - @Override - public String toString() { - return "AutoLearn [avDiffs=" + avDiffs + ", counts=" + counts + "]"; - } - /** - * {@inheritDoc}. - */ - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((avDiffs == null) ? 0 : avDiffs.hashCode()); - result = prime * result + ((counts == null) ? 0 : counts.hashCode()); - return result; - } - - /** - * {@inheritDoc}. - */ - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final AutoLearn other = (AutoLearn) obj; - if (avDiffs == null) { - if (other.avDiffs != null) { - return false; - } - } else if (!avDiffs.equals(other.avDiffs)) { - return false; - } - if (counts == null) { - if (other.counts != null) { - return false; - } - } else if (!counts.equals(other.counts)) { - return false; - } - return true; - } } diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java index 6c1998108..6b61e822b 100644 --- a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java +++ b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2020-2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -78,7 +78,7 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { public boolean getTask(final TaskSelectionExecutionContext executor) { String id = executor.subject.getId(); executor.logger.debug(id); - String inFields = executor.inFields.toString(); + var inFields = executor.inFields.toString(); executor.logger.debug(inFields); final double now = (Double) (executor.inFields.get("MonitoredValue")); final Integer iteration = (Integer) (executor.inFields.get("Iteration")); @@ -120,7 +120,7 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { } // Get the context object - AnomalyDetection anomalyDetection = + var anomalyDetection = (AnomalyDetection) executor.getContextAlbum(ANOMALY_DETECTION_ALBUM).get(ANOMALY_DETECTION); if (anomalyDetection == null) { anomalyDetection = new AnomalyDetection(); @@ -132,7 +132,7 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { anomalyDetection.init(FREQUENCY); } - boolean unsetfirstround = false; + var unsetfirstround = false; int frequency = anomalyDetection.getFrequency(); frequency = frequency + 1; @@ -169,7 +169,7 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { listSizeControl(anomalyDetection.getAnomalyScores(), FREQUENCY * 4); // ---------- calculate the anomaly probability - double anomalyProbability = 0.0; + var anomalyProbability = 0.0; if (anomalyDetection.getAnomalyScores().size() > 30) { // 0.5 anomalyProbability = getStatsTest(anomalyDetection.getAnomalyScores(), ANOMALY_SENSITIVITY); @@ -282,10 +282,10 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { final double s = (z * z * n * (2.0 - n)) / (z * z * n - (n - 1.0) * (n - 1.0)); final double t = FastMath.sqrt(s); // default p value = 0 - double pvalue = 0.0; + var pvalue = 0.0; if (!Double.isNaN(t)) { // t distribution with n-2 degrees of freedom - final TDistribution tDist = new TDistribution(n - 2); + final var tDist = new TDistribution(n - 2); pvalue = n * (1.0 - tDist.cumulativeProbability(t)); // set max pvalue = 1 pvalue = pvalue > 1.0 ? 1.0 : pvalue; @@ -312,9 +312,9 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { * @return the double[] */ private static Double[] removevalue(final Double[] lvalues, final double val) { - for (int i = 0; i < lvalues.length; i++) { + for (var i = 0; i < lvalues.length; i++) { if (Double.compare(lvalues[i], val) == 0) { - final Double[] ret = new Double[lvalues.length - 1]; + final var ret = new Double[lvalues.length - 1]; System.arraycopy(lvalues, 0, ret, 0, i); System.arraycopy(lvalues, i + 1, ret, i, lvalues.length - i - 1); return ret; @@ -330,7 +330,7 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { * @return the mean */ private static double getMean(final Double[] lvalues) { - double sum = 0.0; + var sum = 0.0; for (final double d : lvalues) { sum += d; @@ -346,7 +346,7 @@ public class AnomalyDetectionPolicyDecideTaskSelectionLogic { * @return stddev */ private static double getStdDev(final Double[] lvalues, final double mean) { - double temp = 0.0; + var temp = 0.0; for (final double d : lvalues) { temp += (mean - d) * (mean - d); } diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicyDecideTaskSelectionLogic.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicyDecideTaskSelectionLogic.java index 1805d81a2..317a86349 100644 --- a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicyDecideTaskSelectionLogic.java +++ b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicyDecideTaskSelectionLogic.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (c) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,10 +52,10 @@ public class AutoLearnPolicyDecideTaskSelectionLogic { * @return the task */ public boolean getTask(final TaskSelectionExecutionContext executor) { - String idString = executor.subject.getId(); + var idString = executor.subject.getId(); executor.logger.debug(idString); - String inFieldsString = executor.inFields.toString(); + var inFieldsString = executor.inFields.toString(); executor.logger.debug(inFieldsString); final List tasks = executor.subject.getTaskNames(); @@ -68,7 +69,7 @@ public class AutoLearnPolicyDecideTaskSelectionLogic { } // Get the context object - AutoLearn autoLearn = (AutoLearn) executor.getContextAlbum(AUTO_LEARN_ALBUM).get(AUTO_LEARN); + var autoLearn = (AutoLearn) executor.getContextAlbum(AUTO_LEARN_ALBUM).get(AUTO_LEARN); if (autoLearn == null) { autoLearn = new AutoLearn(); } @@ -105,12 +106,12 @@ public class AutoLearnPolicyDecideTaskSelectionLogic { */ private int getOption(final double diff, final AutoLearn autoLearn) { final Double[] avdiffs = autoLearn.getAvDiffs().toArray(new Double[autoLearn.getAvDiffs().size()]); - final int r = RAND.nextInt(size); + final var r = RAND.nextInt(size); int closestupi = -1; int closestdowni = -1; double closestup = Double.MAX_VALUE; double closestdown = Double.MIN_VALUE; - for (int i = 0; i < size; i++) { + for (var i = 0; i < size; i++) { if (Double.isNaN(avdiffs[i])) { return r; } -- cgit 1.2.3-korg