Unit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / util / PolicyItemSetChangeNotifier.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.rest.util;
23
24 import java.io.Serializable;
25 import java.util.Collection;
26 import java.util.EventObject;
27 import java.util.LinkedList;
28
29 import org.onap.policy.rest.util.PolicyContainer.ItemSetChangeEvent;
30 import org.onap.policy.rest.util.PolicyContainer.ItemSetChangeListener;
31
32 public class PolicyItemSetChangeNotifier implements PolicyContainer.ItemSetChangeNotifier {
33     private static final long serialVersionUID = 1L;
34
35     private Collection<PolicyContainer.ItemSetChangeListener> itemSetChangeListeners = null;
36     private PolicyContainer container = null;
37
38     public PolicyItemSetChangeNotifier() {
39         // Empty constructor
40     }
41
42     protected void setContainer(PolicyContainer container) {
43         this.container = container;
44     }
45
46     @Override
47     public void addItemSetChangeListener(ItemSetChangeListener listener) {
48         if (getItemSetChangeListeners() == null) {
49             setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
50         }
51         getItemSetChangeListeners().add(listener);
52     }
53
54     @Override
55     public void removeItemSetChangeListener(ItemSetChangeListener listener) {
56         if (getItemSetChangeListeners() != null) {
57             getItemSetChangeListeners().remove(listener);
58         }
59     }
60
61     protected static class BaseItemSetChangeEvent extends EventObject
62                     implements PolicyContainer.ItemSetChangeEvent, Serializable {
63         private static final long serialVersionUID = 1L;
64
65         protected BaseItemSetChangeEvent(PolicyContainer source) {
66             super(source);
67         }
68
69         @Override
70         public PolicyContainer getContainer() {
71             return (PolicyContainer) getSource();
72         }
73     }
74
75     protected void setItemSetChangeListeners(Collection<PolicyContainer.ItemSetChangeListener> itemSetChangeListeners) {
76         this.itemSetChangeListeners = itemSetChangeListeners;
77     }
78
79     protected Collection<PolicyContainer.ItemSetChangeListener> getItemSetChangeListeners() {
80         return itemSetChangeListeners;
81     }
82
83     protected void fireItemSetChange() {
84         fireItemSetChange(new BaseItemSetChangeEvent(this.container));
85     }
86
87     protected void fireItemSetChange(ItemSetChangeEvent event) {
88         if (getItemSetChangeListeners() != null) {
89             final Object[] l = getItemSetChangeListeners().toArray();
90             for (int i = 0; i < l.length; i++) {
91                 ((PolicyContainer.ItemSetChangeListener) l[i]).containerItemSetChange(event);
92             }
93         }
94     }
95 }