package cz.muni.fi.pb162.project.geometry; import java.util.SortedMap; import java.util.TreeMap; import java.util.Collection; import java.util.List; import java.util.LinkedList; import java.util.Collections; import java.util.SortedSet; import java.util.TreeSet; import java.util.Comparator; public class LabeledPolygon extends SimplePolygon { private SortedMap sorted = new TreeMap(); public LabeledPolygon() { } public void addVertex(String label, Vertex2D vert) { if(label == null) throw new NullPointerException("null label"); if(vert == null) throw new NullPointerException("null vert"); sorted.put(label, vert); } public Vertex2D getVertex(String label) { if(sorted.containsKey(label)) return sorted.get(label); throw new IllegalArgumentException("label"); } public Vertex2D getVertex(int index) { if(index < 0) throw new IllegalArgumentException("index"); int i = 0; for(String label : sorted.keySet()) { if(i == index % sorted.size()) return sorted.get(label); i++; } throw new IllegalArgumentException("index"); } public Collection getLabels(Vertex2D vert) { List labels = new LinkedList(); for(String label : sorted.keySet()) { if(sorted.get(label).equals(vert)) labels.add(label); } return Collections.unmodifiableList(labels); } public int getNumVertices() { return sorted.size(); } public Collection getSortedVertices() { SortedSet sort = new TreeSet(sorted.values()); return Collections.unmodifiableSet(sort); } public Collection getSortedVertices(Comparator comp) { SortedSet sort = new TreeSet(comp); for(String key : sorted.keySet()) sort.add(sorted.get(key)); return Collections.unmodifiableSet(sort); } }