re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / constraints / InRangeConstraint.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.model.tosca.constraints;
22
23 import com.google.common.collect.Lists;
24 import org.openecomp.sdc.be.model.tosca.ToscaType;
25 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
26 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
27
28 import javax.validation.constraints.NotNull;
29 import java.util.List;
30
31 public class InRangeConstraint extends AbstractPropertyConstraint {
32
33     private List<String> inRange;
34
35     private Comparable min;
36     private Comparable max;
37
38     public InRangeConstraint(List<String> inRange) {
39         this.inRange = inRange;
40     }
41
42     public InRangeConstraint() { }
43
44     @Override
45     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
46         // Perform verification that the property type is supported for
47         // comparison
48         ConstraintUtil.checkComparableType(propertyType);
49         if (inRange == null || inRange.size() != 2) {
50             throw new ConstraintValueDoNotMatchPropertyTypeException("In range constraint must have two elements.");
51         }
52         String minRawText = inRange.get(0);
53         String maxRawText = inRange.get(1);
54         if (!propertyType.isValidValue(minRawText)) {
55             throw new ConstraintValueDoNotMatchPropertyTypeException("Invalid min value for in range constraint ["
56                     + minRawText + "] as it does not follow the property type [" + propertyType + "]");
57         }
58         if (!propertyType.isValidValue(maxRawText)) {
59             throw new ConstraintValueDoNotMatchPropertyTypeException("Invalid max value for in range constraint ["
60                     + maxRawText + "] as it does not follow the property type [" + propertyType + "]");
61         }
62         min = ConstraintUtil.convertToComparable(propertyType, minRawText);
63         max = ConstraintUtil.convertToComparable(propertyType, maxRawText);
64     }
65
66     @Override
67     public void validate(Object propertyValue) throws ConstraintViolationException {
68         if (propertyValue == null) {
69             throw new ConstraintViolationException("Value to check is null");
70         }
71         if (!(min.getClass().isAssignableFrom(propertyValue.getClass()))) {
72             throw new ConstraintViolationException("Value to check is not comparable to range type, value type ["
73                     + propertyValue.getClass() + "], range type [" + min.getClass() + "]");
74         }
75         if (min.compareTo(propertyValue) > 0 || max.compareTo(propertyValue) < 0) {
76             throw new ConstraintViolationException("The value [" + propertyValue + "] is out of range " + inRange);
77         }
78     }
79
80     @NotNull
81     public String getRangeMinValue() {
82         if (inRange != null) {
83             return inRange.get(0);
84         } else {
85             return null;
86         }
87     }
88
89     public void setRangeMinValue(String minValue) {
90         if (inRange == null) {
91             inRange = Lists.newArrayList(minValue, "");
92         } else {
93             inRange.set(0, minValue);
94         }
95     }
96
97     @NotNull
98     public String getRangeMaxValue() {
99         if (inRange != null) {
100             return inRange.get(1);
101         } else {
102             return null;
103         }
104     }
105
106     public void setRangeMaxValue(String maxValue) {
107         if (inRange == null) {
108             inRange = Lists.newArrayList("", maxValue);
109         } else {
110             inRange.set(1, maxValue);
111         }
112     }
113
114 }