Skip to content

Commit 6cbdcc5

Browse files
author
DvirDukhan
committed
Added graph entities and graph entity property classes
1 parent cc9faee commit 6cbdcc5

File tree

4 files changed

+395
-0
lines changed

4 files changed

+395
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.redislabs.redisgraph.impl;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* A class represent an edge (graph entity). In addition to the base class id and properties, an edge shows its source,
7+
* destination and relationship type
8+
*/
9+
public class Edge extends GraphEntity {
10+
11+
//memebers
12+
String relationshipType;
13+
int source;
14+
int destination;
15+
16+
17+
//getters & setters
18+
19+
/**
20+
* @return the edge relationship type
21+
*/
22+
public String getRelationshipType() {
23+
return relationshipType;
24+
}
25+
26+
/**
27+
* @param relationshipType - the relationship type to be set.
28+
*/
29+
public void setRelationshipType(String relationshipType) {
30+
this.relationshipType = relationshipType;
31+
}
32+
33+
34+
/**
35+
* @return The id of the source node
36+
*/
37+
public int getSource() {
38+
return source;
39+
}
40+
41+
/**
42+
* @param source - The id of the source node to be set
43+
*/
44+
public void setSource(int source) {
45+
this.source = source;
46+
}
47+
48+
/**
49+
*
50+
* @return the id of the destination node
51+
*/
52+
public int getDestination() {
53+
return destination;
54+
}
55+
56+
/**
57+
*
58+
* @param destination - The id of the destination node to be set
59+
*/
60+
public void setDestination(int destination) {
61+
this.destination = destination;
62+
}
63+
64+
65+
@Override
66+
public boolean equals(Object o) {
67+
if (this == o) return true;
68+
if (!(o instanceof Edge)) return false;
69+
if (!super.equals(o)) return false;
70+
Edge edge = (Edge) o;
71+
return source == edge.source &&
72+
destination == edge.destination &&
73+
Objects.equals(relationshipType, edge.relationshipType);
74+
}
75+
76+
@Override
77+
public int hashCode() {
78+
return Objects.hash(super.hashCode(), relationshipType, source, destination);
79+
}
80+
81+
@Override
82+
public String toString() {
83+
final StringBuilder sb = new StringBuilder("Edge{");
84+
sb.append("relationshipType='").append(relationshipType).append('\'');
85+
sb.append(", source=").append(source);
86+
sb.append(", destination=").append(destination);
87+
sb.append(", id=").append(id);
88+
sb.append(", propertyMap=").append(propertyMap);
89+
sb.append('}');
90+
return sb.toString();
91+
}
92+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.redislabs.redisgraph.impl;
2+
3+
4+
import com.redislabs.redisgraph.ResultSet.ResultSetScalarTypes;
5+
6+
import java.util.*;
7+
8+
9+
/**
10+
* This is an abstract class for representing a graph entity.
11+
* A graph entity has an id and a set of properties. The properties are mapped and accessed by their names.
12+
*/
13+
public abstract class GraphEntity {
14+
15+
16+
17+
//members
18+
19+
int id;
20+
Map<String, Property> propertyMap = new HashMap<>();
21+
22+
23+
//setters & getters
24+
25+
/**
26+
*
27+
* @return entity id
28+
*/
29+
public int getId() {
30+
return id;
31+
}
32+
33+
/**
34+
*
35+
* @param id - entity id to be set
36+
*/
37+
public void setId(int id) {
38+
this.id = id;
39+
}
40+
41+
42+
/**
43+
* Adds a property to the entity, by composing name, type and value to a property object
44+
* @param name
45+
* @param type
46+
* @param value
47+
*/
48+
public void addProperty(String name, ResultSetScalarTypes type, Object value){
49+
50+
addProperty(new Property(name, type, value));
51+
52+
}
53+
54+
/**
55+
* Add a property to the entity
56+
* @param property
57+
*/
58+
public void addProperty (Property property){
59+
60+
61+
propertyMap.put(property.name, property);
62+
}
63+
64+
/**
65+
*
66+
* @return number of properties
67+
*/
68+
public int getNumberOfProperties(){
69+
return propertyMap.size();
70+
}
71+
72+
73+
/**
74+
*
75+
* @param propertyName - property name as lookup key (String)
76+
* @return property object, or null if key is not found
77+
*/
78+
public Property getProperty(String propertyName){
79+
return propertyMap.get(propertyName);
80+
}
81+
82+
83+
/**
84+
*
85+
* @param name - the name of the property to be removed
86+
*/
87+
public void removeProperty(String name){
88+
89+
propertyMap.remove(name);
90+
91+
}
92+
93+
@Override
94+
public boolean equals(Object o) {
95+
if (this == o) return true;
96+
if (!(o instanceof GraphEntity)) return false;
97+
GraphEntity that = (GraphEntity) o;
98+
return id == that.id &&
99+
Objects.equals(propertyMap, that.propertyMap);
100+
}
101+
102+
@Override
103+
public int hashCode() {
104+
return Objects.hash(id, propertyMap);
105+
}
106+
107+
108+
/**
109+
* Default toString implementation.
110+
* @return
111+
*/
112+
@Override
113+
public String toString() {
114+
final StringBuilder sb = new StringBuilder("GraphEntity{");
115+
sb.append("id=").append(id);
116+
sb.append(", propertyMap=").append(propertyMap);
117+
sb.append('}');
118+
return sb.toString();
119+
}
120+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.redislabs.redisgraph.impl;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Objects;
6+
7+
/**
8+
* * A class represent an node (graph entity). In addition to the base class id and properties, a node has labels.
9+
*/
10+
public class Node extends GraphEntity {
11+
12+
//members
13+
List<String> labels = new ArrayList<>();
14+
15+
/**
16+
* @param label - a label to be add
17+
*/
18+
public void addLabel(String label) {
19+
labels.add(label);
20+
}
21+
22+
/**
23+
* @param label - a label to be removed
24+
*/
25+
public void removeLabel(String label) {
26+
labels.remove(label);
27+
}
28+
29+
/**
30+
* @param index - label index
31+
* @return the proprty label
32+
* @throws IndexOutOfBoundsException if the index is out of range
33+
* ({@code index < 0 || index >= getNumberOfLabels()})
34+
*/
35+
public String getLabel(int index) throws IndexOutOfBoundsException{
36+
return labels.get(index);
37+
}
38+
39+
/**
40+
*
41+
* @return the number of labels
42+
*/
43+
public int getNumberOfLabels() {
44+
return labels.size();
45+
}
46+
47+
@Override
48+
public boolean equals(Object o) {
49+
if (this == o) return true;
50+
if (!(o instanceof Node)) return false;
51+
if (!super.equals(o)) return false;
52+
Node node = (Node) o;
53+
return Objects.equals(labels, node.labels);
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
return Objects.hash(super.hashCode(), labels);
59+
}
60+
61+
@Override
62+
public String toString() {
63+
final StringBuilder sb = new StringBuilder("Node{");
64+
sb.append("labels=").append(labels);
65+
sb.append(", id=").append(id);
66+
sb.append(", propertyMap=").append(propertyMap);
67+
sb.append('}');
68+
return sb.toString();
69+
}
70+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.redislabs.redisgraph.impl;
2+
3+
import com.redislabs.redisgraph.ResultSet;
4+
5+
import java.util.Objects;
6+
7+
/**
8+
* A Graph entity property. Has a name, type, and value
9+
*/
10+
public class Property {
11+
12+
//members
13+
String name;
14+
ResultSet.ResultSetScalarTypes type;
15+
Object value;
16+
17+
18+
/**
19+
* Default constructor
20+
*/
21+
public Property() {
22+
23+
}
24+
25+
/**
26+
* Parameterized constructor
27+
*
28+
* @param name
29+
* @param type
30+
* @param value
31+
*/
32+
public Property(String name, ResultSet.ResultSetScalarTypes type, Object value) {
33+
this.name = name;
34+
this.type = type;
35+
this.value = value;
36+
}
37+
38+
//getters & setters
39+
40+
/**
41+
* @return property name
42+
*/
43+
public String getName() {
44+
return name;
45+
}
46+
47+
/**
48+
* @param name - property name to be set
49+
*/
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
54+
/**
55+
* @return property type
56+
*/
57+
public ResultSet.ResultSetScalarTypes getType() {
58+
return type;
59+
}
60+
61+
/**
62+
* @param type property type to be set
63+
*/
64+
public void setType(ResultSet.ResultSetScalarTypes type) {
65+
this.type = type;
66+
}
67+
68+
69+
/**
70+
* @return property value
71+
*/
72+
public Object getValue() {
73+
return value;
74+
}
75+
76+
77+
/**
78+
* @param value property value to be set
79+
*/
80+
public void setValue(Object value) {
81+
this.value = value;
82+
}
83+
84+
85+
@Override
86+
public boolean equals(Object o) {
87+
if (this == o) return true;
88+
if (!(o instanceof Property)) return false;
89+
Property property = (Property) o;
90+
return Objects.equals(name, property.name) &&
91+
type == property.type &&
92+
Objects.equals(value, property.value);
93+
}
94+
95+
@Override
96+
public int hashCode() {
97+
return Objects.hash(name, type, value);
98+
}
99+
100+
/**
101+
* Default toString implementation
102+
* @return
103+
*/
104+
@Override
105+
public String toString() {
106+
final StringBuilder sb = new StringBuilder("Property{");
107+
sb.append("name='").append(name).append('\'');
108+
sb.append(", type=").append(type);
109+
sb.append(", value=").append(value);
110+
sb.append('}');
111+
return sb.toString();
112+
}
113+
}

0 commit comments

Comments
 (0)