-
Notifications
You must be signed in to change notification settings - Fork 22
added path support #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/main/java/com/redislabs/redisgraph/graph_entities/Path.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package com.redislabs.redisgraph.graph_entities; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * This class represents a path in the graph. | ||
| */ | ||
| public final class Path { | ||
|
|
||
| private final List<Node> nodes; | ||
| private final List<Edge> edges; | ||
|
|
||
|
|
||
| /** | ||
| * Parametrized constructor | ||
| * @param nodes - List of nodes. | ||
| * @param edges - List of edges. | ||
| */ | ||
| public Path(List<Node> nodes, List<Edge> edges) { | ||
| this.nodes = nodes; | ||
| this.edges = edges; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the nodes of the path. | ||
| * @return List of nodes. | ||
| */ | ||
| public List<Node> getNodes() { | ||
| return nodes; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the edges of the path. | ||
| * @return List of edges. | ||
| */ | ||
| public List<Edge> getEdges() { | ||
| return edges; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the length of the path - number of edges. | ||
| * @return Number of edges. | ||
| */ | ||
| public int length() { | ||
| return edges.size(); | ||
| } | ||
|
|
||
| /** | ||
| * Return the number of nodes in the path. | ||
| * @return Number of nodes. | ||
| */ | ||
| public int nodeCount(){ | ||
| return nodes.size(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the first node in the path. | ||
| * @return First nodes in the path. | ||
| * @throws IndexOutOfBoundsException if the path is empty. | ||
| */ | ||
| public Node firstNode(){ | ||
| return nodes.get(0); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the last node in the path. | ||
| * @return Last nodes in the path. | ||
| * @throws IndexOutOfBoundsException if the path is empty. | ||
| */ | ||
| public Node lastNode(){ | ||
| return nodes.get(nodes.size() - 1); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a node with specified index in the path. | ||
| * @return Node. | ||
| * @throws IndexOutOfBoundsException if the index is out of range | ||
| * ({@code index < 0 || index >= nodesCount()}) | ||
| */ | ||
| public Node getNode(int index){ | ||
| return nodes.get(index); | ||
| } | ||
|
|
||
| /** | ||
| * Returns an edge with specified index in the path. | ||
| * @return Edge. | ||
| * @throws IndexOutOfBoundsException if the index is out of range | ||
| * ({@code index < 0 || index >= length()}) | ||
| */ | ||
| public Edge getEdge(int index){ | ||
| return edges.get(index); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Path path = (Path) o; | ||
| return Objects.equals(nodes, path.nodes) && | ||
| Objects.equals(edges, path.edges); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(nodes, edges); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| final StringBuilder sb = new StringBuilder("Path{"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return "Path{nodes=" + nodes + " edges=" + edges + "}";
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. auto-generated. I like string builder better than string appends. |
||
| sb.append("nodes=").append(nodes); | ||
| sb.append(", edges=").append(edges); | ||
| sb.append('}'); | ||
| return sb.toString(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/test/java/com/redislabs/redisgraph/graph_entities/PathTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package com.redislabs.redisgraph.graph_entities; | ||
|
|
||
| import nl.jqno.equalsverifier.EqualsVerifier; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.ThreadLocalRandom; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class PathTest { | ||
|
|
||
| private Node buildNode(int id){ | ||
| Node n = new Node(); | ||
| n.setId(0); | ||
| return n; | ||
| } | ||
|
|
||
| private Edge buildEdge(int id, int src, int dst){ | ||
| Edge e = new Edge(); | ||
| e.setId(id); | ||
| e.setSource(src); | ||
| e.setDestination(dst); | ||
| return e; | ||
| } | ||
|
|
||
| private List<Node> buildNodeArray(int size) { | ||
| List<Node> nodes = new ArrayList<>(); | ||
| return IntStream.range(0, size).mapToObj(i -> buildNode(i)).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private List<Edge> buildEdgeArray(int size){ | ||
| List<Node> nodes = new ArrayList<>(); | ||
| return IntStream.range(0, size).mapToObj(i -> buildEdge(i, i, i+1)).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private Path buildPath(int nodeCount){ | ||
| return new Path(buildNodeArray(nodeCount), buildEdgeArray(nodeCount-1)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyPath(){ | ||
| Path path = buildPath(0); | ||
| assertEquals(0, path.length()); | ||
| assertEquals(0, path.nodeCount()); | ||
| assertThrows(IndexOutOfBoundsException.class, ()->path.getNode(0)); | ||
| assertThrows(IndexOutOfBoundsException.class, ()->path.getEdge(0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSingleNodePath(){ | ||
| Path path = buildPath(1); | ||
| assertEquals(0, path.length()); | ||
| assertEquals(1, path.nodeCount()); | ||
| Node n = new Node(); | ||
| n.setId(0); | ||
| assertEquals(n, path.firstNode()); | ||
| assertEquals(n, path.lastNode()); | ||
| assertEquals(n, path.getNode(0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRandomLengthPath(){ | ||
| int nodeCount = ThreadLocalRandom.current().nextInt(2, 100 + 1); | ||
| Path path = buildPath(nodeCount); | ||
| assertEquals(buildNodeArray(nodeCount), path.getNodes()); | ||
| assertEquals(buildEdgeArray(nodeCount-1), path.getEdges()); | ||
| assertDoesNotThrow(()->path.getEdge(0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void hashCodeEqualTest(){ | ||
| EqualsVerifier.forClass(Path.class).verify(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.