summaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/pcj/Result.java
blob: af5398e17f9f5b8989f65d01a6afeb392b7f5f09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package moe.nea.pcj;

import org.jspecify.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

public sealed interface Result<Good, Bad> permits Result.Ok, Result.Fail {
	default boolean isOk() {
		return errors().isEmpty();
	}

	Optional<Good> value();

	Optional<Good> partial();

	default Optional<Good> valueOrPartial() {
		return value().or(this::partial);
	}

	List<Bad> errors();

	default <Next> Result<Next, Bad> map(Function<Good, Next> mapper) {
		return flatMap(mapper.andThen(Result::ok));
	}

	<Next> Result<Next, Bad> flatMap(Function<Good, Result<? extends Next, ? extends Bad>> mapper);

	default <NextBad> Result<Good, NextBad> mapError(Function<Bad, NextBad> mapper) {
		return mapErrors(it -> it.stream().map(mapper).toList());
	}

	<NextBad> Result<Good, NextBad> mapErrors(Function<List<Bad>, List<NextBad>> mapper);

	Result<Good, Bad> appendErrors(List<Bad> error);

	record Ok<Good, Bad>(Good okValue) implements Result<Good, Bad> {
		@Override
		public Result<Good, Bad> appendErrors(List<Bad> errors) {
			if (errors.isEmpty()) return new Ok<>(okValue);
			return new Fail<>(okValue, errors);
		}

		@Override
		public <NextBad> Result<Good, NextBad> mapErrors(Function<List<Bad>, List<NextBad>> mapper) {
			return new Ok<>(okValue);
		}

		@Override
		public Optional<Good> partial() {
			return Optional.empty();
		}

		@Override
		public List<Bad> errors() {
			return List.of();
		}

		@Override
		public <Next> Result<Next, Bad> flatMap(Function<Good, Result<? extends Next, ? extends Bad>> mapper) {
			return Result.cast(mapper.apply(okValue));
		}

		@Override
		public Optional<Good> value() {
			return Optional.of(okValue);
		}

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

		@Override
		public boolean equals(Object obj) {
			if (obj == this) return true;
			if (obj instanceof Result.Ok<?, ?> ok) {
				return Objects.equals(ok.okValue, this.okValue);
			}
			return false;
		}
	}

	record Fail<Good, Bad>(@Nullable Good partialValue, List<Bad> badValue) implements Result<Good, Bad> {
		public Fail {
			if (badValue.isEmpty())
				throw new IllegalArgumentException("Cannot create failure without any error values");
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj) return true;
			if (obj instanceof Result.Fail<?, ?> fail) {
				return Objects.equals(partialValue, fail.partialValue) && badValue.equals(fail.badValue);
			}
			return false;
		}

		@Override
		public int hashCode() {
			return Objects.hash(partialValue, badValue);
		}

		@Override
		public Optional<Good> value() {
			return Optional.empty();
		}

		@Override
		public Optional<Good> partial() {
			return Optional.ofNullable(partialValue);
		}

		@Override
		public List<Bad> errors() {
			return Collections.unmodifiableList(badValue);
		}

		@Override
		public <Next> Result<Next, Bad> flatMap(Function<Good, Result<? extends Next, ? extends Bad>> mapper) {
			if (partialValue != null) {
				return Result.<Next, Bad>cast(mapper.apply(partialValue)).appendErrors(badValue);
			}
			return new Fail<>(null, badValue);
		}

		@Override
		public <NextBad> Result<Good, NextBad> mapErrors(Function<List<Bad>, List<NextBad>> mapper) {
			return new Fail<>(partialValue, mapper.apply(badValue));
		}

		@Override
		public Result<Good, Bad> appendErrors(List<Bad> errors) {
			var nextErrors = new ArrayList<>(badValue);
			nextErrors.addAll(errors);
			return new Fail<>(partialValue, nextErrors);
		}
	}

	static <Good, Bad> Result<Good, Bad> ok(Good value) {
		return new Ok<>(value);
	}

	static <Good, Bad> Result.Fail<Good, Bad> fail(Bad error) {
		return new Fail<>(null, List.of(error));
	}


	static <Good, Bad> Result<Good, Bad> cast(Result<? extends Good, ? extends Bad> c) {
		//noinspection unchecked
		return (Result<Good, Bad>) c;
	}

	static <Good, Bad> Result.Fail<Good, Bad> partial(@Nullable Good partial, Bad error) {
		return new Fail<>(partial, List.of(error));
	}
}