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