Merge "Added @Override annotation above signature"
[aai/champ.git] / src / main / java / org / onap / aai / champ / model / ChampField.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.JsonIgnore;
25 import com.fasterxml.jackson.annotation.JsonProperty;
26 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
27 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
28
29 @JsonDeserialize(builder = ChampField.Builder.class)
30 public final class ChampField implements Comparable<ChampField> {
31
32         private final String name;
33         private final ChampField.Type type;
34
35         private ChampField() {
36                 throw new RuntimeException("Cannot use ChampField() constructor");
37         }
38         
39         public String getName() { return name; }
40         public ChampField.Type getType() { return type; }
41
42         @JsonIgnore
43         public Class<?> getJavaType() {
44                 switch (type) {
45                 case BOOLEAN:
46                         return Boolean.class;
47                 case DOUBLE:
48                         return Double.class;
49                 case FLOAT:
50                         return Float.class;
51                 case INTEGER:
52                         return Integer.class;
53                 case LONG:
54                         return Long.class;
55                 case STRING:
56                         return String.class;
57                 default:
58                         throw new RuntimeException("Unknown ChampField.Type " + type);
59                 }
60         }
61
62         private ChampField(Builder builder) {
63                 this.name = builder.name;
64                 this.type = builder.type;
65         }
66
67         public static enum Type {
68                 STRING,
69                 INTEGER,
70                 LONG,
71                 DOUBLE,
72                 FLOAT,
73                 BOOLEAN
74         }
75
76         @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "")
77         public static class Builder {
78                 private final String name;
79
80                 private ChampField.Type type = ChampField.Type.STRING;
81
82                 public Builder(@JsonProperty("name") String name) {
83                         this.name = name;
84                 }
85                 
86                 public Builder type(ChampField.Type type) {
87                         this.type = type;
88                         return this;
89                 }
90
91                 public ChampField build() {
92                         return new ChampField(this);
93                 }
94         }
95
96         @Override
97         public int hashCode() {
98                 return 31 * (getName().hashCode());
99         }
100
101         @Override
102         public boolean equals(Object object) {
103                 if (object instanceof ChampField) {
104                         final ChampField champField = (ChampField) object;
105
106                         if (champField.getName().equals(getName())) return true;
107                 }
108                 
109                 return false;
110         }
111
112         @Override
113         public String toString() {
114                 return "{name: " + getName() +
115                                 ", type: " + getType() +
116                                 "}";
117         }
118
119         @Override
120         public int compareTo(ChampField o) {
121                 final int nameComparison = getName().compareTo(o.getName());
122
123                 if (nameComparison == 0) {
124                         return getType().compareTo(o.getType());
125                 }
126
127                 return nameComparison;
128         }
129 }