Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/main/java/mate/academy/AsyncRequestProcessor.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
package mate.academy;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;

public class AsyncRequestProcessor {
private final Executor executor;
private final Map<String, UserData> cache;
Comment thread
unknownpanic marked this conversation as resolved.

public AsyncRequestProcessor(Executor executor) {
this.executor = executor;
this.cache = new ConcurrentHashMap<>();
}

public CompletableFuture<UserData> processRequest(String userId) {
return null;
if (cache.containsKey(userId)) {
return CompletableFuture.completedFuture(cache.get(userId));
}

return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
Comment on lines +21 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The toString() method uses curly braces {} but the expected output requires square brackets []. Change lines 22 and 25 to use [ and ] instead.

throw new RuntimeException(e);
}
return "Details for " + userId;
}, executor).thenApply(details -> {
UserData userData = new UserData(userId, details);
cache.put(userId, userData);
return userData;
});
}
}
8 changes: 3 additions & 5 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
public static void main(String[] args) {
// Feel free to play with AsyncRequestProcessor in this main method if you want
ExecutorService executor = null; // Provide implementation that fits your needs
ExecutorService executor = Executors.newCachedThreadPool();
AsyncRequestProcessor asyncRequestProcessor = new AsyncRequestProcessor(executor);

// Simulating multiple concurrent requests
String[] userIds = {"user1", "user2", "user3", "user1"}; // Note: "user1" is repeated
String[] userIds = {"user1", "user2", "user3", "user1"};
CompletableFuture<?>[] futures = new CompletableFuture[userIds.length];

for (int i = 0; i < userIds.length; i++) {
Expand All @@ -19,7 +18,6 @@ public static void main(String[] args) {
.thenAccept(userData -> System.out.println("Processed: " + userData));
}

// Wait for all futures to complete
CompletableFuture.allOf(futures).join();
executor.shutdown();
}
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/mate/academy/UserData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package mate.academy;

public record UserData(String userId, String details) {
public class UserData {
private final String userId;
private String details;

public UserData(String userId, String details) {
this.userId = userId;
this.details = details;
}

public String userId() {
return userId;
}

public String details() {
return details;
}

@Override
public String toString() {
return "UserData{"
+ "userId=" + userId
+ ", details=" + details
+ '}';
Comment on lines +21 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The toString() format uses curly braces {} but the expected output requires square brackets []. Change {userId= to [userId= and } to ]

}
}
Loading