Created
November 20, 2017 06:54
-
-
Save apelsocz/687b27279eb5dc2e034d1ef6a0e5cac8 to your computer and use it in GitHub Desktop.
wip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.pelsoczi.javaeight; | |
| import org.json.JSONObject; | |
| class City { | |
| public String city; | |
| public String growth; | |
| public String latitude; | |
| public String longitude; | |
| public int population; | |
| public int rank; | |
| public String state; | |
| public City(JSONObject cityAsJson) { | |
| city = cityAsJson.optString("city"); | |
| growth = cityAsJson.optString("growth_from_2000_to_2013"); | |
| latitude = cityAsJson.optString("latitude"); | |
| longitude = cityAsJson.optString("longitude"); | |
| population = cityAsJson.optInt("population"); | |
| rank = cityAsJson.optInt("population"); | |
| state = cityAsJson.optString("state"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.pelsoczi.javaeight; | |
| import android.util.Log; | |
| import org.json.JSONObject; | |
| import java.util.List; | |
| import javax.xml.transform.Result; | |
| import java9.util.function.Function; | |
| import java9.util.stream.Collectors; | |
| import java9.util.stream.StreamSupport; | |
| public class CityPopulator implements Function<List<JSONObject>, List<City>> { | |
| @Override | |
| public List<City> apply(List<JSONObject> citiesJson) { | |
| return StreamSupport.stream(citiesJson) | |
| .map(City::new) | |
| .collect(Collectors.toList()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.pelsoczi.javaeight; | |
| import android.util.Log; | |
| import org.json.JSONObject; | |
| import java.util.List; | |
| import java9.util.Optional; | |
| import java9.util.concurrent.CompletableFuture; | |
| import java9.util.function.Consumer; | |
| import java9.util.stream.StreamSupport; | |
| class CityProvider { | |
| public static final String TAG = CityProvider.class.getSimpleName(); | |
| void go(Optional<String> citiesJson) { | |
| CompletableFuture<List<City>> future = | |
| CompletableFuture.supplyAsync(new CitySupplier(citiesJson)) | |
| .thenApply(new CityPopulator()); | |
| // CompletableFuture<Void> futureOutput = | |
| // future.thenAcceptAsync(this::msg) | |
| // .exceptionally(ex -> { | |
| // Log.e(".futureOutput", ex.getMessage()); | |
| // return null; | |
| // }); | |
| CompletableFuture.allOf(future); | |
| Log.i(TAG, ".printCities() completed"); | |
| } | |
| private void msg(List<City> cities) { | |
| // this can be further cleaned up with Method References | |
| StreamSupport.stream(cities).forEach( jsonObject -> { | |
| Log.i("", jsonObject.toString()); | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.pelsoczi.javaeight; | |
| import android.content.res.AssetManager; | |
| import org.json.JSONArray; | |
| import org.json.JSONException; | |
| import org.json.JSONObject; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.Collections; | |
| import java.util.List; | |
| import java9.util.Optional; | |
| import java9.util.function.Supplier; | |
| import java9.util.stream.Stream; | |
| import java9.util.stream.StreamSupport; | |
| public class CitySupplier implements Supplier<List<JSONObject>> { | |
| private Optional<String> jsonOptional = Optional.empty(); | |
| CitySupplier(Optional<String> citiesJson) { | |
| jsonOptional = citiesJson; | |
| } | |
| // static Optional<List<JSONObject>> parseJson() throws JSONException { | |
| // Optional<List<JSONObject>> cities = Optional.empty(); | |
| // | |
| // JSONArray json = new JSONArray(jsonAsString); | |
| // List<JSONObject> list = new ArrayList<>(); | |
| // | |
| // for (int i = 0; i < json.length(); i++) { | |
| // list.add(json.optJSONObject(i)); | |
| // } | |
| // | |
| // return cities; | |
| // } | |
| private List<JSONObject> parseJson() { | |
| /* | |
| * Optional.toString() = return value != null | |
| * ? String.format("Optional[%s]", value) | |
| * : "Optional.empty"; | |
| */ | |
| final String jsonString = jsonOptional.get(); | |
| List<JSONObject> list = Collections.emptyList(); | |
| try { | |
| JSONArray json = new JSONArray(jsonString); // JSONException | |
| for (int i = 0; i < json.length(); i++) { | |
| list.add(json.optJSONObject(i)); | |
| } | |
| } catch (JSONException e) { | |
| e.printStackTrace(); | |
| // type-safe way to return an empty collection. | |
| // compiler recognizes <T> as method | |
| return Collections.emptyList(); | |
| } | |
| return list; | |
| } | |
| /** | |
| * Gets a result. | |
| * | |
| * @return a result | |
| */ | |
| @Override | |
| public List<JSONObject> get() { | |
| return parseJson(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment