dd5bf0c45d0b8c2b969db23bf96ed02c7d30ddd4
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.examples.adaptive.concepts;
22
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 /**
28  * The Class AutoLearn is used as a Java context for Adaptive auto-learning of trends towards a fixed value in the
29  * adaptive domain.
30  */
31 public class AutoLearn implements Serializable {
32     private static final long serialVersionUID = 3825970380434170754L;
33
34     private transient List<Double> avDiffs = null;
35
36     private transient List<Long> counts = null;
37
38     /**
39      * Checks if the Autolearn instance is initialized.
40      *
41      * @return true, if the Autolearn instance is initialized
42      */
43     public boolean isInitialized() {
44         return (avDiffs != null && counts != null);
45     }
46
47     /**
48      * initializes the auto learning algorithm with the number of convergent variables to use.
49      *
50      * @param size the number of convergent variables to use
51      */
52     public void init(final int size) {
53         if (avDiffs == null || avDiffs.isEmpty()) {
54             avDiffs = new ArrayList<>(size);
55             for (int i = 0; i < size; i++) {
56                 avDiffs.add(i, Double.NaN);
57             }
58         }
59
60         if (counts == null || counts.isEmpty()) {
61             counts = new ArrayList<>(size);
62             for (int i = 0; i < size; i++) {
63                 counts.add(i, 0L);
64             }
65         }
66     }
67
68     /**
69      * Gets the average difference values of the algorithm.
70      *
71      * @return the average difference values of the algorithm
72      */
73     public List<Double> getAvDiffs() {
74         return avDiffs;
75     }
76
77     /**
78      * Sets the average difference values of the algorithm.
79      *
80      * @param avDiffs the average difference values of the algorithm
81      */
82     public void setAvDiffs(final List<Double> avDiffs) {
83         this.avDiffs = avDiffs;
84     }
85
86     /**
87      * Check if the average difference values of the algorithm are set.
88      *
89      * @return true, if check set av diffs
90      */
91     public boolean checkSetAvDiffs() {
92         return ((avDiffs != null) && (!avDiffs.isEmpty()));
93     }
94
95     /**
96      * Unset the average difference values of the algorithm.
97      */
98     public void unsetAvDiffs() {
99         avDiffs = null;
100     }
101
102     /**
103      * Gets the count values of the algorithm.
104      *
105      * @return the count values of the algorithm
106      */
107     public List<Long> getCounts() {
108         return counts;
109     }
110
111     /**
112      * Sets the count values of the algorithm.
113      *
114      * @param counts the count values of the algorithm
115      */
116     public void setCounts(final List<Long> counts) {
117         this.counts = counts;
118     }
119
120     /**
121      * Check if the count values of the algorithm are set.
122      *
123      * @return true, if the count values of the algorithm are set
124      */
125     public boolean checkSetCounts() {
126         return ((counts != null) && (!counts.isEmpty()));
127     }
128
129     /**
130      * Unset the count values of the algorithm.
131      */
132     public void unsetCounts() {
133         counts = null;
134     }
135
136     /**
137      * {@inheritDoc}.
138      */
139     @Override
140     public String toString() {
141         return "AutoLearn [avDiffs=" + avDiffs + ", counts=" + counts + "]";
142     }
143
144     /**
145      * {@inheritDoc}.
146      */
147     @Override
148     public int hashCode() {
149         final int prime = 31;
150         int result = 1;
151         result = prime * result + ((avDiffs == null) ? 0 : avDiffs.hashCode());
152         result = prime * result + ((counts == null) ? 0 : counts.hashCode());
153         return result;
154     }
155
156     /**
157      * {@inheritDoc}.
158      */
159     @Override
160     public boolean equals(final Object obj) {
161         if (this == obj) {
162             return true;
163         }
164         if (obj == null) {
165             return false;
166         }
167         if (getClass() != obj.getClass()) {
168             return false;
169         }
170         final AutoLearn other = (AutoLearn) obj;
171         if (avDiffs == null) {
172             if (other.avDiffs != null) {
173                 return false;
174             }
175         } else if (!avDiffs.equals(other.avDiffs)) {
176             return false;
177         }
178         if (counts == null) {
179             if (other.counts != null) {
180                 return false;
181             }
182         } else if (!counts.equals(other.counts)) {
183             return false;
184         }
185         return true;
186     }
187 }