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