2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.examples.adaptive.concepts;
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.LinkedList;
26 import java.util.List;
29 * The Class AnomalyDetection is used as a Java context for Adaptive anomaly detection in the adaptive domain.
31 public class AnomalyDetection implements Serializable {
32 private static final long serialVersionUID = -823013127095523727L;
34 private static final int HASH_PRIME_1 = 31;
35 private static final int HASH_PRIME_2 = 1231;
36 private static final int HASH_PRIME_3 = 1237;
38 private boolean firstRound = true;
39 private int frequency = 0;
42 * The Constructor creates an AnomalyDetection instance.
44 public AnomalyDetection() {
49 private List<Double> anomalyScores = new LinkedList<>();
50 private List<Double> frequencyForecasted;
53 * Checks if the AnomalyDetection instance is initialized.
55 * @return true, if the AnomalyDetection instance is initialized
57 public boolean isInitialized() {
58 return (frequencyForecasted != null);
62 * Initializes the AnomalyDetection instance.
64 * @param incomingFrequency the frequency
66 public void init(final int incomingFrequency) {
67 frequencyForecasted = new ArrayList<>(incomingFrequency);
68 for (int i = 0; i < incomingFrequency; i++) {
69 frequencyForecasted.add(null);
74 * Indicates if this is the first round of the algorithm.
76 * @return true if this is the first round of the algorithm
78 public boolean getFirstRound() {
83 * Sets the first round indicator of the algorithm.
85 * @param firstRound the first round indicator of the algorithm
87 public void setFirstRound(final boolean firstRound) {
88 this.firstRound = firstRound;
92 * Gets the frequency value of the algorithm.
94 * @return the frequency value of the algorithm
96 public int getFrequency() {
101 * Sets the frequency value of the algorithm.
103 * @param frequency the frequency value of the algorithm
105 public void setFrequency(final int frequency) {
106 this.frequency = frequency;
110 * Gets the anomaly score values of the algorithm.
112 * @return the anomaly score values of the algorithm
114 public List<Double> getAnomalyScores() {
115 return anomalyScores;
119 * Sets the anomaly score values of the algorithm.
121 * @param anomalyScores the anomaly score values of the algorithm
123 public void setAnomalyScores(final LinkedList<Double> anomalyScores) {
124 this.anomalyScores = anomalyScores;
128 * Check if the anomaly score values of the algorithm are set.
130 * @return true, if the anomaly score values of the algorithm are set
132 public boolean checkSetAnomalyScores() {
133 return ((anomalyScores != null) && (!anomalyScores.isEmpty()));
137 * Unset the anomaly score values of the algorithm.
139 public void unsetAnomalyScores() {
140 anomalyScores = null;
144 * Gets the frequency forecasted by the algorithm.
146 * @return the frequency forecasted by the algorithm
148 public List<Double> getFrequencyForecasted() {
149 return frequencyForecasted;
153 * Sets the frequency forecasted by the algorithm.
155 * @param frequencyForecasted the frequency forecasted by the algorithm
157 public void setFrequencyForecasted(final List<Double> frequencyForecasted) {
158 this.frequencyForecasted = frequencyForecasted;
162 * Check if the frequency forecasted by the algorithm is set.
164 * @return true, if the frequency forecasted by the algorithm is set
166 public boolean checkSetFrequencyForecasted() {
167 return ((frequencyForecasted != null) && (!frequencyForecasted.isEmpty()));
171 * Unset the frequency forecasted by the algorithm.
173 public void unsetFrequencyForecasted() {
174 frequencyForecasted = null;
180 * @see java.lang.Object#toString()
183 public String toString() {
184 return "AnomalyDetection [firstRound=" + firstRound + ", frequency=" + frequency + ", anomalyScores="
185 + anomalyScores + ", frequencyForecasted=" + frequencyForecasted + "]";
191 * @see java.lang.Object#hashCode()
194 public int hashCode() {
195 final int prime = HASH_PRIME_1;
197 result = prime * result + ((anomalyScores == null) ? 0 : anomalyScores.hashCode());
198 result = prime * result + (firstRound ? HASH_PRIME_2 : HASH_PRIME_3);
199 result = prime * result + frequency;
200 result = prime * result + ((frequencyForecasted == null) ? 0 : frequencyForecasted.hashCode());
207 * @see java.lang.Object#equals(java.lang.Object)
210 public boolean equals(final Object obj) {
217 if (getClass() != obj.getClass()) {
220 final AnomalyDetection other = (AnomalyDetection) obj;
221 if (anomalyScores == null) {
222 if (other.anomalyScores != null) {
225 } else if (!anomalyScores.equals(other.anomalyScores)) {
228 if (firstRound != other.firstRound) {
231 if (frequency != other.frequency) {
234 if (frequencyForecasted == null) {
235 if (other.frequencyForecasted != null) {
238 } else if (!frequencyForecasted.equals(other.frequencyForecasted)) {