Backend support for operation milestones with activities
[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     PATTERN("pattern"),
43     SCHEMA("schema");
44
45     private static final Set<ConstraintType> comparableConstraints = Set.of(
46         ConstraintType.EQUAL,
47         ConstraintType.GREATER_THAN,
48         ConstraintType.GREATER_OR_EQUAL,
49         ConstraintType.LESS_OR_EQUAL,
50         ConstraintType.LESS_THAN);
51     private static final Set<ConstraintType> lengthConstraints = Set.of(
52         ConstraintType.LENGTH,
53         ConstraintType.MIN_LENGTH,
54         ConstraintType.MAX_LENGTH);
55     private final String type;
56     private final List<String> typeAlias;
57
58
59     ConstraintType(final String type, final String... typeAliases) {
60         this.type = type;
61         if (typeAliases == null) {
62             this.typeAlias = Collections.emptyList();
63         } else {
64             this.typeAlias = Arrays.asList(typeAliases);
65         }
66     }
67
68     public static Optional<ConstraintType> findByType(final String type) {
69         if (type == null) {
70             return Optional.empty();
71         }
72         return Arrays.stream(ConstraintType.values())
73             .filter(constraintType -> constraintType.getType().equals(type) || constraintType.getTypeAlias().contains(type))
74             .findFirst();
75     }
76
77     public boolean isComparable() {
78         return comparableConstraints.contains(this);
79     }
80
81     public boolean isLengthConstraint() {
82         return lengthConstraints.contains(this);
83     }
84
85 }