7d7991787e4386e4a84b1e711f31adc14d80cb63
[policy/engine.git] / ECOMP-ControlloopPolicy / src / main / java / org / openecomp / policy / controlloop / policy / guard / Constraint.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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 package org.openecomp.policy.controlloop.policy.guard;
21
22 import java.util.Collections;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26
27 public class Constraint {
28
29         public Integer num;
30         public String duration;
31         public Map<String, String> time_in_range;
32         
33         public LinkedList<String> blacklist;
34         
35         public Constraint() {
36                 
37         }
38         
39         public Constraint(Integer num, String duration) {
40                 this.num = num;
41                 this.duration = duration;
42         }
43         
44         public Constraint(List<String> blacklist) {
45                 this.blacklist = new LinkedList<String>(blacklist);
46                 
47         }
48         
49         public Constraint(Integer num, String duration, Map<String, String> time_in_range) {
50                 this(num, duration);
51                 if (time_in_range != null) {
52                         this.time_in_range = Collections.unmodifiableMap(time_in_range);
53                 }
54         }
55         
56         public Constraint(Integer num, String duration, List<String> blacklist) {
57                 this.num = num;
58                 this.duration = duration;
59                 this.blacklist = new LinkedList<String>(blacklist);
60         }
61         
62         public Constraint(Integer num, String duration, Map<String, String> time_in_range, List<String> blacklist) {
63                 this(num, duration);
64                 if (time_in_range != null) {
65                         this.time_in_range = Collections.unmodifiableMap(time_in_range);
66                 }
67                 this.blacklist = new LinkedList<String>(blacklist);
68         }
69         
70         public Constraint(Constraint constraint) {
71                 this.num = constraint.num;
72                 this.duration = constraint.duration;
73                 if (constraint.time_in_range != null) {
74                         this.time_in_range = Collections.unmodifiableMap(constraint.time_in_range);
75                 }
76                 this.blacklist = new LinkedList<String>(constraint.blacklist);
77         }
78         
79         public boolean isValid() {
80                 try {
81                         if (num == null && duration != null) {
82                                 throw new NullPointerException();
83                         }
84                         if (duration == null && num != null) {
85                                 throw new NullPointerException();
86                         }
87                 } catch (Exception e) {
88                         return false;
89                 }
90                 return true;
91         }
92         
93         @Override
94         public String toString() {
95                 return "Constraint [num=" + num + ", duration=" + duration + ", time_in_range=" + time_in_range + ", blacklist=" + blacklist + "]";
96         }
97
98         @Override
99         public int hashCode() {
100                 final int prime = 31;
101                 int result = 1;
102                 result = prime * result + ((num == null) ? 0 : num.hashCode());
103                 result = prime * result + ((duration == null) ? 0 : duration.hashCode());
104                 result = prime * result + ((time_in_range == null) ? 0 : time_in_range.hashCode());
105                 result = prime * result + ((blacklist == null) ? 0 : blacklist.hashCode());
106                 return result;
107         }
108
109         @Override
110         public boolean equals(Object obj) {
111                 if (this == obj)
112                         return true;
113                 if (obj == null)
114                         return false;
115                 if (getClass() != obj.getClass())
116                         return false;
117                 Constraint other = (Constraint) obj;
118                 if (num == null) {
119                         if (other.num != null) 
120                                 return false;
121                 } else if (!num.equals(other.num))
122                         return false;
123                 if (duration == null) {
124                         if (other.duration != null)
125                                 return false;
126                 } else if (!duration.equals(other.duration))
127                         return false;
128                 if (time_in_range == null) {
129                         if (other.time_in_range != null)
130                                 return false;
131                 } else if (!time_in_range.equals(other.time_in_range))
132                         return false;
133                 if (blacklist == null) {
134                         if (other.blacklist != null)
135                                 return false;
136                 } else if (!blacklist.equals(other.blacklist))
137                         return false;
138                 return true;
139         }
140 }