From b8653453d9c20e9797839af89085fa2f9b0ff521 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 27 Oct 2015 16:03:11 +0100 Subject: Using a type variable for a Node's content --- Practical1/src/nl/camilstaps/cs/graphs/Node.java | 34 +++++++++++++----------- 1 file changed, 18 insertions(+), 16 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 b9d078e..ccdbd4a 100644 --- a/Practical1/src/nl/camilstaps/cs/graphs/Node.java +++ b/Practical1/src/nl/camilstaps/cs/graphs/Node.java @@ -10,11 +10,11 @@ import java.util.stream.Collectors; * * @author Camil Staps */ -public class Node implements Comparable { - private final int content; - private final List neighbours = new ArrayList<>(); +public class Node implements Comparable> { + private final T content; + private final List> neighbours = new ArrayList<>(); - public Node(int content) { + public Node(T content) { this.content = content; } @@ -28,15 +28,15 @@ public class Node implements Comparable { /** * @return the neighbours */ - public List getNeighbourhood() { + public List> getNeighbourhood() { return neighbours; } /** * @return the neighbours with the Node itself */ - public List getInclusiveNeighbourhood() { - List nb = new ArrayList<>(); + public List> getInclusiveNeighbourhood() { + List> nb = new ArrayList<>(); nb.add(this); nb.addAll(neighbours); return nb; @@ -45,10 +45,12 @@ public class Node implements Comparable { /** * @return the neighbours of the neighbours, without this Node itself */ - public List getSecondNeighbourhood() { - List nb = new ArrayList<>(); - for (Node n : getNeighbourhood()) - n.getNeighbourhood().stream().filter(n2 -> !nb.contains(n2)).forEach(nb::add); + public List> getSecondNeighbourhood() { + List> nb = new ArrayList<>(); + for (Node n : getNeighbourhood()) + for (Node n2 : n.getNeighbourhood()) + if (!nb.contains(n2)) + nb.add(n2); nb.remove(this); return nb; } @@ -58,8 +60,8 @@ public class Node implements Comparable { * @param b the Node to compare to * @return true iff this Node dominates Node b */ - public boolean dominates(Node b) { - for (Node nb : getInclusiveNeighbourhood()) + public boolean dominates(Node b) { + for (Node nb : getInclusiveNeighbourhood()) if (!b.getInclusiveNeighbourhood().contains(nb)) return false; return true; @@ -69,7 +71,7 @@ public class Node implements Comparable { * @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) { + public boolean neighbourhoodsDisjoint(Node b) { return neighbourhoodIntersection(b).isEmpty(); } @@ -77,7 +79,7 @@ public class Node implements Comparable { * @param b another Node * @return the intersection of the neighbourhood of this Node and the neighbourhood of Node b */ - public List neighbourhoodIntersection(Node b) { + public List> neighbourhoodIntersection(Node b) { return neighbours.stream().filter(n -> b.getNeighbourhood().contains(n)).collect(Collectors.toList()); } @@ -105,7 +107,7 @@ public class Node implements Comparable { */ @Override @SuppressWarnings("NullableProblems") - public int compareTo(Node b) { + public int compareTo(Node b) { return Integer.compare(neighbours.size(), b.neighbours.size()); } } -- cgit v1.2.3