From 9e50734312e78dc7225a6263cbb41fa3873ec31d Mon Sep 17 00:00:00 2001 From: Amos Chepchieng Date: Tue, 13 Jul 2021 17:53:43 +0300 Subject: [PATCH] Fix Issue #1 Release v1.0.1 --- README.md | 49 ++++++++++++++------- pom.xml | 6 +-- src/main/java/com/k33ptoo/KRandomUser.java | 50 ++++++++++++++-------- 3 files changed, 70 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 087608f..9376574 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # KRandomUser -A JavaFX /Java Swing Consumable Library for getting random users data with abundant details from api provided by https://random-data-api.com/ +A JavaFX /Java Swing Consumable Library for getting 100 random users data with abundant details from api provided by https://random-data-api.com/ **Usage** @@ -18,27 +18,46 @@ Code ``` java -//example for javafx -//create your observable list from your Model -private ObservableList data = FXCollections.observableArrayList(); - //for java swing you can simply create a List //add the random data from the lib -//specify the size of data you want to be generated - - try { - KRandomUser.fetchRandomUserList(30, list -> { - for (KRandomUserModel f : list - ) { - //populate your javafx list e.g - data.add(new T(f.getFirstName() + " " + f.getLastName(), f.getPhoneNumber(), f.getEmail() /*etc*/)); +//specify the size of data you want to be generated api allow max of 100 + + /** + * How to use on Java Swing + * InvokeLater to prevent UI Blocking + */ + SwingUtilities.invokeLater(()->{ + KRandomUser.fetchRandomUserList(50, (List data) -> { + //do something with the list of data + }); + }); + + + /** + * How to use on JavaFX without blocking the UI + * You have to run on a task + */ + ExecutorService es = Executors.newCachedThreadPool(); + Task> t = new Task() { + @Override + protected Object call() throws Exception { + ObservableList oLst = FXCollections.observableArrayList(); + KRandomUser.fetchRandomUserList(50, list -> { + for (KRandomUserModel f : list) { + oLst.add(/*whatever infor you want*/); } }); - } catch (Exception e) { - e.printStackTrace(); + return oLst; } + }; + es.submit(t); + t.setOnSucceeded((evt) -> { + tableView.setItems(t.getValue()); + }); + + ``` Other user details are as follows diff --git a/pom.xml b/pom.xml index 1c4334d..ad4a9d6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.k33ptoo KRandomUser - 1.0.0 + 1.0.1 maven-plugin KRandomUser @@ -84,8 +84,8 @@ UTF-8 github - 1.8 - 1.8 + 9 + 9 diff --git a/src/main/java/com/k33ptoo/KRandomUser.java b/src/main/java/com/k33ptoo/KRandomUser.java index e3ec651..7f3908d 100644 --- a/src/main/java/com/k33ptoo/KRandomUser.java +++ b/src/main/java/com/k33ptoo/KRandomUser.java @@ -7,34 +7,50 @@ import java.net.URL; import java.util.List; import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.FutureTask; +import java.util.logging.Level; +import java.util.logging.Logger; /** * * @author KeepToo */ public class KRandomUser { - + private static final ExecutorService executorService = Executors.newCachedThreadPool(); - - public static void fetchRandomUserList(int size, KRandomUserCallback randomUserCallback) throws InterruptedException, Exception { - - //create a task - Callable> t = () -> { - List list = null; - try { - list = new Gson().fromJson(readUrl("https://random-data-api.com/api/users/random_user?size=" + size), new TypeToken>() { + + public static void fetchRandomUserList(int size, KRandomUserCallback randomUserCallback) { + if (size < 101) { + //create a callable + Callable> t = () -> { + return new Gson().fromJson(readUrl("https://random-data-api.com/api/users/random_user?size=" + size), new TypeToken>() { }.getType()); - } catch (Exception e) { + }; + + FutureTask> ft = new FutureTask<>(t); + //submit the task + executorService.submit(ft); + try { + randomUserCallback.onFinish(ft.get()); + } catch (InterruptedException | ExecutionException ex) { + Logger.getLogger(KRandomUser.class.getName()).log(Level.SEVERE, null, ex); + } + if (ft.isDone()) { + executorService.shutdown(); } - return list; - }; - //submit task - executorService.submit(t); - randomUserCallback.onFinish(t.call()); + } else { + try { + throw new Exception("Sorry the api only allows maximum list of 100"); + } catch (Exception ex) { + Logger.getLogger(KRandomUser.class.getName()).log(Level.SEVERE, null, ex); + } + } + } - + private static String readUrl(String urlString) throws Exception { BufferedReader reader = null; try { @@ -46,7 +62,7 @@ private static String readUrl(String urlString) throws Exception { while ((read = reader.read(chars)) != -1) { buffer.append(chars, 0, read); } - + return buffer.toString(); } finally { if (reader != null) {