2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.crud.entity;
26 import net.dongliu.gson.GsonJava8TypeAdapterFactory;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.annotations.SerializedName;
32 import java.util.HashMap;
34 import java.util.Optional;
37 private static final Gson gson = new GsonBuilder().registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
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;
47 private Edge(Builder builder) {
49 this.type = builder.type;
50 this.source = builder.source;
51 this.target = builder.target;
52 this.properties = builder.properties;
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>();
62 public Builder(String type) {
64 throw new IllegalArgumentException("Type cannot be null");
70 public Builder id(String id) {
72 throw new IllegalArgumentException("id cannot be null");
75 this.id = Optional.of(id);
79 public Builder property(String key, Object value) {
80 if (key == null || value == null) {
81 throw new IllegalArgumentException("Property key/value cannot be null");
83 properties.put(key, value);
87 public Builder source(Vertex source) {
92 public Builder target(Vertex target) {
98 return new Edge(this);
103 public String toString() {
104 return "Edge [id=" + id + ", type=" + type + ", properties=" + properties + ", source=" + source + ", target="
108 public String toJson() {
109 return gson.toJson(this);
112 public String toJson(Gson customGson) {
113 return customGson.toJson(this);
116 public static Edge fromJson(String jsonString) {
117 return gson.fromJson(jsonString, Edge.class);
120 public Optional<String> getId() {
124 public String getType() {
128 public Map<String, Object> getProperties() {
132 public Vertex getSource() {
136 public Vertex getTarget() {