Update license date and text
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / model / ChampObjectConstraint.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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  */
21 package org.onap.aai.champcore.model;
22
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.Set;
28
29 import com.fasterxml.jackson.annotation.JsonIgnore;
30 import com.fasterxml.jackson.annotation.JsonProperty;
31 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
32 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
33
34 @JsonDeserialize(builder = ChampObjectConstraint.Builder.class)
35 public final class ChampObjectConstraint {
36
37         private final String type;
38         private final Map<String, ChampPropertyConstraint> propertyConstraints;
39         
40         private ChampObjectConstraint() {
41                 throw new RuntimeException("Cannot call ChampObjectConstraint() constructor");
42         }
43         
44         private ChampObjectConstraint(Builder builder) {
45                 this.type = builder.type;
46                 this.propertyConstraints = builder.propertyConstraints;
47         }
48         
49         public String getType() { return type; }
50         public Set<ChampPropertyConstraint> getPropertyConstraints() { return new HashSet<ChampPropertyConstraint> (propertyConstraints.values()); }
51         
52         public Optional<ChampPropertyConstraint> getPropertyConstraint(String fieldName) {
53                 if (!propertyConstraints.containsKey(fieldName)) return Optional.empty();
54
55                 return Optional.of(propertyConstraints.get(fieldName));
56         }
57
58         @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "")
59         public static class Builder {
60                 private final String type;
61                 private final Map<String, ChampPropertyConstraint> propertyConstraints;
62                 
63                 public Builder(@JsonProperty("type") String type) {
64                         this.type = type;
65                         this.propertyConstraints = new HashMap<String, ChampPropertyConstraint> ();
66                 }
67
68                 @JsonProperty("propertyConstraints")
69                 public Builder constraints(Set<ChampPropertyConstraint> propertyConstraints) {
70
71                         for (ChampPropertyConstraint propConstraint : propertyConstraints) {
72                                 constraint(propConstraint);
73                         }
74
75                         return this;
76                 }
77
78                 @JsonIgnore
79                 public Builder constraint(ChampPropertyConstraint propConstraint) {
80                         propertyConstraints.put(propConstraint.getField().getName(), propConstraint);
81                         return this;
82                 }
83
84                 public ChampObjectConstraint build() {
85                         return new ChampObjectConstraint(this);
86                 }
87         }
88
89         @Override
90         public boolean equals(Object o) {
91                 if (o instanceof ChampObjectConstraint) {
92                         final ChampObjectConstraint objectConstraint = (ChampObjectConstraint) o;
93
94                         if (objectConstraint.getType().equals(getType())) return true;
95                 }
96                 
97                 return false;
98         }
99
100         @Override
101         public String toString() {
102                 return "{type: " + getType() +
103                                 ", propertyConstraints: " + getPropertyConstraints() +
104                                 "}";
105         }
106 }