cef310ea01a7892444b6d227d0c68332f53b379f
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / enums / ConstraintType.java
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.openecomp.sdc.be.datatypes.enums;
22
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.Set;
28 import lombok.Getter;
29
30 @Getter
31 public enum ConstraintType {
32     EQUAL("equal"),
33     IN_RANGE("in_range", "inRange"),
34     GREATER_THAN("greater_than", "greaterThan"),
35     GREATER_OR_EQUAL("greater_or_equal", "greaterOrEqual"),
36     LESS_OR_EQUAL("less_or_equal", "lessOrEqual"),
37     LENGTH("length"),
38     MIN_LENGTH("min_length", "minLength"),
39     MAX_LENGTH("max_length", "maxLength"),
40     VALID_VALUES("valid_values", "validValues"),
41     LESS_THAN("less_than", "lessThan"),
42     SCHEMA("schema");
43
44     private static final Set<ConstraintType> comparableConstraints = Set.of(ConstraintType.GREATER_THAN, ConstraintType.LESS_THAN);
45     private final String type;
46     private final List<String> typeAlias;
47
48
49     ConstraintType(final String type, final String... typeAliases) {
50         this.type = type;
51         if (typeAliases == null) {
52             this.typeAlias = Collections.emptyList();
53         } else {
54             this.typeAlias = Arrays.asList(typeAliases);
55         }
56     }
57
58     public static Optional<ConstraintType> findByType(final String type) {
59         if (type == null) {
60             return Optional.empty();
61         }
62         return Arrays.stream(ConstraintType.values())
63             .filter(constraintType -> constraintType.getType().equals(type) || constraintType.getTypeAlias().contains(type))
64             .findFirst();
65     }
66
67     public boolean isComparable() {
68         return comparableConstraints.contains(this);
69     }
70
71 }