re base code
[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.util.List;
27
28 public class PropertyRule extends ToscaDataDefinition {
29
30         public final static String FORCE_ALL = "FORCE_ALL";
31         public final static String ALL = "ALL";
32         public final static String RULE_ANY_MATCH = ".+";
33
34         List<String> rule;
35         String value;
36
37         public PropertyRule() {
38                 super();
39         }
40
41         public PropertyRule(List<String> rule, String value) {
42                 super();
43                 this.rule = rule;
44                 this.value = value;
45         }
46
47         public List<String> getRule() {
48                 return rule;
49         }
50
51         public void setRule(List<String> rule) {
52                 this.rule = rule;
53         }
54
55         public String getValue() {
56                 return value;
57         }
58
59         public void setValue(String value) {
60                 this.value = value;
61         }
62
63         @JsonIgnore
64         public String getFirstToken() {
65                 return getToken(1);
66         }
67
68         public String getToken(int tokenNumber) {
69
70                 int index = tokenNumber - 1;
71                 if (rule == null || index >= rule.size() || index < 0) {
72                         return null;
73                 }
74
75                 return rule.get(index);
76         }
77
78         @JsonIgnore
79         public int getRuleSize() {
80                 if (rule == null) {
81                         return 0;
82                 }
83                 return rule.size();
84         }
85
86         @Override
87         public String toString() {
88                 return "PropertyRule [rule=" + rule + ", value=" + value + "]";
89         }
90
91         public boolean compareRule(PropertyRule comparedPropertyRule) {
92
93                 if (comparedPropertyRule == null) {
94                         return false;
95                 }
96
97                 List<String> comparedRule = comparedPropertyRule.getRule();
98                 if (rule == null && comparedRule == null) {
99                         return true;
100                 }
101
102                 if (rule != null && comparedRule != null) {
103                         if (rule.size() != comparedRule.size()) {
104                                 return false;
105                         } else {
106                                 int size = rule.size();
107                                 boolean isEqual = true;
108                                 for (int i = 0; i < size; i++) {
109                                         String item = rule.get(i);
110                                         String comparedItem = comparedRule.get(i);
111                                         if (item == null || !item.equals(comparedItem)) {
112                                                 isEqual = false;
113                                                 break;
114                                         }
115                                 }
116                                 return isEqual;
117                         }
118                 } else {
119                         return false;
120                 }
121
122         }
123
124         public void replaceFirstToken(String token) {
125
126                 if (rule != null && rule.size() > 0) {
127                         rule.set(0, token);
128                 }
129
130         }
131
132 }