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