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