Skip to content

Commit

Permalink
Move out inner classes
Browse files Browse the repository at this point in the history
- This refactor moves out the graph classes to their own and provide
  implementations for the Node and Edge. Providing the implementations
  seem to allow for a nicer assertion.
- Also, moved the game-of-life and dots-dsl in the track's config.json
  to sort the practice exercises by difficulty and then name.
  • Loading branch information
kahgoh committed Sep 5, 2024
1 parent e1e782c commit 96f9b29
Show file tree
Hide file tree
Showing 12 changed files with 326 additions and 224 deletions.
43 changes: 22 additions & 21 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,17 @@
],
"difficulty": 2
},
{
"slug": "dot-dsl",
"name": "DOT DSL",
"uuid": "03e1070d-a3ed-4664-9559-283e535c6bc4",
"practices": [],
"prerequisites": [
"classes",
"lists"
],
"difficulty": 2
},
{
"slug": "grains",
"name": "Grains",
Expand Down Expand Up @@ -916,6 +927,17 @@
],
"difficulty": 5
},
{
"slug": "game-of-life",
"name": "Conway's Game of Life",
"uuid": "749de7fc-3dcb-4231-9b4f-115d153af74f",
"practices": [],
"prerequisites": [
"arrays",
"if-statements"
],
"difficulty": 5
},
{
"slug": "grep",
"name": "Grep",
Expand Down Expand Up @@ -1791,27 +1813,6 @@
"lists"
],
"difficulty": 10
},
{
"slug": "game-of-life",
"name": "Conway's Game of Life",
"uuid": "749de7fc-3dcb-4231-9b4f-115d153af74f",
"practices": [],
"prerequisites": [
"arrays",
"if-statements"
],
"difficulty": 5
},
{
"slug": "dot-dsl",
"name": "DOT DSL",
"uuid": "03e1070d-a3ed-4664-9559-283e535c6bc4",
"practices": [],
"prerequisites": [
"inheritance"
],
"difficulty": 5
}
],
"foregone": [
Expand Down
6 changes: 4 additions & 2 deletions exercises/practice/dot-dsl/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Instructions append

The DSL uses method chaining.
You can learn more about method chaining [here](https://www.geeksforgeeks.org/method-chaining-in-java-with-examples/).
The graph is represented in the DSL by the `Graph` class.
The implementation for the nodes and edges (represented by the `Node` and `Edge` classes respectively) are provided.

For more details on the DSL's expected design, take a look at the test cases in `GraphTest.java`.

Check failure on line 6 in exercises/practice/dot-dsl/.docs/instructions.append.md

View workflow job for this annotation

GitHub Actions / Lint Markdown files

Trailing spaces

exercises/practice/dot-dsl/.docs/instructions.append.md:6:98 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md009.md
13 changes: 9 additions & 4 deletions exercises/practice/dot-dsl/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
{
"authors": [
"kahgoh"
"kahgoh",
"manumafe98"
],
"files": {
"solution": [
"src/main/java/DotDsl.java"
"src/main/java/Graph.java"
],
"test": [
"src/test/java/DotDslTest.java"
"src/test/java/GraphTest.java"
],
"example": [
".meta/src/reference/java/DotDsl.java"
".meta/src/reference/java/Graph.java"
],
"editor": [
"src/main/java/Node.java",
"src/main/java/Edge.java"
]
},
"blurb": "Write a Domain Specific Language similar to the Graphviz dot language.",
Expand Down
108 changes: 0 additions & 108 deletions exercises/practice/dot-dsl/.meta/src/reference/java/DotDsl.java

This file was deleted.

48 changes: 48 additions & 0 deletions exercises/practice/dot-dsl/.meta/src/reference/java/Edge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.Collections;
import java.util.Map;
import java.util.Objects;

/**
* Represents an edge on the graph.
*
* NOTE: There is no need to change this file and is treated as read only by the Exercism test runners.
*/
public final class Edge {
private final String start;

private final String end;

private final Map<String, String> attributes;

public Edge(String start, String end) {
this.start = start;
this.end = end;
this.attributes = Collections.emptyMap();
}

public Edge(String start, String end, Map<String, String> attributes) {
this.start = start;
this.end = end;
this.attributes = Map.copyOf(attributes);
}

@Override
public int hashCode() {
return Objects.hash(start, end, attributes);
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Edge other) {
return Objects.equals(other.start, start)
&& Objects.equals(other.end, end)
&& Objects.equals(other.attributes, attributes);
}
return false;
}

@Override
public String toString() {
return "Edge [start=" + start + ", end=" + end + ", attributes=" + attributes + "]";
}
}
54 changes: 54 additions & 0 deletions exercises/practice/dot-dsl/.meta/src/reference/java/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class Graph {

private final List<Node> nodes = new ArrayList<>();

private final List<Edge> edges = new ArrayList<>();

private final Map<String, String> attributes;

public Graph() {
this.attributes = Collections.emptyMap();
}

public Graph(Map<String, String> attributes) {
this.attributes = Map.copyOf(attributes);
}

public Collection<Node> getNodes() {
return Collections.unmodifiableList(nodes);
}

public Collection<Edge> getEdges() {
return Collections.unmodifiableList(edges);
}

public Graph node(String name) {
nodes.add(new Node(name));
return this;
}

public Graph node(String name, Map<String, String> attributes) {
nodes.add(new Node(name, attributes));
return this;
}

public Graph edge(String start, String end) {
edges.add(new Edge(start, end));
return this;
}

public Graph edge(String start, String end, Map<String, String> attributes) {
edges.add(new Edge(start, end, attributes));
return this;
}

public Map<String, String> getAttributes() {
return attributes;
}
}
43 changes: 43 additions & 0 deletions exercises/practice/dot-dsl/.meta/src/reference/java/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.Collections;
import java.util.Map;
import java.util.Objects;

/**
* Represents a node on the graph.
*
* NOTE: There is no need to change this file and is treated as read only by the Exercism test runners.
*/
public final class Node {

private final String name;

private final Map<String, String> attributes;

public Node(String name) {
this.name = name;
this.attributes = Collections.emptyMap();
}

public Node(String name, Map<String, String> attributes) {
this.name = name;
this.attributes = Map.copyOf(attributes);
}

@Override
public int hashCode() {
return Objects.hash(name, attributes);
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Node other) {
return Objects.equals(other.name, name) && Objects.equals(other.attributes, attributes);
}
return false;
}

@Override
public String toString() {
return "Node [name=" + name + ", attributes=" + attributes + "]";
}
}
Loading

0 comments on commit 96f9b29

Please sign in to comment.