Renaming openecomp to onap
[aai/champ.git] / src / main / java / org / onap / aai / champ / model / ChampObjectConstraint.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ===================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.champ.model;
23
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.Set;
29
30 import com.fasterxml.jackson.annotation.JsonIgnore;
31 import com.fasterxml.jackson.annotation.JsonProperty;
32 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
33 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
34
35 @JsonDeserialize(builder = ChampObjectConstraint.Builder.class)
36 public final class ChampObjectConstraint {
37
38         private final String type;
39         private final Map<String, ChampPropertyConstraint> propertyConstraints;
40         
41         private ChampObjectConstraint() {
42                 throw new RuntimeException("Cannot call ChampObjectConstraint() constructor");
43         }
44         
45         private ChampObjectConstraint(Builder builder) {
46                 this.type = builder.type;
47                 this.propertyConstraints = builder.propertyConstraints;
48         }
49         
50         public String getType() { return type; }
51         public Set<ChampPropertyConstraint> getPropertyConstraints() { return new HashSet<ChampPropertyConstraint> (propertyConstraints.values()); }
52         
53         public Optional<ChampPropertyConstraint> getPropertyConstraint(String fieldName) {
54                 if (!propertyConstraints.containsKey(fieldName)) return Optional.empty();
55
56                 return Optional.of(propertyConstraints.get(fieldName));
57         }
58
59         @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "")
60         public static class Builder {
61                 private final String type;
62                 private final Map<String, ChampPropertyConstraint> propertyConstraints;
63                 
64                 public Builder(@JsonProperty("type") String type) {
65                         this.type = type;
66                         this.propertyConstraints = new HashMap<String, ChampPropertyConstraint> ();
67                 }
68
69                 @JsonProperty("propertyConstraints")
70                 public Builder constraints(Set<ChampPropertyConstraint> propertyConstraints) {
71
72                         for (ChampPropertyConstraint propConstraint : propertyConstraints) {
73                                 constraint(propConstraint);
74                         }
75
76                         return this;
77                 }
78
79                 @JsonIgnore
80                 public Builder constraint(ChampPropertyConstraint propConstraint) {
81                         propertyConstraints.put(propConstraint.getField().getName(), propConstraint);
82                         return this;
83                 }
84
85                 public ChampObjectConstraint build() {
86                         return new ChampObjectConstraint(this);
87                 }
88         }
89
90         @Override
91         public boolean equals(Object o) {
92                 if (o instanceof ChampObjectConstraint) {
93                         final ChampObjectConstraint objectConstraint = (ChampObjectConstraint) o;
94
95                         if (objectConstraint.getType().equals(getType())) return true;
96                 }
97                 
98                 return false;
99         }
100
101         @Override
102         public String toString() {
103                 return "{type: " + getType() +
104                                 ", propertyConstraints: " + getPropertyConstraints() +
105                                 "}";
106         }
107 }