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