Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
chiquitinxx committed Mar 8, 2023
1 parent c345994 commit 39e67c1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/dev/yila/functional/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.function.BiFunction;

/**
* Class to store 2 sorted values, also known as couple
* Class to store 2 sorted values, also known as couple.
* @param <L> left
* @param <R> right
*/
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dev/yila/functional/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import java.util.stream.Collectors;

/**
* Class to store the result value or the failure
* Class to store the result value or the failure.
* This class is immutable and returns a new instance after any modifying operation.
* @param <T> Type of result value
*/
public class Result<T> {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/dev/yila/functional/agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

/**
* Utility class to create and operate Store classes.
*/
public class Agent {

private Agent() {}

/**
* Store object and returns reference to get it.
* Store value and returns reference to it.
* @param value to store, immutable if possible
* @return Id
*/
Expand All @@ -21,7 +24,7 @@ public static <T> Store<T> create(T value) {
}

/**
* Store completable future and returns reference to get the result.
* Store completable future and returns reference to result.
* @param completableFuture
* @return
* @param <T>
Expand All @@ -35,7 +38,7 @@ public static <T> Store<T> create(CompletableFuture<T> completableFuture) {
}

/**
* Get immutable object stored for an store.
* Get immutable value stored.
* @param store
* @return
* @param <T>
Expand All @@ -45,7 +48,7 @@ public static <T> T get(Store<T> store) {
}

/**
* Change object saved in the store.
* Change value saved in the store.
* @param store
* @param function
* @return
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/dev/yila/functional/agent/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

/**
* Store values that can not be updated concurrently.
* Creating and update values in the store is done asynchronously.
* Only blocks when you get the value stored.
* @param <T>
*/
public class Store<T> {

private volatile CompletableFuture<T> completableFuture;
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/dev/yila/functional/agent/AgentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -90,4 +91,36 @@ void storeAndUpdateImmutable() {
assertEquals(25, updated.intValue());
assertNotSame(bigDecimal, updated);
}

@Test
void notBlockingCreatingStore() {
AtomicInteger atomicInteger = new AtomicInteger(0);
Agent.create(CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(100);
atomicInteger.set(1);
return "hello";
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
}));

assertEquals(0, atomicInteger.get());
}
@Test
void notBlockingUpdatingStore() {
Store<String> string = Agent.create("hello");
AtomicInteger atomicInteger = new AtomicInteger(0);

Agent.update(string, (value) -> {
try {
Thread.sleep(100);
atomicInteger.set(1);
return value.toUpperCase();
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
});
assertEquals(0, atomicInteger.get());
}
}

0 comments on commit 39e67c1

Please sign in to comment.