package atmsdriver.model; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; /** * PostmileCoords is a collection of Postmiles (id and LatLong Coordinates). * @author jdalbey */ public class PostmileCoords implements Iterable { private List postmileList; public PostmileCoords() { postmileList = new ArrayList(); } /** Accessor to value at given index */ public Postmile get(int idx) { return postmileList.get(idx); } /** Return the size */ public int size() { return postmileList.size(); } /** Find the postmile with the given name */ public Postmile find(String target) { int idx = 0; while (idx < postmileList.size() && !postmileList.get(idx).nameEquals(target)) { idx++; } if (idx < postmileList.size()) { return postmileList.get(idx); } else { return null; } } /** Load the postmile list from external file */ public void load(Scanner scan) { String out = scan.next(); // split into an array String[] results = out.split("\\n"); for (String item : results) { String[] fields = item.split(","); String[] nameparts = fields[0].split(" "); String statepm = nameparts[2]; // some postmiles come with a prefix, which we ignore if (Character.isLetter(statepm.charAt(0))) { statepm = statepm.substring(1); } String revisedpm = nameparts[0] + " " + nameparts[1] + " " + statepm; Postmile pm = new Postmile(revisedpm,fields[1],fields[2]); postmileList.add(pm); } } @Override public Iterator iterator() { return postmileList.iterator(); } /** * Postmile is a postmile id and its LatLong Coordinates. */ final static class Postmile { String name; // the postmile name (Route,Direction,State postmile) String latitude; // the latitude coordinate for this postmile String longitude; // the longitude coordinate for this postmile public Postmile(String name, String lat, String longitude) { this.name = name.trim(); this.latitude = lat.trim(); this.longitude = longitude.trim(); } public boolean nameEquals(String target) { return this.name.equals(target); } @Override public boolean equals(Object aThat) { if (this == aThat) return true; if (!(aThat instanceof Postmile)) return false; Postmile that = (Postmile) aThat; boolean r1 = this.name.equals(that.name); boolean r2 = this.latitude.equals(that.latitude); boolean r3 = this.longitude.equals(that.longitude); return r1 && r2 && r3; } @Override public String toString() { return name + ": " + latitude +","+ longitude; } public String toJson() { String kFeature = "\n{\n \"type\": \"Feature\",\n \"id\": \""; String kPoint = "\", \n \"geometry\":\n {\n \"type\": \"Point\",\n \"coordinates\": ["; String kProp = "]\n" + " },\n" + " \"properties\": \n" + " {\"color\": \"green\""; String kTail = "}\n" + " },"; return kFeature + name + kPoint + longitude + "," + latitude + kProp + kTail; } } }