e13318089c7d6d0532123a1be59427ca67edd572
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml
4  * ================================================================================
5  * Copyright (C) 2017 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         public Integer freq_limit_per_target;
31         public Map<String, String> time_window;
32         public Map<String, String> active_time_range;
33         
34         public LinkedList<String> blacklist;
35         
36         public Constraint() {
37                 
38         }
39         
40         public Constraint(Integer freq_limit_per_target, Map<String, String> time_window) {
41                 this.freq_limit_per_target = freq_limit_per_target;
42                 if (time_window != null) {
43                         this.time_window = Collections.unmodifiableMap(time_window);
44                 }
45         }
46         
47         public Constraint(List<String> blacklist) {
48                 this.blacklist = new LinkedList<String>(blacklist);
49                 
50         }
51         
52         public Constraint(Integer freq_limit_per_target, Map<String, String> time_window, List<String> blacklist) {
53                 this.freq_limit_per_target = freq_limit_per_target;
54                 this.time_window = Collections.unmodifiableMap(time_window);
55                 this.blacklist = new LinkedList<String>(blacklist);
56         }
57         
58         public Constraint(Integer freq_limit_per_target, Map<String, String> time_window, Map<String, String> active_time_range, List<String> blacklist) {
59                 this(freq_limit_per_target, time_window);
60                 if (active_time_range != null) {
61                         this.active_time_range = Collections.unmodifiableMap(active_time_range);
62                 }
63                 this.blacklist = new LinkedList<String>(blacklist);
64         }
65         
66         public Constraint(Integer freq_limit_per_target, Map<String, String> time_window, Map<String, String> active_time_range) {
67                 this(freq_limit_per_target, time_window);
68                 if (active_time_range != null) {
69                         this.active_time_range = Collections.unmodifiableMap(active_time_range);
70                 }
71         }
72         
73         public Constraint(Constraint constraint) {
74                 this.freq_limit_per_target = constraint.freq_limit_per_target;
75                 this.time_window = constraint.time_window;
76                 if (constraint.active_time_range != null) {
77                         this.active_time_range = Collections.unmodifiableMap(constraint.active_time_range);
78                 }
79                 this.blacklist = new LinkedList<String>(constraint.blacklist);
80         }
81         
82         public boolean isValid() {
83                 //System.out.println("freq_limit_per_target: " + freq_limit_per_target + " time_window" + time_window );
84                 try {
85                         if (freq_limit_per_target == null && time_window != null) {
86                                 throw new NullPointerException();
87                         }
88                         if (time_window == null && freq_limit_per_target != null) {
89                                 throw new NullPointerException();
90                         }
91                 } catch (Exception e) {
92                         return false;
93                 }
94                 return true;
95         }
96         
97         @Override
98         public String toString() {
99                 return "Constraint [freq_limit_per_target=" + freq_limit_per_target + ", time_window=" + time_window + ", active_time_range=" + active_time_range + ", blacklist=" + blacklist + "]";
100         }
101
102         @Override
103         public int hashCode() {
104                 final int prime = 31;
105                 int result = 1;
106                 result = prime * result + ((freq_limit_per_target == null) ? 0 : freq_limit_per_target.hashCode());
107                 result = prime * result + ((time_window == null) ? 0 : time_window.hashCode());
108                 result = prime * result + ((active_time_range == null) ? 0 : active_time_range.hashCode());
109                 result = prime * result + ((blacklist == null) ? 0 : blacklist.hashCode());
110                 return result;
111         }
112
113         @Override
114         public boolean equals(Object obj) {
115                 if (this == obj)
116                         return true;
117                 if (obj == null)
118                         return false;
119                 if (getClass() != obj.getClass())
120                         return false;
121                 Constraint other = (Constraint) obj;
122                 if (freq_limit_per_target == null) {
123                         if (other.freq_limit_per_target != null) 
124                                 return false;
125                 } else if (!freq_limit_per_target.equals(other.freq_limit_per_target))
126                         return false;
127                 if (time_window == null) {
128                         if (other.time_window != null)
129                                 return false;
130                 } else if (!time_window.equals(other.time_window))
131                         return false;
132                 if (active_time_range == null) {
133                         if (other.active_time_range != null)
134                                 return false;
135                 } else if (!active_time_range.equals(other.active_time_range))
136                         return false;
137                 if (blacklist == null) {
138                         if (other.blacklist != null)
139                                 return false;
140                 } else if (!blacklist.equals(other.blacklist))
141                         return false;
142                 return true;
143         }
144 }