Skip to content

Commit a5fe4d7

Browse files
committed
Add annotations
1 parent 966bcab commit a5fe4d7

File tree

10 files changed

+142
-66
lines changed

10 files changed

+142
-66
lines changed

common/addons/command-locate/src/main/java/com/dfsek/terra/addons/commands/locate/LocateCommandAddon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.dfsek.terra.api.world.World;
2828
import com.dfsek.terra.api.world.biome.Biome;
2929

30-
import static com.dfsek.terra.api.util.function.FunctionUtils.collapse;
30+
import static com.dfsek.terra.api.util.generic.data.types.Either.collapse;
3131

3232

3333
public class LocateCommandAddon implements AddonInitializer {

common/api/src/main/java/com/dfsek/terra/api/util/function/FunctionUtils.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,39 @@
22

33
import com.dfsek.terra.api.util.generic.data.types.Either;
44

5-
import java.util.Optional;
5+
import org.jetbrains.annotations.Contract;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
import java.util.Objects;
69
import java.util.function.Consumer;
710
import java.util.function.Function;
811

912

1013
public final class FunctionUtils {
11-
private FunctionUtils() {}
14+
private FunctionUtils() { }
1215

13-
public static <T> Function<T, T> lift(Consumer<T> c) {
16+
@Contract("_ -> new")
17+
public static <T> @NotNull Function<T, T> lift(@NotNull Consumer<T> c) {
18+
Objects.requireNonNull(c);
1419
return co -> {
1520
c.accept(co);
1621
return co;
1722
};
1823
}
1924

20-
@SuppressWarnings("unchecked")
21-
public static <T, L> Either<L, T> toEither(Optional<T> o, L de) {
22-
return (Either<L, T>) o.map(Either::right).orElseGet(() -> Either.left(de));
23-
}
24-
25-
public static <T> T collapse(Either<T, T> either) {
26-
return either.collect(Function.identity(), Function.identity());
27-
}
28-
29-
public static <T extends Throwable, U> U throw_(T e) throws T {
25+
@Contract("_ -> fail")
26+
public static <T extends Throwable, U> @NotNull U throw_(@NotNull T e) throws T {
3027
throw e;
3128
}
3229

3330
@SuppressWarnings("unchecked")
34-
public static <E extends Throwable, U> U sneakyThrow(Throwable e) throws E {
31+
@Contract("_ -> fail")
32+
public static <E extends Throwable, U> @NotNull U sneakyThrow(@NotNull Throwable e) throws E {
3533
throw (E) e;
3634
}
3735

38-
public static <T, U> Function<T, Either<Exception, U>> liftTry(Function<T, U> f) {
36+
@Contract(pure = true, value = "_ -> new")
37+
public static <T, U> @NotNull Function<T, Either<Exception, U>> liftTry(@NotNull Function<T, U> f) {
3938
return s -> {
4039
try {
4140
return Either.right(f.apply(s));
@@ -45,4 +44,14 @@ public static <T, U> Function<T, Either<Exception, U>> liftTry(Function<T, U> f)
4544
};
4645
}
4746

47+
@Contract(pure = true, value = "_ -> new")
48+
public static <T, U> @NotNull Function<T, Either<Throwable, U>> liftTryUnsafe(@NotNull Function<T, U> f) {
49+
return s -> {
50+
try {
51+
return Either.right(f.apply(s));
52+
} catch(Throwable e) {
53+
return Either.left(e);
54+
}
55+
};
56+
}
4857
}

common/api/src/main/java/com/dfsek/terra/api/util/generic/control/Monad.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,32 @@
33
import com.dfsek.terra.api.util.generic.data.Functor;
44
import com.dfsek.terra.api.util.generic.kinds.K;
55

6+
import org.jetbrains.annotations.Contract;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
import java.util.Objects;
610
import java.util.function.Function;
711

812

913
/**
1014
* A monad is a monoid in the category of endofunctors.
1115
*/
1216
public interface Monad<T, M extends Monad<?, M>> extends Functor<T, M>, K<M, T> {
13-
<T2> Monad<T2, M> bind(Function<T, Monad<T2, M>> map);
17+
@Contract(pure = true, value = "_ -> new")
18+
<T2> @NotNull Monad<T2, M> bind(@NotNull Function<T, Monad<T2, M>> map);
1419

15-
<T1> Monad<T1, M> pure(T1 t);
20+
@Contract(pure = true, value = "_ -> new")
21+
<T1> @NotNull Monad<T1, M> pure(@NotNull T1 t);
1622

1723
@Override
18-
default <U> Monad<U, M> map(Function<T, U> map) {
19-
return bind(map.andThen(this::pure));
24+
@Contract(pure = true, value = "_ -> new")
25+
default <U> @NotNull Monad<U, M> map(@NotNull Function<T, U> map) {
26+
return bind(Objects.requireNonNull(map).andThen(this::pure));
2027
}
2128

2229
// almost all well-known applicative functors are also monads, so we can just put that here.
23-
default <U> Monad<U, M> apply(Monad<Function<T, U>, M> amap) {
24-
return amap.bind(this::map);
30+
@Contract(pure = true, value = "_ -> new")
31+
default <U> @NotNull Monad<U, M> apply(@NotNull Monad<Function<T, U>, M> amap) {
32+
return Objects.requireNonNull(amap).bind(this::map);
2533
}
2634
}

common/api/src/main/java/com/dfsek/terra/api/util/generic/data/BiFunctor.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,41 @@
22

33
import com.dfsek.terra.api.util.generic.data.types.Pair;
44

5+
import org.jetbrains.annotations.Contract;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
import java.util.Objects;
59
import java.util.function.Consumer;
610
import java.util.function.Function;
711

812

913
public interface BiFunctor<T, U, B extends BiFunctor<?, ?, B>> {
10-
static <L, R, B extends BiFunctor<?, ?, B>> Consumer<BiFunctor<L, R, B>> consumeLeft(Consumer<L> consumer) {
14+
@Contract("_ -> new")
15+
static <L, R, B extends BiFunctor<?, ?, B>> @NotNull Consumer<BiFunctor<L, R, B>> consumeLeft(@NotNull Consumer<L> consumer) {
16+
Objects.requireNonNull(consumer);
1117
return pair -> pair.mapLeft(p -> {
1218
consumer.accept(p);
1319
return p;
1420
});
1521
}
1622

17-
static <L, R> Consumer<Pair<L, R>> consumeRight(Consumer<R> consumer) {
23+
@Contract("_ -> new")
24+
static <L, R, B extends BiFunctor<?, ?, B>> @NotNull Consumer<BiFunctor<L, R, B>> consumeRight(@NotNull Consumer<R> consumer) {
25+
Objects.requireNonNull(consumer);
1826
return pair -> pair.mapRight(p -> {
1927
consumer.accept(p);
2028
return p;
2129
});
2230
}
2331

24-
<V> BiFunctor<V, U, B> mapLeft(Function<T, V> map);
25-
<V> BiFunctor<T, V, B> mapRight(Function<U, V> map);
32+
@Contract(pure = true, value = "_ -> new")
33+
<V> @NotNull BiFunctor<V, U, B> mapLeft(@NotNull Function<T, V> map);
34+
35+
@Contract(pure = true, value = "_ -> new")
36+
<V> @NotNull BiFunctor<T, V, B> mapRight(@NotNull Function<U, V> map);
2637

27-
default <V, W> BiFunctor<V, W, B> bimap(Function<T, V> left, Function<U, W> right) {
38+
@Contract(pure = true, value = "_, _-> new")
39+
default <V, W> @NotNull BiFunctor<V, W, B> bimap(@NotNull Function<T, V> left, @NotNull Function<U, W> right) {
2840
return mapLeft(left).mapRight(right);
2941
}
3042
}

common/api/src/main/java/com/dfsek/terra/api/util/generic/data/Functor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import com.dfsek.terra.api.util.generic.kinds.K;
44

5+
import org.jetbrains.annotations.Contract;
6+
import org.jetbrains.annotations.NotNull;
7+
58
import java.util.function.Function;
69

710

811
public interface Functor<T, F extends Functor<?, F>> extends K<F, T> {
9-
<U> Functor<U, F> map(Function<T, U> map);
12+
@Contract(pure = true, value = "_ -> new")
13+
<U> @NotNull Functor<U, F> map(@NotNull Function<T, U> map);
1014
}

common/api/src/main/java/com/dfsek/terra/api/util/generic/data/LinkedList.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,81 @@
33
import com.dfsek.terra.api.util.generic.control.Monad;
44
import com.dfsek.terra.api.util.generic.data.types.Maybe;
55

6+
import org.checkerframework.checker.nullness.qual.NonNull;
7+
import org.checkerframework.dataflow.qual.Pure;
8+
import org.jetbrains.annotations.Contract;
9+
import org.jetbrains.annotations.NotNull;
10+
611
import java.util.ArrayList;
712
import java.util.Collection;
813
import java.util.HashSet;
914
import java.util.List;
15+
import java.util.Objects;
1016
import java.util.Set;
1117
import java.util.function.Function;
1218

1319

1420
public sealed interface LinkedList<T> extends Monad<T, LinkedList<?>>, Monoid<T, LinkedList<?>> {
1521
@Override
16-
<T2> LinkedList<T2> bind(Function<T, Monad<T2, LinkedList<?>>> map);
22+
@Contract(pure = true, value = "_ -> new")
23+
<T2> @NotNull LinkedList<T2> bind(@NotNull Function<T, Monad<T2, LinkedList<?>>> map);
1724

1825
@Override
19-
default <T1> LinkedList<T1> pure(T1 t) {
26+
@Contract(pure = true, value = "_ -> new")
27+
default <T1> @NotNull LinkedList<T1> pure(@NotNull T1 t) {
2028
return of(t);
2129
}
2230

2331
@Override
24-
<U> LinkedList<U> map(Function<T, U> map);
32+
@Contract(pure = true, value = "_ -> new")
33+
<U> @NotNull LinkedList<U> map(@NotNull Function<T, U> map);
2534

2635
@Override
27-
default <T1> LinkedList<T1> identity() {
36+
@Contract(pure = true, value = "-> new")
37+
default <T1> @NotNull LinkedList<T1> identity() {
2838
return empty();
2939
}
3040

41+
@Contract(pure = true, value = "-> new")
3142
default Maybe<T> head() {
3243
return get(0);
3344
}
3445

3546
LinkedList<T> tail();
3647

48+
@Contract(pure = true)
3749
int length();
3850

51+
@Contract(pure = true)
3952
Maybe<T> get(int index);
4053

54+
@Contract(pure = true, value = "_ -> new")
4155
LinkedList<T> add(T value);
4256

43-
default LinkedList<T> prepend(T value) {
44-
return new Cons<>(value, this);
57+
@NotNull
58+
@Contract(pure = true, value = "_ -> new")
59+
default LinkedList<T> prepend(@NotNull T value) {
60+
return new Cons<>(Objects.requireNonNull(value), this);
4561
}
4662

63+
@Contract(mutates = "param")
4764
<C extends Collection<T>> C toCollection(C collection);
4865

66+
@NotNull
67+
@Contract(pure = true, value = "-> new")
4968
default List<T> toList() {
5069
return toCollection(new ArrayList<>());
5170
}
5271

72+
@NotNull
73+
@Contract(pure = true, value = "-> new")
5374
default Set<T> toSet() {
5475
return toCollection(new HashSet<>());
5576
}
5677

5778
@Override
58-
LinkedList<T> multiply(Monoid<T, LinkedList<?>> t);
79+
@NotNull
80+
LinkedList<T> multiply(@NotNull Monoid<T, LinkedList<?>> t);
5981

6082
static <T> LinkedList<T> of(T value) {
6183
return new Cons<>(value, empty());
@@ -68,12 +90,12 @@ static <T> Nil<T> empty() {
6890

6991
record Cons<T>(T value, LinkedList<T> tail) implements LinkedList<T> {
7092
@Override
71-
public <T2> LinkedList<T2> bind(Function<T, Monad<T2, LinkedList<?>>> map) {
93+
public <T2> @NotNull LinkedList<T2> bind(@NotNull Function<T, Monad<T2, LinkedList<?>>> map) {
7294
return ((LinkedList<T2>) map.apply(value)).multiply(tail.bind(map));
7395
}
7496

7597
@Override
76-
public <U> LinkedList<U> map(Function<T, U> map) {
98+
public <U> @NotNull LinkedList<U> map(@NotNull Function<T, U> map) {
7799
return new Cons<>(map.apply(value), tail.map(map));
78100
}
79101

@@ -101,7 +123,7 @@ public <C extends Collection<T>> C toCollection(C collection) {
101123
}
102124

103125
@Override
104-
public LinkedList<T> multiply(Monoid<T, LinkedList<?>> t) {
126+
public @NotNull LinkedList<T> multiply(@NotNull Monoid<T, LinkedList<?>> t) {
105127
return new Cons<>(value, tail.multiply(t));
106128
}
107129
}
@@ -111,13 +133,13 @@ record Nil<T>() implements LinkedList<T> {
111133

112134
@Override
113135
@SuppressWarnings("unchecked")
114-
public <T2> LinkedList<T2> bind(Function<T, Monad<T2, LinkedList<?>>> map) {
136+
public <T2> @NotNull LinkedList<T2> bind(@NotNull Function<T, Monad<T2, LinkedList<?>>> map) {
115137
return (LinkedList<T2>) this;
116138
}
117139

118140
@Override
119141
@SuppressWarnings("unchecked")
120-
public <U> LinkedList<U> map(Function<T, U> map) {
142+
public <U> @NotNull LinkedList<U> map(@NotNull Function<T, U> map) {
121143
return (LinkedList<U>) this;
122144
}
123145

@@ -147,7 +169,7 @@ public <C extends Collection<T>> C toCollection(C collection) {
147169
}
148170

149171
@Override
150-
public LinkedList<T> multiply(Monoid<T, LinkedList<?>> t) {
172+
public @NotNull LinkedList<T> multiply(@NotNull Monoid<T, LinkedList<?>> t) {
151173
return (LinkedList<T>) t;
152174
}
153175
}

common/api/src/main/java/com/dfsek/terra/api/util/generic/data/Monoid.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
import com.dfsek.terra.api.util.generic.kinds.K;
44

5+
import org.jetbrains.annotations.Contract;
6+
import org.jetbrains.annotations.NotNull;
57

6-
public interface Monoid<T, M extends Monoid<?, M>> extends K<M, T>{
7-
<T1> Monoid<T1, M> identity();
88

9-
Monoid<T, M> multiply(Monoid<T, M> t);
9+
public interface Monoid<T, M extends Monoid<?, M>> extends K<M, T> {
10+
@Contract(pure = true, value = "-> new")
11+
<T1> @NotNull Monoid<T1, M> identity();
12+
13+
@Contract(pure = true, value = "_ -> new")
14+
@NotNull Monoid<T, M> multiply(@NotNull Monoid<T, M> t);
1015
}

0 commit comments

Comments
 (0)