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