Update license date and text
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / model / ChampPropertyConstraint.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 com.fasterxml.jackson.annotation.JsonProperty;
24 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
25 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
26
27 @JsonDeserialize(builder = ChampPropertyConstraint.Builder.class)
28 public final class ChampPropertyConstraint implements Comparable<ChampPropertyConstraint> {
29
30         private final ChampField field;
31         private final boolean required;
32         private final ChampCardinality cardinality;
33
34         private ChampPropertyConstraint() {
35                 throw new RuntimeException("Cannot call ChampPropertyConstraint() constructor");
36         }
37         
38         private ChampPropertyConstraint(Builder builder) {
39                 this.field = builder.field;
40                 this.required = builder.required;
41                 this.cardinality = builder.cardinality;
42         }
43
44         public ChampField getField() { return field; }
45         public boolean isRequired() { return required; }
46         public ChampCardinality getCardinality() { return cardinality; }
47
48         @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "")
49         public static class Builder {
50                 private final ChampField field;
51
52                 private boolean required = false;
53                 private ChampCardinality cardinality = ChampCardinality.SINGLE;
54
55                 public Builder(@JsonProperty("field") ChampField field) {
56                         this.field = field;
57                 }
58
59                 public Builder required(boolean required) {
60                         this.required = required;
61                         return this;
62                 }
63
64                 public Builder cardinality(ChampCardinality cardinality) {
65                         this.cardinality = cardinality;
66                         return this;
67                 }
68
69                 public ChampPropertyConstraint build() {
70                         return new ChampPropertyConstraint(this);
71                 }
72         }
73
74         @Override
75         public int hashCode() {
76                 return 31 * (getField().hashCode());
77         }
78
79         @Override
80         public boolean equals(Object o) {
81                 if (o instanceof ChampPropertyConstraint) {
82                         final ChampPropertyConstraint propertyConstraint = (ChampPropertyConstraint) o;
83
84                         if (propertyConstraint.getField().equals(getField()))
85                                 return true;
86                 }
87
88                 return false;
89         }
90
91         @Override
92         public String toString() {
93                 return "{field: " + getField() +
94                                 ", required: " + isRequired() +
95                                 ", cardinality: " + getCardinality() +
96                                 "}";
97         }
98
99         @Override
100         public int compareTo(ChampPropertyConstraint o) {
101                 final int fieldComparison = o.getField().compareTo(getField());
102
103                 if (fieldComparison == 0) {
104                         return o.getCardinality().compareTo(getCardinality());
105                 }
106
107                 return fieldComparison;
108         }
109 }