package network; import java.awt.Point; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Network { private Set points = new HashSet<>(); private Set links = new HashSet<>(); public Network(Point... points) { for (Point p : points) { this.points.add((Point)p.clone()); } } public Set points() { Set copy = new HashSet(); for (Point p : points) copy.add((Point)p.clone()); return copy; } public void addLink(Point p1, Point p2) throws ExistingLinkException { Link l = link(p1, p2); if (l != null) throw new ExistingLinkException(l); links.add(new Link(p1, p2)); } public Set links() { return Collections.unmodifiableSet(links); } // Returns the link betweeen the points p1 and p2 if it exists, // null otherwise. public Link link(Point p1, Point p2) { for (Link l : links) if ((l.p1().equals(p1) && l.p2().equals(p2)) || (l.p2().equals(p1) && l.p1().equals(p2))) return l; return null; } }