e53f3e40f06bb04ff16a0ea9c790019f2d65c215
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml
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.controlloop.policy.guard;
22
23 import java.util.Collections;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
27
28 public class Constraint {
29
30     private Integer freqLimitPerTarget;
31     private Map<String,String> timeWindow;
32     private Map<String, String> activeTimeRange;
33     
34     private List<String> blacklist;
35     
36     public Constraint() {
37         // Do Nothing empty constructor. 
38     }
39
40     public Integer getFreq_limit_per_target() {
41         return freqLimitPerTarget;
42     }
43
44
45     public void setFreq_limit_per_target(Integer freqLimitPerTarget) {
46         this.freqLimitPerTarget = freqLimitPerTarget;
47     }
48
49
50     public Map<String, String> getTime_window() {
51         return timeWindow;
52     }
53
54
55     public void setTime_window(Map<String, String> timeWindow) {
56         this.timeWindow = timeWindow;
57     }
58
59
60     public Map<String, String> getActive_time_range() {
61         return activeTimeRange;
62     }
63
64
65     public void setActive_time_range(Map<String, String> activeTimeRange) {
66         this.activeTimeRange = activeTimeRange;
67     }
68
69
70     public List<String> getBlacklist() {
71         return blacklist;
72     }
73
74     public void setBlacklist(List<String> blacklist) {
75         this.blacklist = blacklist;
76     }
77
78     public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow) {
79         this.freqLimitPerTarget = freqLimitPerTarget;
80         if(timeWindow!=null){
81             this.timeWindow = Collections.unmodifiableMap(timeWindow);
82         }
83     }
84     
85     public Constraint(List<String> blacklist) {
86         this.blacklist = new LinkedList<>(blacklist);
87     }
88     
89     public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow, List<String> blacklist) {
90         this.freqLimitPerTarget = freqLimitPerTarget;
91         this.timeWindow = Collections.unmodifiableMap(timeWindow);
92         this.blacklist = new LinkedList<>(blacklist);
93     }
94     
95     public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow, Map<String, String> activeTimeRange) {
96         this(freqLimitPerTarget, timeWindow);
97         if (activeTimeRange != null) {
98             this.activeTimeRange = Collections.unmodifiableMap(activeTimeRange);
99         }
100     }
101     
102     public Constraint(Integer freqLimitPerTarget, Map<String, String> timeWindow, Map<String, String> activeTimeRange, 
103                     List<String> blacklist) {
104         this(freqLimitPerTarget, timeWindow);
105         if (activeTimeRange != null) {
106             this.activeTimeRange = Collections.unmodifiableMap(activeTimeRange);
107         }
108         if(blacklist != null){
109             this.blacklist = new LinkedList<>(blacklist);
110         }
111     }
112     
113     public Constraint(Constraint constraint) {
114         this.freqLimitPerTarget = constraint.freqLimitPerTarget;
115         this.timeWindow = constraint.timeWindow;
116         if (constraint.activeTimeRange != null) {
117             this.activeTimeRange = Collections.unmodifiableMap(constraint.activeTimeRange);
118         }
119         this.blacklist = new LinkedList<>(constraint.blacklist);
120     }
121     
122     public boolean isValid() {
123         return ((freqLimitPerTarget == null && timeWindow != null)
124                         || (timeWindow == null && freqLimitPerTarget != null)) ? false : true;
125     }
126     
127     @Override
128     public String toString() {
129         return "Constraint [freq_limit_per_target=" + freqLimitPerTarget + ", time_window=" 
130                + timeWindow + ", active_time_range=" + activeTimeRange + ", blacklist=" + blacklist + "]";
131     }
132
133     @Override
134     public int hashCode() {
135         final int prime = 31;
136         int result = 1;
137         result = prime * result + ((freqLimitPerTarget == null) ? 0 : freqLimitPerTarget.hashCode());
138         result = prime * result + ((timeWindow == null) ? 0 : timeWindow.hashCode());
139         result = prime * result + ((activeTimeRange == null) ? 0 : activeTimeRange.hashCode());
140         result = prime * result + ((blacklist == null) ? 0 : blacklist.hashCode());
141         return result;
142     }
143
144     @Override
145     public boolean equals(Object obj) {
146         if (this == obj)
147             return true;
148         if (obj == null)
149             return false;
150         if (getClass() != obj.getClass())
151             return false;
152         Constraint other = (Constraint) obj;
153         return equalsMayBeNull(freqLimitPerTarget, other.freqLimitPerTarget)
154                         && equalsMayBeNull(timeWindow, other.timeWindow)
155                         && equalsMayBeNull(activeTimeRange, other.activeTimeRange)
156                         && equalsMayBeNull(blacklist, other.blacklist);
157     }
158     
159     private boolean equalsMayBeNull(final Object obj1, final Object obj2){
160         if ( obj1 == null ) {
161             return obj2 == null;
162         } 
163         return obj1.equals(obj2);
164     }
165 }