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