Skip to content

Commit

Permalink
Fix Issue #1 Release v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
k33ptoo committed Jul 13, 2021
1 parent bacb0aa commit 9e50734
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 35 deletions.
49 changes: 34 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -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**

Expand All @@ -18,27 +18,46 @@ Code

``` java

//example for javafx
//create your observable list from your Model
private ObservableList<T> data = FXCollections.observableArrayList();

//for java swing you can simply create a List<T>

//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<KRandomUserModel> 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<ObservableList<T>> t = new Task() {
@Override
protected Object call() throws Exception {
ObservableList<T> 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

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.k33ptoo</groupId>
<artifactId>KRandomUser</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>

<packaging>maven-plugin</packaging>
<name>KRandomUser</name>
Expand Down Expand Up @@ -84,8 +84,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<github.global.server>github</github.global.server>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>

<developers>
Expand Down
50 changes: 33 additions & 17 deletions src/main/java/com/k33ptoo/KRandomUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<KRandomUserModel>> t = () -> {
List<KRandomUserModel> list = null;
try {
list = new Gson().fromJson(readUrl("https://random-data-api.com/api/users/random_user?size=" + size), new TypeToken<List<KRandomUserModel>>() {

public static void fetchRandomUserList(int size, KRandomUserCallback randomUserCallback) {
if (size < 101) {
//create a callable
Callable<List<KRandomUserModel>> t = () -> {
return new Gson().fromJson(readUrl("https://random-data-api.com/api/users/random_user?size=" + size), new TypeToken<List<KRandomUserModel>>() {
}.getType());
} catch (Exception e) {
};

FutureTask<List<KRandomUserModel>> 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 {
Expand All @@ -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) {
Expand Down

0 comments on commit 9e50734

Please sign in to comment.