Advertisement
Guest User

Untitled

a guest
May 28th, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.11 KB | None | 0 0
  1. package com.tester;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. class I360 {
  12.    
  13.     private static class Plane {
  14.        
  15.         private String icao24;
  16.         private String callSign;
  17.         private String originCountry;
  18.         private Integer timePosition;
  19.         private Integer lastContact;
  20.         private Double longitude;
  21.         private Double latitude;
  22.         private Double geoAltitude;
  23.         private Boolean onGround;
  24.         private Double velocity;
  25.         private Double heading;
  26.         private Double verticalRate;
  27.         private Integer[] sensors;
  28.         private Double baroAltitude;
  29.         private String squawk;
  30.         private Boolean spi;
  31.         private Integer positionSource;
  32.        
  33.         Plane(String icao24, String callSign, String originCountry, Integer timePosition,
  34.                 Integer lastContact, Double longitude, Double latitude, Double geoAltitude,
  35.                 Boolean onGround, Double velocity, Double heading, Double verticalRate,
  36.                 Integer[] sensors, Double baroAltitude, String squawk, Boolean spi,
  37.                 int positionSource) {
  38.             this.icao24 = icao24;
  39.             this.callSign = callSign;
  40.             this.originCountry = originCountry;
  41.             this.timePosition = timePosition;
  42.             this.lastContact = lastContact;
  43.             this.longitude = longitude;
  44.             this.latitude = latitude;
  45.             this.geoAltitude = geoAltitude;
  46.             this.onGround = onGround;
  47.             this.velocity = velocity;
  48.             this.heading = heading;
  49.             this.verticalRate = verticalRate;
  50.             this.sensors = sensors;
  51.             this.baroAltitude = baroAltitude;
  52.             this.squawk = squawk;
  53.             this.spi = spi;
  54.             this.positionSource = positionSource;
  55.         }
  56.        
  57.         public String getIcao24() { return icao24; }
  58.         public String getCallSign() { return callSign; }
  59.         public String getOriginCountry() { return originCountry; }
  60.         public Integer getTimePosition() { return timePosition; }
  61.         public Integer getLastContact() { return lastContact; }
  62.         public Double getLongitude() { return longitude; }
  63.         public Double getLatitude() { return latitude; }
  64.         public Double getGeoAltitude() { return geoAltitude; }
  65.         public Boolean isOnGround() { return onGround; }
  66.         public Double getVelocity() { return velocity; }
  67.         public Double getHeading() { return heading; }
  68.         public Double getVerticalRate() { return verticalRate; }
  69.         public Integer[] getSensors() { return sensors; }
  70.         public Double getBaroAltitude() { return baroAltitude; }
  71.         public String getSquawk() { return squawk; }
  72.         public Boolean getSpi() { return spi; }
  73.         public Integer getPositionSource() { return positionSource; }
  74.        
  75.         @Override
  76.         public String toString() {
  77.             return "[" + icao24 + "," + callSign + "," + originCountry + ","
  78.                     + timePosition + "," + lastContact + "," + longitude + ","
  79.                     + latitude + "," + geoAltitude + "," + onGround + ","
  80.                     + velocity + "," + heading + "," + verticalRate + ","
  81.                     + sensors + "," + baroAltitude + "," + squawk + ","
  82.                     + spi + "," + positionSource + "]";
  83.         }
  84.     }
  85.    
  86.     private static class JsonObject {
  87.        
  88.         private String jsonString;
  89.         private int jsonLength;
  90.         private int clsCrlBrcInd;
  91.         private List<Integer> bracketIndexes;
  92.        
  93.         JsonObject(String jsonString) {
  94.             this.jsonString = jsonString;
  95.             this.jsonLength = jsonString.length();
  96.             this.clsCrlBrcInd = jsonString.length() - 1;
  97.             this.bracketIndexes = new ArrayList<>();
  98.             initIndexLists(jsonString);
  99.         }
  100.        
  101.         private void initIndexLists(String jsonString) {
  102.             for (int index = 0; index < jsonString.length(); index++) {
  103.                 char currentCharacter = jsonString.charAt(index);
  104.                
  105.                 if (currentCharacter == '[' || currentCharacter == ']') {
  106.                     this.bracketIndexes.add(index);
  107.                 }
  108.             }
  109.         }
  110.        
  111.         public String getJsonString() { return jsonString; }
  112.         public int getJsonLength() { return jsonLength; }
  113.         public int getClsCrlBrcInd() { return clsCrlBrcInd; }
  114.         public List<Integer> getBrackIndexes() { return bracketIndexes; }
  115.     }
  116.    
  117.     private static List<Plane> getPlanesFromApi(JsonObject json) {
  118.         List<Plane> planes = new ArrayList<>();
  119.         List<Integer> brackIndxs = json.getBrackIndexes();
  120.        
  121.         for (int currOpenBrackInd = brackIndxs.get(1);
  122.                 (brackIndxs.get(brackIndxs.indexOf(currOpenBrackInd) + 2) + 1) != json.getClsCrlBrcInd(); ) {
  123.            
  124.             int nextBrackInd = brackIndxs.get(brackIndxs.indexOf(currOpenBrackInd) + 1);
  125.             int currObjClsBrack = nextBrackInd;
  126.            
  127.             if (json.getJsonString().charAt(nextBrackInd) == '[') {
  128.                 int arrayOpnBrack = nextBrackInd;
  129.                 int arrayClsBrack = brackIndxs.get(brackIndxs.indexOf(arrayOpnBrack) + 1);
  130.                 currObjClsBrack = brackIndxs.get(brackIndxs.indexOf(currOpenBrackInd) + 3);
  131.                
  132.                 String jsonString =
  133.                         json.getJsonString().substring(currOpenBrackInd + 1, currObjClsBrack);
  134.                
  135.                 String jsonBeforeArray = jsonString.substring(0, arrayOpnBrack);
  136.                 String jsonAfterArray = jsonString.substring(arrayClsBrack + 1, currObjClsBrack);
  137.                
  138.                 String[] valuesBeforeArray = jsonBeforeArray.split(",");
  139.                 String[] valuesAfterArray = jsonAfterArray.split(",");
  140.                
  141.                 System.out.println(valuesAfterArray.length);
  142.                
  143.                 if (valuesBeforeArray.length != 12 || valuesAfterArray.length != 4) {
  144.                     throw new IllegalStateException("Incorrect array length");
  145.                 }
  146.                
  147.                 String[] sensorsArray =
  148.                         jsonString.substring(arrayOpnBrack + 1, arrayClsBrack).split(",");
  149.                
  150.                 planes.add(new Plane(
  151.                         nullSafeStripQuotes(valuesBeforeArray[0]),
  152.                         nullSafeStripQuotes(valuesBeforeArray[1]),
  153.                         nullSafeStripQuotes(valuesBeforeArray[2]),
  154.                         nullSafeParseInt(valuesBeforeArray[3]),
  155.                         nullSafeParseInt(valuesBeforeArray[4]),
  156.                         nullSafeParseDouble(valuesBeforeArray[5]),
  157.                         nullSafeParseDouble(valuesBeforeArray[6]),
  158.                         nullSafeParseDouble(valuesBeforeArray[7]),
  159.                         nullSafeParseBoolean(valuesBeforeArray[8]),
  160.                         nullSafeParseDouble(valuesBeforeArray[9]),
  161.                         nullSafeParseDouble(valuesBeforeArray[10]),
  162.                         nullSafeParseDouble(valuesBeforeArray[11]),
  163.                         convertStringArrayToIntegerArray(sensorsArray),
  164.                         nullSafeParseDouble(valuesAfterArray[0]),
  165.                         nullSafeStripQuotes(valuesAfterArray[1]),
  166.                         nullSafeParseBoolean(valuesAfterArray[2]),
  167.                         nullSafeParseInt(valuesAfterArray[3])));
  168.                
  169.                 currOpenBrackInd =
  170.                         brackIndxs.get(brackIndxs.indexOf(currObjClsBrack) + 1);
  171.             } else {
  172.                 String jsonString =
  173.                         json.getJsonString().substring(currOpenBrackInd + 1, currObjClsBrack);
  174.                
  175.                 String[] valueArray = jsonString.split(",");
  176.                
  177.                 planes.add(new Plane(
  178.                         nullSafeStripQuotes(valueArray[0]),
  179.                         nullSafeStripQuotes(valueArray[1]),
  180.                         nullSafeStripQuotes(valueArray[2]),
  181.                         nullSafeParseInt(valueArray[3]),
  182.                         nullSafeParseInt(valueArray[4]),
  183.                         nullSafeParseDouble(valueArray[5]),
  184.                         nullSafeParseDouble(valueArray[6]),
  185.                         nullSafeParseDouble(valueArray[7]),
  186.                         nullSafeParseBoolean(valueArray[8]),
  187.                         nullSafeParseDouble(valueArray[9]),
  188.                         nullSafeParseDouble(valueArray[10]),
  189.                         nullSafeParseDouble(valueArray[11]),
  190.                         null,
  191.                         nullSafeParseDouble(valueArray[13]),
  192.                         nullSafeStripQuotes(valueArray[14]),
  193.                         nullSafeParseBoolean(valueArray[15]),
  194.                         nullSafeParseInt(valueArray[16])));
  195.                
  196.                 currOpenBrackInd =
  197.                         brackIndxs.get(brackIndxs.indexOf(currObjClsBrack) + 1);
  198.             }
  199.         }
  200.        
  201.         return planes;
  202.     }
  203.    
  204.     private static String nullSafeStripQuotes(String possibleString) {
  205.         if (possibleString.equals("null")) {
  206.             return null;
  207.         } else {
  208.             return stripQuotes(possibleString);
  209.         }
  210.     }
  211.    
  212.     private static Integer nullSafeParseInt(String possibleInt) {
  213.         if (possibleInt.equals("null")) {
  214.             return null;
  215.         } else {
  216.             return Integer.parseInt(possibleInt);
  217.         }
  218.     }
  219.    
  220.     private static Double nullSafeParseDouble(String possibleFloat) {
  221.         if (possibleFloat.equals("null")) {
  222.             return null;
  223.         } else {
  224.             return Double.parseDouble(possibleFloat);
  225.         }
  226.     }
  227.    
  228.     private static Boolean nullSafeParseBoolean(String possibleBoolean) {
  229.         if (possibleBoolean.equals("null")) {
  230.             return null;
  231.         } else {
  232.             return Boolean.parseBoolean(possibleBoolean);
  233.         }
  234.     }
  235.    
  236.     private static String getHttpResponse(String urlString) throws Exception {
  237.         StringBuilder response = new StringBuilder();
  238.        
  239.         try {
  240.             URL url = new URL(urlString);
  241.        
  242.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  243.            
  244.             conn.setRequestMethod("GET");
  245.            
  246.             try (BufferedReader br = new BufferedReader(
  247.                     new InputStreamReader(conn.getInputStream()))) {
  248.                 String line;
  249.                 while ((line = br.readLine()) != null) {
  250.                     response.append(line);
  251.                 }
  252.             } catch (Exception e) {
  253.                 e.printStackTrace();
  254.             }
  255.            
  256.         } catch (MalformedURLException e) {
  257.             e.printStackTrace();
  258.         }
  259.        
  260.         return response.toString();
  261.     }
  262.    
  263.     private static Integer[] convertStringArrayToIntegerArray(String[] stringArray) {
  264.         Integer[] intArray = new Integer[stringArray.length];
  265.        
  266.         for (int i = 0; i < stringArray.length; i++) {
  267.             intArray[i] = nullSafeParseInt(stringArray[i]);
  268.         }
  269.        
  270.         return intArray;
  271.     }
  272.    
  273.     private static String stripQuotes(String stringWithQuotes) {
  274.         return stringWithQuotes.substring(1, stringWithQuotes.length() - 1);
  275.     }
  276.    
  277.     private static Plane getPlaneClosestTo(double latitude, double longitude, List<Plane> planes) {
  278.         Plane closestPlane = planes.get(0);
  279.        
  280.         for (Plane plane : planes) {
  281.             if (plane.getLatitude() == null || plane.getLongitude() == null) {
  282.                 continue;
  283.             }
  284.            
  285.             if (getDistanceBetween(latitude, longitude,
  286.                     plane.getLatitude(), plane.getLongitude())
  287.                  < getDistanceBetween(latitude, longitude,
  288.                     closestPlane.getLatitude(), closestPlane.getLongitude())) {
  289.                closestPlane = plane;
  290.             }
  291.         }
  292.        
  293.         return closestPlane;
  294.     }
  295.    
  296.     private static double getDistanceBetween(double x1, double y1, double x2, double y2) {
  297.         return Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow(y1 - y2, 2));
  298.     }
  299.  
  300.     public static void main(String[] args) throws Exception {
  301.         JsonObject apiCall = new JsonObject(
  302.                 getHttpResponse("https://opensky-network.org/api/states/all"));
  303.        
  304.         List<Plane> planes = getPlanesFromApi(apiCall);
  305.        
  306.         Plane closestPlaneToEifelTower =
  307.                 getPlaneClosestTo(48.8584, 2.2945, planes);
  308.         Plane closestPlaneToJohnFKennedyAirport =
  309.                 getPlaneClosestTo(40.6413, 73.7781, planes);
  310.        
  311.         System.out.println("Closest plane to Eifel Tower:");
  312.         System.out.println("Callsign: " + closestPlaneToEifelTower.getCallSign());
  313.         System.out.println("Latitude: " + closestPlaneToEifelTower.getLatitude());
  314.         System.out.println("Longitude: " + closestPlaneToEifelTower.getLongitude());
  315.         System.out.println("Geometric Altitude: " + closestPlaneToEifelTower.getGeoAltitude());
  316.         System.out.println("Country of origin: " + closestPlaneToEifelTower.getOriginCountry());
  317.         System.out.println("ICAO24 ID: " + closestPlaneToEifelTower.getIcao24());
  318.         System.out.println();
  319.         System.out.println("Closest plane to John F. Kennedy Airport:");
  320.         System.out.println("Callsign: " + closestPlaneToJohnFKennedyAirport.getCallSign());
  321.         System.out.println("Latitude: " + closestPlaneToJohnFKennedyAirport.getLatitude());
  322.         System.out.println("Longitude: " + closestPlaneToJohnFKennedyAirport.getLongitude());
  323.         System.out.println("Geometric Altitude: " + closestPlaneToJohnFKennedyAirport.getGeoAltitude());
  324.         System.out.println("Country of origin: " + closestPlaneToJohnFKennedyAirport.getOriginCountry());
  325.         System.out.println("ICAO24 ID: " + closestPlaneToJohnFKennedyAirport.getIcao24());
  326.     }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement