Sync Integ to Master
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / PropertyRule.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.openecomp.sdc.be.datatypes.elements;
22
23 import com.fasterxml.jackson.annotation.JsonIgnore;
24 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
25
26 import java.io.Serializable;
27 import java.util.List;
28
29 public class PropertyRule extends ToscaDataDefinition implements Serializable {
30
31         /**
32          * 
33          */
34         private static final long serialVersionUID = -3357933382124599996L;
35
36         public static String FORCE_ALL = "FORCE_ALL";
37         public static String ALL = "ALL";
38         public static String RULE_ANY_MATCH = ".+";
39
40         List<String> rule;
41         String value;
42
43         public PropertyRule() {
44                 super();
45         }
46
47         public PropertyRule(List<String> rule, String value) {
48                 super();
49                 this.rule = rule;
50                 this.value = value;
51         }
52
53         public List<String> getRule() {
54                 return rule;
55         }
56
57         public void setRule(List<String> rule) {
58                 this.rule = rule;
59         }
60
61         public String getValue() {
62                 return value;
63         }
64
65         public void setValue(String value) {
66                 this.value = value;
67         }
68
69         @JsonIgnore
70         public String getFirstToken() {
71                 return getToken(1);
72         }
73
74         public String getToken(int tokenNumber) {
75
76                 int index = tokenNumber - 1;
77                 if (rule == null || index >= rule.size() || index < 0) {
78                         return null;
79                 }
80
81                 return rule.get(index);
82         }
83
84         @JsonIgnore
85         public int getRuleSize() {
86                 if (rule == null) {
87                         return 0;
88                 }
89                 return rule.size();
90         }
91
92         @Override
93         public String toString() {
94                 return "PropertyRule [rule=" + rule + ", value=" + value + "]";
95         }
96
97         public boolean compareRule(PropertyRule comparedPropertyRule) {
98
99                 if (comparedPropertyRule == null) {
100                         return false;
101                 }
102
103                 List<String> comparedRule = comparedPropertyRule.getRule();
104                 if (rule == null && comparedRule == null) {
105                         return true;
106                 }
107
108                 if (rule != null && comparedRule != null) {
109                         if (rule.size() != comparedRule.size()) {
110                                 return false;
111                         } else {
112                                 int size = rule.size();
113                                 boolean isEqual = true;
114                                 for (int i = 0; i < size; i++) {
115                                         String item = rule.get(i);
116                                         String comparedItem = comparedRule.get(i);
117                                         if (item == null || false == item.equals(comparedItem)) {
118                                                 isEqual = false;
119                                                 break;
120                                         }
121                                 }
122                                 return isEqual;
123                         }
124                 } else {
125                         return false;
126                 }
127
128         }
129
130         public void replaceFirstToken(String token) {
131
132                 if (rule != null && rule.size() > 0) {
133                         rule.set(0, token);
134                 }
135
136         }
137
138 }