2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.crud.entity;
23 import net.dongliu.gson.GsonJava8TypeAdapterFactory;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.annotations.SerializedName;
29 import java.util.HashMap;
31 import java.util.Optional;
34 private static final Gson gson = new GsonBuilder().registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
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;
44 private Edge(Builder builder) {
46 this.type = builder.type;
47 this.source = builder.source;
48 this.target = builder.target;
49 this.properties = builder.properties;
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>();
59 public Builder(String type) {
61 throw new IllegalArgumentException("Type cannot be null");
67 public Builder id(String id) {
69 throw new IllegalArgumentException("id cannot be null");
72 this.id = Optional.of(id);
76 public Builder property(String key, Object value) {
77 if (key == null || value == null) {
78 throw new IllegalArgumentException("Property key/value cannot be null");
80 properties.put(key, value);
84 public Builder source(Vertex source) {
89 public Builder target(Vertex target) {
95 return new Edge(this);
100 public String toString() {
101 return "Edge [id=" + id + ", type=" + type + ", properties=" + properties + ", source=" + source + ", target="
105 public String toJson() {
106 return gson.toJson(this);
109 public String toJson(Gson customGson) {
110 return customGson.toJson(this);
113 public static Edge fromJson(String jsonString) {
114 return gson.fromJson(jsonString, Edge.class);
117 public Optional<String> getId() {
121 public String getType() {
125 public Map<String, Object> getProperties() {
129 public Vertex getSource() {
133 public Vertex getTarget() {