16e467041c4e96fc3ea587196d47e36ebb125163
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.rest.util;
22
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
33
34 public class PolicyItemSetChangeNotifier implements PolicyContainer.ItemSetChangeNotifier {
35     private static final long serialVersionUID = 1L;
36     private Collection<PolicyContainer.ItemSetChangeListener> itemSetChangeListeners = null;
37     private PolicyContainer container = null;
38     
39     public PolicyItemSetChangeNotifier() {
40         // Empty constructor
41     }
42     
43     protected void setContainer(PolicyContainer c) {
44         this.container = c;
45     }
46
47     @Override
48     public void addItemSetChangeListener(ItemSetChangeListener listener) {
49         if (getItemSetChangeListeners() == null) {
50             setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
51         }
52         getItemSetChangeListeners().add(listener);      }
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 implements
62         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(
76             Collection<PolicyContainer.ItemSetChangeListener> itemSetChangeListeners) {
77         this.itemSetChangeListeners = itemSetChangeListeners;
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])
92                         .containerItemSetChange(event);
93             }
94         }
95     }
96 }