87b0ac400105009243f4c946c8e62ed6d8b15d6c
[aai/gizmo.git] / src / main / java / org / onap / crud / entity / Edge.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.onap.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 Edge {
37   private static final Gson gson = new GsonBuilder().registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
38       .create();
39
40   @SerializedName(value = "id", alternate = { "key" })
41   private final Optional<String> id;
42   private final String type;
43   private final Map<String, Object> properties;
44   private final Vertex source;
45   private final Vertex target;
46
47   private Edge(Builder builder) {
48     this.id = builder.id;
49     this.type = builder.type;
50     this.source = builder.source;
51     this.target = builder.target;
52     this.properties = builder.properties;
53   }
54
55   public static class Builder {
56     private Optional<String> id = Optional.empty();
57     private final String type;
58     private Vertex source;
59     private Vertex target;
60     private final Map<String, Object> properties = new HashMap<String, Object>();
61
62     public Builder(String type) {
63       if (type == null) {
64         throw new IllegalArgumentException("Type cannot be null");
65       }
66
67       this.type = type;
68     }
69
70     public Builder id(String id) {
71       if (id == null) {
72         throw new IllegalArgumentException("id cannot be null");
73       }
74
75       this.id = Optional.of(id);
76       return this;
77     }
78
79     public Builder property(String key, Object value) {
80       if (key == null || value == null) {
81         throw new IllegalArgumentException("Property key/value cannot be null");
82       }
83       properties.put(key, value);
84       return this;
85     }
86
87     public Builder source(Vertex source) {
88       this.source = source;
89       return this;
90     }
91
92     public Builder target(Vertex target) {
93       this.target = target;
94       return this;
95     }
96
97     public Edge build() {
98       return new Edge(this);
99     }
100   }
101
102   @Override
103   public String toString() {
104     return "Edge [id=" + id + ", type=" + type + ", properties=" + properties + ", source=" + source + ", target="
105         + target + "]";
106   }
107
108   public String toJson() {
109     return gson.toJson(this);
110   }
111
112   public String toJson(Gson customGson) {
113     return customGson.toJson(this);
114   }
115
116   public static Edge fromJson(String jsonString) {
117     return gson.fromJson(jsonString, Edge.class);
118   }
119
120   public Optional<String> getId() {
121     return id;
122   }
123
124   public String getType() {
125     return type;
126   }
127
128   public Map<String, Object> getProperties() {
129     return properties;
130   }
131
132   public Vertex getSource() {
133     return source;
134   }
135
136   public Vertex getTarget() {
137     return target;
138   }
139
140 }