f2e27725b45006dc1d7c49d2d7650ab7a4a32189
[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 List<Double> avDiffs = null;
35
36     private List<Long> counts = null;
37
38     /**
39      * The Constructor creates an AutoLearn concept.
40      */
41     public AutoLearn() {}
42
43     /**
44      * Checks if the Autolearn instance is initialized.
45      *
46      * @return true, if the Autolearn instance is initialized
47      */
48     public boolean isInitialized() {
49         return (avDiffs != null && counts != null);
50     }
51
52     /**
53      * initializes the auto learning algorithm with the number of convergent variables to use.
54      *
55      * @param size the number of convergent variables to use
56      */
57     public void init(final int size) {
58         if (avDiffs == null || avDiffs.size() == 0) {
59             avDiffs = new ArrayList<>(size);
60             for (int i = 0; i < size; i++) {
61                 avDiffs.add(i, Double.NaN);
62             }
63         }
64
65         if (counts == null || counts.size() == 0) {
66             counts = new ArrayList<>(size);
67             for (int i = 0; i < size; i++) {
68                 counts.add(i, 0L);
69             }
70         }
71     }
72
73     /**
74      * Gets the average difference values of the algorithm.
75      *
76      * @return the average difference values of the algorithm
77      */
78     public List<Double> getAvDiffs() {
79         return avDiffs;
80     }
81
82     /**
83      * Sets the average difference values of the algorithm.
84      *
85      * @param avDiffs the average difference values of the algorithm
86      */
87     public void setAvDiffs(final List<Double> avDiffs) {
88         this.avDiffs = avDiffs;
89     }
90
91     /**
92      * Check if the average difference values of the algorithm are set.
93      *
94      * @return true, if check set av diffs
95      */
96     public boolean checkSetAvDiffs() {
97         return ((avDiffs != null) && (!avDiffs.isEmpty()));
98     }
99
100     /**
101      * Unset the average difference values of the algorithm.
102      */
103     public void unsetAvDiffs() {
104         avDiffs = null;
105     }
106
107     /**
108      * Gets the count values of the algorithm.
109      *
110      * @return the count values of the algorithm
111      */
112     public List<Long> getCounts() {
113         return counts;
114     }
115
116     /**
117      * Sets the count values of the algorithm.
118      *
119      * @param counts the count values of the algorithm
120      */
121     public void setCounts(final List<Long> counts) {
122         this.counts = counts;
123     }
124
125     /**
126      * Check if the count values of the algorithm are set.
127      *
128      * @return true, if the count values of the algorithm are set
129      */
130     public boolean checkSetCounts() {
131         return ((counts != null) && (!counts.isEmpty()));
132     }
133
134     /**
135      * Unset the count values of the algorithm.
136      */
137     public void unsetCounts() {
138         counts = null;
139     }
140
141     /*
142      * (non-Javadoc)
143      *
144      * @see java.lang.Object#toString()
145      */
146     @Override
147     public String toString() {
148         return "AutoLearn [avDiffs=" + avDiffs + ", counts=" + counts + "]";
149     }
150
151     /*
152      * (non-Javadoc)
153      *
154      * @see java.lang.Object#hashCode()
155      */
156     @Override
157     public int hashCode() {
158         final int prime = 31;
159         int result = 1;
160         result = prime * result + ((avDiffs == null) ? 0 : avDiffs.hashCode());
161         result = prime * result + ((counts == null) ? 0 : counts.hashCode());
162         return result;
163     }
164
165     /*
166      * (non-Javadoc)
167      *
168      * @see java.lang.Object#equals(java.lang.Object)
169      */
170     @Override
171     public boolean equals(final Object obj) {
172         if (this == obj) {
173             return true;
174         }
175         if (obj == null) {
176             return false;
177         }
178         if (getClass() != obj.getClass()) {
179             return false;
180         }
181         final AutoLearn other = (AutoLearn) obj;
182         if (avDiffs == null) {
183             if (other.avDiffs != null) {
184                 return false;
185             }
186         } else if (!avDiffs.equals(other.avDiffs)) {
187             return false;
188         }
189         if (counts == null) {
190             if (other.counts != null) {
191                 return false;
192             }
193         } else if (!counts.equals(other.counts)) {
194             return false;
195         }
196         return true;
197     }
198 }