Skip to content

Commit

Permalink
Formatting and naming cleanup; test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
esocode committed Dec 29, 2023
1 parent 61adcdb commit 096b4b8
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 163 deletions.
10 changes: 5 additions & 5 deletions src/main/java/de/esoco/lib/expression/monad/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public static <T> Call<T> of(ThrowingSupplier<T> supplier) {
* a collection of the results
*/
public static <T> Call<Collection<T>> ofAll(Collection<Call<T>> calls) {
List<ThrowingSupplier<T>> aSuppliers =
List<ThrowingSupplier<T>> suppliers =
calls.stream().map(c -> c.supplier).collect(toList());

return call(
() -> aSuppliers.stream().map(Supplier::get).collect(toList()));
() -> suppliers.stream().map(Supplier::get).collect(toList()));
}

/**
Expand Down Expand Up @@ -262,15 +262,15 @@ public Try<T> toTry() {
* Internal method to apply a {@link #flatMap(Function)} function to the
* result with the correct generic types.
*
* @param fMap The mapping function
* @param map The mapping function
* @return The mapped Call instance
* @throws Exception If the
*/
@SuppressWarnings("unchecked")
<R, N extends Monad<R, Call<?>>> R applyFlatMapping(
Function<? super T, N> fMap) throws Exception {
Function<? super T, N> map) throws Exception {
try {
return fMap.apply(supplier.tryGet()).orFail();
return map.apply(supplier.tryGet()).orFail();
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/de/esoco/lib/expression/monad/Functor.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public interface Functor<T> {
* consuming operations.</p>
*
* @return The functor's value
* @throws Exception An exception signaling an invalid or indeterminable
* @throws Throwable An exception signaling an invalid or indeterminable
* value
*/
T orFail() throws Throwable;
Expand All @@ -88,7 +88,8 @@ public interface Functor<T> {

/**
* A consuming operation that either returns the functor's value or
* throws a mapped exception if a valid value couldn't be determined.
* throws a
* mapped exception if a valid value couldn't be determined.
*
* <p>
* In general, calls to the monadic functions {@link #map(Function)} or
Expand Down Expand Up @@ -127,9 +128,10 @@ default T orUse(T defaultValue) {
* The default implementation invokes {@link #map(Function)}, discards the
* result, and then returns the original value. Some subclasses may be able
* to provide an optimized version. Furthermore, subclasses should
* typically override this method with their own type as the return type
* (but may just invoke the super implementation and cast the returned
* value).
* typically
* override this method with their own type as the return type (but may
* just
* invoke the super implementation and cast the returned value).
*
* @param consumer The consumer of the wrapped value
* @return The resulting functor for chained invocations
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/esoco/lib/expression/monad/Monad.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public interface Monad<T, M extends Monad<?, M>> extends Functor<T> {
*
* <p>Implementations should override this method to return their own type
* but they can simply invoke the default implementation (<code>
* Monad.super.and(rOther, fJoin)</code>) and cast the result to their own
* Monad.super.and(other, join)</code>) and cast the result to their own
* type. The explicit declaration in subclasses is necessary because of the
* limitations of Java's generic type system.</p>
*
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/esoco/lib/expression/monad/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ public static <T> Option<T> of(T value) {
*/
public static <T> Option<Collection<T>> ofAll(
Collection<Option<T>> options) {
Optional<Option<T>> aMissing =
Optional<Option<T>> missing =
options.stream().filter(o -> !o.exists()).findFirst();

return aMissing.isPresent() ?
return missing.isPresent() ?
none() :
Option.of(options.stream().map(tOption -> {
try {
Expand Down Expand Up @@ -312,7 +312,7 @@ public void orElse(Runnable code) {
* @see Functor#orFail()
*/
@Override
public T orFail() throws Throwable {
public T orFail() throws NullPointerException {
if (exists()) {
return value;
} else {
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/de/esoco/lib/expression/monad/Promise.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,18 @@ public static <T> Promise<Collection<T>> ofAll(
Collection<Promise<T>> promises) {
// list needs to be synchronized because the promises may run in
// parallel in which case result.add(t) will be invoked concurrently
int nCount = promises.size();
List<T> result = Collections.synchronizedList(new ArrayList<>(nCount));
int count = promises.size();
List<T> result = Collections.synchronizedList(new ArrayList<>(count));

CompletableFuture<Collection<T>> stage = new CompletableFuture<>();

if (promises.isEmpty()) {
stage.complete(result);
} else {
promises.forEach(rPromise -> rPromise.then(v -> {
promises.forEach(promise -> promise.then(v -> {
result.add(v);

if (result.size() == nCount) {
if (result.size() == count) {
stage.complete(result);
}
}).orElse(stage::completeExceptionally));
Expand All @@ -136,7 +136,7 @@ public static <T> Promise<T> ofAny(Collection<Promise<T>> promises) {

CompletableFuture<T> stage = new CompletableFuture<>();

promises.forEach(rPromise -> rPromise
promises.forEach(promise -> promise
.then(stage::complete)
.orElse(stage::completeExceptionally));

Expand Down Expand Up @@ -403,8 +403,6 @@ static class CompletionStagePromise<T> extends Promise<T> {

/**
* {@inheritDoc}
*
* @throws Exception
*/
@Override
public Promise<T> await() throws Exception {
Expand Down Expand Up @@ -570,7 +568,7 @@ public Promise<T> withTimeout(long time, TimeUnit unit) {
*
* @return The resolved value
* @throws Exception If the stage execution failed or a timeout has
* been
* been
* reached
*/
Promise<T> getValue() throws Exception {
Expand Down
75 changes: 37 additions & 38 deletions src/main/java/de/esoco/lib/expression/monad/Try.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static Try<Void> run(ThrowingRunnable code) {
/**
* Returns an instance that represents a failed execution.
*
* @param value eError The error exception
* @param value error The error exception
* @return The failure instance
*/
public static <T> Try<T> success(T value) {
Expand Down Expand Up @@ -315,7 +315,7 @@ static class Failure<T> extends Try<T> {
/**
* Creates a new instance.
*
* @param error rValue The successfully created value
* @param error value The successfully created value
*/
Failure(Throwable error) {
this.error = error;
Expand Down Expand Up @@ -415,29 +415,29 @@ String getDescribingValue() {
*/
static class Lazy<T> extends Try<T> {

private final ThrowingSupplier<T> fValueSupplier;
private final ThrowingSupplier<T> valueSupplier;

private Option<Try<T>> aResult = Option.none();
private Option<Try<T>> result = Option.none();

/**
* Creates a new instance.
*
* @param fValueSupplier rValue The successfully created value
* @param valueSupplier value The successfully created value
*/
Lazy(ThrowingSupplier<T> fValueSupplier) {
this.fValueSupplier = fValueSupplier;
Lazy(ThrowingSupplier<T> valueSupplier) {
this.valueSupplier = valueSupplier;
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object rObject) {
return this == rObject || (rObject instanceof Lazy &&
(aResult.exists() ?
Objects.equals(aResult, ((Lazy<?>) rObject).aResult) :
Objects.equals(fValueSupplier,
((Lazy<?>) rObject).fValueSupplier)));
public boolean equals(Object object) {
return this == object || (object instanceof Lazy &&
(result.exists() ?
Objects.equals(result, ((Lazy<?>) object).result) :
Objects.equals(valueSupplier,
((Lazy<?>) object).valueSupplier)));
}

/**
Expand All @@ -454,8 +454,7 @@ public <R, N extends Monad<R, Try<?>>> Try<R> flatMap(
*/
@Override
public int hashCode() {
return Objects.hashCode(
aResult.exists() ? aResult : fValueSupplier);
return Objects.hashCode(result.exists() ? result : valueSupplier);
}

/**
Expand Down Expand Up @@ -529,15 +528,15 @@ public T orUse(T defaultValue) {
* the
* result of this instance.
*
* @param fMap The mapping function
* @param map The mapping function
* @return The mapped Try instance
* @throws Exception If accessing a try result fails
*/
@SuppressWarnings("unchecked")
<R, N extends Monad<R, Try<?>>> R applyFlatMapping(
Function<? super T, N> fMap) throws Exception {
Function<? super T, N> map) throws Exception {
try {
return fMap.apply(getResult().orFail()).orFail();
return map.apply(getResult().orFail()).orFail();
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
Expand All @@ -549,7 +548,7 @@ <R, N extends Monad<R, Try<?>>> R applyFlatMapping(
@Override
Object getDescribingValue() {
try {
return aResult.exists() ? aResult.orFail() : fValueSupplier;
return result.exists() ? result.orFail() : valueSupplier;
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
Expand All @@ -562,18 +561,18 @@ Object getDescribingValue() {
* @return A try of the (evaluated) result
*/
Try<T> getResult() {
if (!aResult.exists()) {
aResult = Option.of(now(fValueSupplier));
if (!result.exists()) {
result = Option.of(now(valueSupplier));

// if supplier returned NULL convert to success
if (!aResult.exists()) {
aResult = Option.of(success(null));
if (!result.exists()) {
result = Option.of(success(null));
}
}

// this will never fail
try {
return aResult.orFail();
return result.orFail();
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
Expand All @@ -587,24 +586,24 @@ Try<T> getResult() {
*/
static class Success<T> extends Try<T> {

private final T rValue;
private final T value;

/**
* Creates a new instance.
*
* @param rValue The successfully created value
* @param value The successfully created value
*/
Success(T rValue) {
this.rValue = rValue;
Success(T value) {
this.value = value;
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object rObject) {
return this == rObject || (rObject instanceof Success &&
Objects.equals(rValue, ((Success<?>) rObject).rValue));
public boolean equals(Object object) {
return this == object || (object instanceof Success &&
Objects.equals(value, ((Success<?>) object).value));
}

/**
Expand All @@ -614,15 +613,15 @@ public boolean equals(Object rObject) {
@SuppressWarnings("unchecked")
public <R, N extends Monad<R, Try<?>>> Try<R> flatMap(
Function<? super T, N> mapper) {
return (Try<R>) mapper.apply(rValue);
return (Try<R>) mapper.apply(value);
}

/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hashCode(rValue);
return Objects.hashCode(value);
}

/**
Expand All @@ -646,15 +645,15 @@ public Try<T> orElse(Consumer<Throwable> handler) {
*/
@Override
public final T orFail() throws Throwable {
return rValue;
return value;
}

/**
* {@inheritDoc}
*/
@Override
public T orGet(Supplier<T> supplier) {
return rValue;
return value;
}

/**
Expand All @@ -663,23 +662,23 @@ public T orGet(Supplier<T> supplier) {
@Override
public <E extends Throwable> T orThrow(
Function<Throwable, E> exceptionMapper) throws E {
return rValue;
return value;
}

/**
* {@inheritDoc}
*/
@Override
public T orUse(T defaultValue) {
return rValue;
return value;
}

/**
* {@inheritDoc}
*/
@Override
Object getDescribingValue() {
return rValue;
return value;
}
}
}
Loading

0 comments on commit 096b4b8

Please sign in to comment.