968b20499e3fc16cba5c88426abb39d7eff2b3aa
[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      * (non-Javadoc)
138      *
139      * @see java.lang.Object#toString()
140      */
141     @Override
142     public String toString() {
143         return "AutoLearn [avDiffs=" + avDiffs + ", counts=" + counts + "]";
144     }
145
146     /*
147      * (non-Javadoc)
148      *
149      * @see java.lang.Object#hashCode()
150      */
151     @Override
152     public int hashCode() {
153         final int prime = 31;
154         int result = 1;
155         result = prime * result + ((avDiffs == null) ? 0 : avDiffs.hashCode());
156         result = prime * result + ((counts == null) ? 0 : counts.hashCode());
157         return result;
158     }
159
160     /*
161      * (non-Javadoc)
162      *
163      * @see java.lang.Object#equals(java.lang.Object)
164      */
165     @Override
166     public boolean equals(final Object obj) {
167         if (this == obj) {
168             return true;
169         }
170         if (obj == null) {
171             return false;
172         }
173         if (getClass() != obj.getClass()) {
174             return false;
175         }
176         final AutoLearn other = (AutoLearn) obj;
177         if (avDiffs == null) {
178             if (other.avDiffs != null) {
179                 return false;
180             }
181         } else if (!avDiffs.equals(other.avDiffs)) {
182             return false;
183         }
184         if (counts == null) {
185             if (other.counts != null) {
186                 return false;
187             }
188         } else if (!counts.equals(other.counts)) {
189             return false;
190         }
191         return true;
192     }
193 }