d2f2b08fd3ab11660b4d13bc85f468daca06255e
[aai/gizmo.git] / src / main / java / org / openecomp / crud / entity / Vertex.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.openecomp.crud.entity;
25
26 import net.dongliu.gson.GsonJava8TypeAdapterFactory;
27
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.annotations.SerializedName;
31
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Optional;
35
36 public class Vertex {
37   private static final Gson gson = new GsonBuilder()
38       .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory()).create();
39
40   @SerializedName(value="id", alternate={"key"})
41   private final Optional<String> id;
42
43   private final String type;
44   private final Map<String, Object> properties;
45
46
47   private Vertex(Builder builder) {
48     this.id = builder.id;
49     this.type = builder.type;
50     this.properties = builder.properties;
51   }
52
53   public static class Builder {
54     private Optional<String> id = Optional.empty();
55     private final String type;
56     private final Map<String, Object> properties = new HashMap<String, Object>();
57
58     public Builder(String type) {
59       if (type == null) {
60         throw new IllegalArgumentException("Type cannot be null");
61       }
62       this.type = type;
63     }
64
65     public Builder id(String id) {
66       if (id == null) {
67         throw new IllegalArgumentException("id cannot be null");
68       }
69
70       this.id = Optional.of(id);
71       return this;
72     }
73
74     public Builder property(String key, Object value) {
75       if (key == null || value == null) {
76         throw new IllegalArgumentException("Property key/value cannot be null");
77       }
78       properties.put(key, value);
79       return this;
80     }
81
82     public Vertex build() {
83       return new Vertex(this);
84     }
85   }
86
87   public String toJson() {
88     return gson.toJson(this);
89   }
90
91   public String toJson(Gson customGson) {
92     return customGson.toJson(this);
93   }
94
95   public static Vertex fromJson(String jsonString) {
96     return gson.fromJson(jsonString, Vertex.class);
97   }
98
99   @Override
100   public String toString() {
101     return "Vertex [id=" + id + ", type=" + type + ", properties=" + properties + "]";
102   }
103
104   public Optional<String> getId() {
105     return id;
106   }
107
108   public String getType() {
109     return type;
110   }
111
112   public Map<String, Object> getProperties() {
113     return properties;
114   }
115
116 }