Merge "Added @Override annotation above signature"
[aai/champ.git] / src / main / java / org / onap / aai / champ / model / ChampObject.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 java.util.HashMap;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28 import org.onap.aai.champ.model.fluent.object.CreateChampObjectable;
29 import org.onap.aai.champ.model.fluent.object.impl.CreateChampObjectableImpl;
30
31 import java.util.Optional;
32
33 import com.fasterxml.jackson.annotation.JsonIgnore;
34 import com.fasterxml.jackson.annotation.JsonProperty;
35
36 public final class ChampObject implements ChampElement {
37
38         private final String type;
39         private final Optional<Object> key;
40         private final Map<String, Object> properties;
41
42         public static CreateChampObjectable create() {
43                 return new CreateChampObjectableImpl();
44         }
45
46         private ChampObject() {
47                 throw new RuntimeException("Attempted to call private AAIObject() constructor");
48         } //Not instantiable
49         
50         private ChampObject(Builder builder) {
51                 this.type = builder.type;
52                 this.key = builder.key;
53                 this.properties = builder.properties;
54         }
55
56         @SuppressWarnings("unchecked")
57         public <T> Optional<T> getProperty(String key) {
58                 if (!properties.containsKey(key)) return Optional.empty();
59
60                 return Optional.of((T) properties.get(key));
61         }
62
63         public String getType() { return type; }
64         
65         @JsonIgnore
66         public Optional<Object> getKey() { return key; }
67         public Map<String, Object> getProperties() { return properties; }
68
69         @JsonProperty("key")
70         public Object getKeyValue() {
71           return key.orElse("");
72           
73         }
74         
75         public static class Builder {
76                 private final String type;
77                 private final Map<String, Object> properties = new HashMap<String, Object> ();
78                 
79                 private Optional<Object> key = Optional.empty();
80
81                 public Builder(String type) {
82                         if (type == null) throw new IllegalArgumentException("Type cannot be null");
83
84                         this.type = type;
85                 }
86
87                 public Builder(ChampObject object) {
88                         type = object.getType();
89                         key = object.getKey();
90                         properties(object.getProperties());
91                 }
92
93                 public Builder key(Object key) {
94                         if (key == null) throw new IllegalArgumentException("Key cannot be set to null");
95
96                         this.key = Optional.of(key);
97                         return this;
98                 }
99
100                 public Builder property(String key, Object value) {
101                         if (key == null) throw new IllegalArgumentException("Property key cannot be null");
102                         if (value == null) throw new IllegalArgumentException("Property value cannot be null");
103
104                         if (ReservedPropertyKeys.contains(key)) throw new IllegalArgumentException("Property key " + key + " is reserved");
105
106                         properties.put(key, value);
107                         return this;
108                 }
109
110                 public Builder properties(Map<String, Object> properties) {
111                         for (Entry<String, Object> property : properties.entrySet()) {
112                                 property(property.getKey(), property.getValue());
113                         }
114                         
115                         return this;
116                 }
117                 
118                 public ChampObject build() {
119                         return new ChampObject(this);
120                 }
121         }
122         
123         @Override
124         public boolean equals(Object object) {
125                 if (this == object) return true;
126
127                 if (object instanceof ChampObject) {
128                         final ChampObject champObj = (ChampObject) object;
129
130                         if (getKey().isPresent() && champObj.getKey().isPresent()) {
131
132                                 if (getKey().get().equals(champObj.getKey().get())) return true;
133
134                         } else if (!getKey().isPresent() && !champObj.getKey().isPresent()) {
135                                 if (getType().equals(champObj.getType()) &&
136                                         getProperties().equals(champObj.getProperties())) return true;
137                         }
138                 }
139                 
140                 return false;
141         }
142
143         @Override
144         public int hashCode() {
145                 if (getKey().isPresent()) return getKey().get().hashCode();
146
147                 final int returnValue = 31 * (getType().hashCode() + getProperties().hashCode());
148                 return returnValue;
149         }
150
151         @Override
152         public String toString() {
153                 return "{key: " + (getKey().isPresent() ? getKey().get() : "")
154                         + ", type: " + getType()
155                         + ", properties: " + getProperties() + "}";
156         }
157         
158         public enum ReservedPropertyKeys {
159                 CHAMP_OBJECT_TYPE ("aai_node_type"),
160                 CHAMP_OBJECT_KEY ("key");
161
162                 private final String text;
163
164                 private ReservedPropertyKeys(final String text) {
165                         this.text = text;
166                 }
167
168                 @Override
169                 public String toString() {
170                         return text;
171                 }
172
173                 public static boolean contains(String key) {
174                         for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
175                                 if (choice.toString().equals(key)) return true;
176                         }
177
178                         return false;
179                 }
180         }
181
182         public enum IgnoreOnReadPropertyKeys {
183                 CHAMP_IMPORT_ASSIGNED_ID ("importAssignedId");
184                 
185                 private final String text;
186
187                 private IgnoreOnReadPropertyKeys(final String text) {
188                         this.text = text;
189                 }
190
191                 @Override
192                 public String toString() {
193                         return text;
194                 }
195
196                 public static boolean contains(String key) {
197                         for (IgnoreOnReadPropertyKeys choice : IgnoreOnReadPropertyKeys.values()) {
198                                 if (choice.toString().equals(key)) return true;
199                         }
200
201                         return false;
202                 }
203         }
204
205         public enum ReservedTypes {
206                 ANY ("ANY");
207
208                 private final String text;
209
210                 private ReservedTypes(final String text) {
211                         this.text = text;
212                 }
213
214                 @Override
215                 public String toString() {
216                         return text;
217                 }
218         }
219
220         @Override
221         public boolean isObject() {
222                 return true;
223         }
224
225         @Override
226         public ChampObject asObject() {
227                 return this;
228         }
229
230         @Override
231         public boolean isRelationship() {
232                 return false;
233         }
234
235         @Override
236         public ChampRelationship asRelationship() {
237                 throw new UnsupportedOperationException("Cannot call asRelationship() on ChampObject");
238         }
239 }