Skip to content

Commit

Permalink
Merge pull request #117 from tonivade/feature/full-java21
Browse files Browse the repository at this point in the history
Feature/full java21
  • Loading branch information
tonivade authored Feb 10, 2024
2 parents 72fb479 + 249f343 commit 4fde4ba
Show file tree
Hide file tree
Showing 30 changed files with 285 additions and 534 deletions.
14 changes: 10 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,23 @@ allprojects {
}

jacoco {
toolVersion = "0.8.10"
toolVersion = "0.8.11"
}

javadoc.options {
addStringOption('-release', '21')
}

compileJava {
options.compilerArgs << '-Xlint:unchecked'
options.compilerArgs << '-Xlint:rawtypes'
options.release = 21
}

compileTestJava {
options.compilerArgs << '-Xlint:unchecked'
options.compilerArgs << '-Xlint:rawtypes'
options.release = 21
}

jacocoTestReport {
Expand All @@ -107,8 +113,8 @@ allprojects {
subprojects {

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

withJavadocJar()
withSourcesJar()
Expand All @@ -128,7 +134,7 @@ subprojects {
if (it.getName().contains("Java21")) {
it.options.release = 21
} else {
it.options.release = 17
it.options.release = 21
}
}

Expand Down
27 changes: 0 additions & 27 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
sourceSets {
java21 {
java {
srcDirs = ['src/main/java21']
}
}
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

withJavadocJar()
withSourcesJar()
}

compileJava21Java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

jar {
into('META-INF/versions/21') {
from sourceSets.java21.output
}
manifest.attributes('Multi-Release': 'true')
}

dependencies {
annotationProcessor projects.purefunProcessor
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
@HigherKind
public sealed interface Future<T> extends FutureOf<T>, Bindable<Future_, T> {

Executor DEFAULT_EXECUTOR = DefaultExecutor.EXECUTOR;
Executor DEFAULT_EXECUTOR = Executors.newVirtualThreadPerTaskExecutor();

Try<T> await();
Try<T> await(Duration timeout);
Expand Down Expand Up @@ -133,7 +133,7 @@ default Future<T> recover(Function1<? super Throwable, ? extends T> mapper) {
<X extends Throwable> Future<T> recoverWith(Class<X> type, Function1<? super X, ? extends T> mapper);

<U> Future<U> fold(
Function1<? super Throwable, ? extends U> failureMapper,
Function1<? super Throwable, ? extends U> failureMapper,
Function1<? super T, ? extends U> successMapper);

default CompletableFuture<T> toCompletableFuture() {
Expand Down Expand Up @@ -233,41 +233,41 @@ static <T> Future<T> later(Executor executor, Producer<? extends T> producer) {
return task(executor, producer::get);
}

static <T extends AutoCloseable, R> Future<R> bracket(Future<? extends T> acquire,
static <T extends AutoCloseable, R> Future<R> bracket(Future<? extends T> acquire,
Function1<? super T, ? extends Future<? extends R>> use) {
return bracket(DEFAULT_EXECUTOR, acquire, use);
}

static <T extends AutoCloseable, R> Future<R> bracket(Executor executor,
Future<? extends T> acquire,
static <T extends AutoCloseable, R> Future<R> bracket(Executor executor,
Future<? extends T> acquire,
Function1<? super T, ? extends Future<? extends R>> use) {
return FutureImpl.bracket(executor, acquire, use, AutoCloseable::close);
}

static <T, R> Future<R> bracket(Future<? extends T> acquire,
Function1<? super T, ? extends Future<? extends R>> use,
static <T, R> Future<R> bracket(Future<? extends T> acquire,
Function1<? super T, ? extends Future<? extends R>> use,
Consumer1<? super T> release) {
return bracket(DEFAULT_EXECUTOR, acquire, use, release);
}

static <T, R> Future<R> bracket(Executor executor,
Future<? extends T> acquire,
Function1<? super T, ? extends Future<? extends R>> use,
static <T, R> Future<R> bracket(Executor executor,
Future<? extends T> acquire,
Function1<? super T, ? extends Future<? extends R>> use,
Consumer1<? super T> release) {
return FutureImpl.bracket(executor, acquire, use, release);
}

// TODO
static <A> Future<Sequence<A>> traverse(Sequence<Future<A>> sequence) {
return sequence.foldLeft(success(ImmutableList.empty()),
return sequence.foldLeft(success(ImmutableList.empty()),
(Future<Sequence<A>> xs, Future<A> a) -> map2(xs, a, Sequence::append));
}
static <T, V, R> Future<R> map2(Future<? extends T> fa, Future<? extends V> fb,

static <T, V, R> Future<R> map2(Future<? extends T> fa, Future<? extends V> fb,
Function2<? super T, ? super V, ? extends R> mapper) {
return fb.ap(fa.map(mapper.curried()));
}

static <T, V> Future<Tuple2<T, V>> tuple(Future<T> fa, Future<V> fb) {
return map2(fa, fb, Tuple2::of);
}
Expand All @@ -287,7 +287,7 @@ final class FutureImpl<T> implements Future<T> {
private final Propagate propagate;
private final Promise<T> promise;
private final Cancellable cancellable;

private final UUID uuid;

private FutureImpl(Executor executor, Callback<T> callback) {
Expand Down Expand Up @@ -345,7 +345,7 @@ public Try<T> await(Duration timeout) {
public <R> Future<R> map(Function1<? super T, ? extends R> mapper) {
return transform(value -> value.map(mapper));
}

@Override
public Future<T> mapError(Function1<? super Throwable, ? extends Throwable> mapper) {
return transform(value -> value.mapError(mapper));
Expand All @@ -360,11 +360,11 @@ public <R> Future<R> flatMap(Function1<? super T, ? extends Kind<Future_, ? exte
public <R> Future<R> andThen(Future<? extends R> next) {
return flatMap(ignore -> next);
}

@Override
public <R> Future<R> ap(Future<Function1<? super T, ? extends R>> apply) {
checkNonNull(apply);
return new FutureImpl<>(executor,
return new FutureImpl<>(executor,
(p, c) -> promise.onComplete(try1 -> apply.onComplete(
try2 -> p.tryComplete(Try.map2(try2, try1, Function1::apply)))), this::cancel);
}
Expand Down Expand Up @@ -406,7 +406,7 @@ public void cancel(boolean mayInterruptThread) {
public Promise<T> toPromise() {
return promise;
}

@Override
public String toString() {
return "Future(" + uuid + ')';
Expand Down Expand Up @@ -447,7 +447,7 @@ static <T> Future<T> async(Executor executor,
Consumer1<Consumer1<? super Try<? extends T>>> consumer) {
checkNonNull(executor);
checkNonNull(consumer);
return new FutureImpl<>(executor,
return new FutureImpl<>(executor,
(p, c) -> Future.later(executor, () -> {
c.updateThread();
return consumer.asFunction().apply(p::tryComplete);
Expand Down Expand Up @@ -494,7 +494,7 @@ static Executor delayedExecutor(Duration delay, Executor executor) {
}

interface Callback<T> {

void accept(Promise<T> promise, Cancellable cancellable);
}

Expand Down
40 changes: 5 additions & 35 deletions core/src/main/java/com/github/tonivade/purefun/data/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,25 @@
*/
package com.github.tonivade.purefun.data;

import com.github.tonivade.purefun.Equal;
import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Tuple;

import java.io.Serial;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Objects;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static com.github.tonivade.purefun.Function1.identity;
import static com.github.tonivade.purefun.Precondition.check;
import static com.github.tonivade.purefun.Precondition.greaterThanOrEquals;
import static com.github.tonivade.purefun.type.Validation.mapN;
import static com.github.tonivade.purefun.type.Validation.requireGreaterThanOrEqual;
import static com.github.tonivade.purefun.type.Validation.requireLowerThan;
import static com.github.tonivade.purefun.type.Validation.requireLowerThanOrEqual;

public final class Range implements Iterable<Integer>, Serializable {
public record Range(int begin, int end) implements Iterable<Integer> {

@Serial
private static final long serialVersionUID = 7923835507243835436L;

private static final Equal<Range> EQUAL =
Equal.<Range>of().comparing(x -> x.begin).comparing(x -> x.end);

private final int begin;
private final int end;

private Range(int begin, int end) {
this.begin = begin;
this.end = end;
}

public int begin() {
return begin;
}

public int end() {
return end;
public Range {
check(greaterThanOrEquals(end, begin));
}

public boolean contains(int value) {
Expand Down Expand Up @@ -76,16 +56,6 @@ public Iterator<Integer> iterator() {
return intStream().iterator();
}

@Override
public boolean equals(Object obj) {
return EQUAL.applyTo(this, obj);
}

@Override
public int hashCode() {
return Objects.hash(begin, end);
}

@Override
public String toString() {
return String.format("Range(%d..%d)", begin, end);
Expand Down
31 changes: 3 additions & 28 deletions core/src/main/java/com/github/tonivade/purefun/type/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,20 @@

import static com.github.tonivade.purefun.Precondition.checkNonNull;

import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import com.github.tonivade.purefun.Equal;
import com.github.tonivade.purefun.HigherKind;

@HigherKind
public final class Const<T, A> implements ConstOf<T, A>, Serializable {
public record Const<T, A>(T value) implements ConstOf<T, A> {

@Serial
private static final long serialVersionUID = 7431389527943145565L;

private static final Equal<Const<?, ?>> EQUAL = Equal.<Const<?, ?>>of().comparing(Const::get);

private final T value;

private Const(T value) {
this.value = checkNonNull(value);
}

public T get() {
return value;
public Const {
checkNonNull(value);
}

@SuppressWarnings("unchecked")
public <B> Const<T, B> retag() {
return (Const<T, B>) this;
}

@Override
public int hashCode() {
return Objects.hash(value);
}

@Override
public boolean equals(Object obj) {
return EQUAL.applyTo(this, obj);
}

@Override
public String toString() {
return "Const(" + value + ")";
Expand Down
Loading

0 comments on commit 4fde4ba

Please sign in to comment.