From 78c8b3bd7dd49700c8607093304c599e2313f346 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Fri, 23 Oct 2015 12:35:46 +0200 Subject: Practical1: comments; Java8 fancy stuff; enhancements tester --- Practical1/src/nl/camilstaps/cs/graphs/Node.java | 59 ++++++++++++++++++------ 1 file changed, 44 insertions(+), 15 deletions(-) (limited to 'Practical1/src/nl/camilstaps/cs/graphs/Node.java') diff --git a/Practical1/src/nl/camilstaps/cs/graphs/Node.java b/Practical1/src/nl/camilstaps/cs/graphs/Node.java index 4edc666..b9d078e 100644 --- a/Practical1/src/nl/camilstaps/cs/graphs/Node.java +++ b/Practical1/src/nl/camilstaps/cs/graphs/Node.java @@ -2,6 +2,7 @@ package nl.camilstaps.cs.graphs; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * A Node is a simple point in a graph, with references to its neighbours. It holds an integer 'content' which can act @@ -17,14 +18,23 @@ public class Node implements Comparable { this.content = content; } + /** + * @return the number of neighbours + */ public int getDegree() { return neighbours.size(); } + /** + * @return the neighbours + */ public List getNeighbourhood() { return neighbours; } + /** + * @return the neighbours with the Node itself + */ public List getInclusiveNeighbourhood() { List nb = new ArrayList<>(); nb.add(this); @@ -32,16 +42,22 @@ public class Node implements Comparable { return nb; } + /** + * @return the neighbours of the neighbours, without this Node itself + */ public List getSecondNeighbourhood() { List nb = new ArrayList<>(); for (Node n : getNeighbourhood()) - for (Node n2 : n.getNeighbourhood()) - if (!nb.contains(n2)) - nb.add(n2); + n.getNeighbourhood().stream().filter(n2 -> !nb.contains(n2)).forEach(nb::add); nb.remove(this); return nb; } + /** + * Node a dominates a Node b if the inclusive neighbourhood of a is a subset of the inclusive neighbourhood of b. + * @param b the Node to compare to + * @return true iff this Node dominates Node b + */ public boolean dominates(Node b) { for (Node nb : getInclusiveNeighbourhood()) if (!b.getInclusiveNeighbourhood().contains(nb)) @@ -49,34 +65,47 @@ public class Node implements Comparable { return true; } + /** + * @param b the Node to compare to + * @return true iff the neighbourhood of this Node is disjoint with the neighbourhood of Node b + */ public boolean neighbourhoodsDisjoint(Node b) { return neighbourhoodIntersection(b).isEmpty(); } + /** + * @param b another Node + * @return the intersection of the neighbourhood of this Node and the neighbourhood of Node b + */ public List neighbourhoodIntersection(Node b) { - List intersection = new ArrayList<>(); - for (Node n : neighbours) - if (b.getNeighbourhood().contains(n)) - intersection.add(n); - return intersection; + return neighbours.stream().filter(n -> b.getNeighbourhood().contains(n)).collect(Collectors.toList()); } + /** + * @return a String representation of the Node + */ public String toString() { return "<" + content + ">"; } /** * Nodes are equal if their identifiers are equal - * @param another - * @return + * @param b the Node to compare to + * @return true iff this Node is equivalent to Node b */ - public boolean equals(Object another) { - return !(another == null || another.getClass() != this.getClass()) && - content == (((Node) another).content); + public boolean equals(Object b) { + return !(b == null || b.getClass() != this.getClass()) && + content == (((Node) b).content); } + /** + * This method allows sorting of Nodes by their degrees. + * @param b the Node to compare to + * @return 1 if d(this) > d(b), -1 if d(this) < d(b), 0 otherwise + */ @Override - public int compareTo(Node node) { - return node == null ? 1 : Integer.compare(neighbours.size(), node.neighbours.size()); + @SuppressWarnings("NullableProblems") + public int compareTo(Node b) { + return Integer.compare(neighbours.size(), b.neighbours.size()); } } -- cgit v1.2.3