Skip to content

Commit 23ea424

Browse files
refactor: Add more methods on Option, fix ambiguous method
1 parent 9c6d1f1 commit 23ea424

2 files changed

Lines changed: 64 additions & 6 deletions

File tree

src/main/java/net/marcellperger/mathexpr/util/rs/Option.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ default <U> U mapOrElse(Supplier<? extends U> defaultFn, Function<? super T, ? e
7676
case None() -> defaultFn.get();
7777
};
7878
}
79-
default Option<T> mapErr(Runnable noneFn) { // there is nothing to map from or to (None -> None)!
79+
// there is nothing to map from or to (None -> None)! so this is Runnable
80+
// so this is the same as inspectErr. It's here for consistency with Result
81+
default Option<T> mapErr(Runnable noneFn) {
8082
if(isNone()) noneFn.run();
8183
return this;
8284
}
8385

8486
// not Rust functions, but provides more logical argument order
8587
// (if/else vs else/if) than mapOrElse
86-
default void ifThenElse(Consumer<? super T> someFn, Runnable noneFn) {
88+
default void ifThenElse_void(Consumer<? super T> someFn, Runnable noneFn) {
8789
mapOrElse(Util.runnableToSupplier(noneFn), Util.consumerToFunction(someFn));
8890
}
8991
default <U> U ifThenElse(Function<? super T, ? extends U> someFn, Supplier<? extends U> noneFn) {
@@ -116,4 +118,23 @@ default Iterator<T> iterator() {
116118
default void forEach(Consumer<? super T> action) {
117119
map(Util.consumerToFunction(action));
118120
}
121+
122+
default T unwrapOr(T default_) {
123+
return unwrapOrElse(() -> default_);
124+
}
125+
default T unwrapOrElse(Supplier<? extends T> ifNone) {
126+
return mapOrElse(ifNone, Function.identity());
127+
}
128+
// no unwrap_or_default (see Result.java for big explanation)
129+
default T expect(String msg) {
130+
return unwrapOrElse(() -> {
131+
throw new OptionPanicException(msg);
132+
});
133+
}
134+
default T unwrap() {
135+
return expect("Option.unwrap() got None value");
136+
}
137+
// no insert / get_or_insert(_*) / take(_if) / replace
138+
// (as record is immutable in Java)
139+
// TODO unwrap, check Rust docs
119140
}

src/test/java/net/marcellperger/mathexpr/util/rs/OptionTest.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void mapOrElse() {
9090
}
9191

9292
@Test
93-
void ifThenElse__func_supp() {
93+
void ifThenElse() {
9494
MockedSupplier<Integer> mSupplier = new MockedSupplier<>(-6);
9595
MockedFunction<Integer, Integer> mfAdd1 = new MockedFunction<>(i -> i + 1);
9696
{
@@ -108,18 +108,18 @@ void ifThenElse__func_supp() {
108108
}
109109

110110
@Test
111-
void ifThenElse__cons_runnable() {
111+
void ifThenElse_void() {
112112
MockedConsumer<Integer> mCons = new MockedConsumer<>();
113113
MockedRunnable mRunnable = new MockedRunnable();
114114
{
115-
getSome().ifThenElse(mCons, mRunnable);
115+
getSome().ifThenElse_void(mCons, mRunnable);
116116
mCons.assertCalledOnceWith(314);
117117
mRunnable.assertNotCalled();
118118
}
119119
mCons.reset();
120120
mRunnable.reset();
121121
{
122-
getNone().ifThenElse(mCons, mRunnable);
122+
getNone().ifThenElse_void(mCons, mRunnable);
123123
mCons.assertNotCalled();
124124
mRunnable.assertCalledOnce();
125125
}
@@ -188,4 +188,41 @@ void forEach() {
188188
intCons.assertNotCalled();
189189
}
190190
}
191+
192+
@Test
193+
void unwrapOr() {
194+
assertEquals(314, getSome().unwrapOr(-1));
195+
assertEquals(-1, getNone().unwrapOr(-1));
196+
}
197+
198+
@Test
199+
void unwrapOrElse() {
200+
MockedSupplier<Integer> ms = new MockedSupplier<>(271);
201+
{
202+
assertEquals(314, getSome().unwrapOrElse(ms));
203+
ms.assertNotCalled();
204+
}
205+
ms.reset();
206+
{
207+
assertEquals(271, getNone().unwrapOrElse(ms));
208+
ms.assertCalledOnce();
209+
}
210+
}
211+
212+
@Test
213+
void expect() {
214+
assertEquals(314, assertDoesNotThrow(() -> getSome().expect("EXPECT_MSG")));
215+
Option<Integer> n = getNone();
216+
OptionPanicException exc = assertThrows(OptionPanicException.class, () -> n.expect("EXPECT_MSG"));
217+
assertEquals("EXPECT_MSG", exc.getMessage());
218+
assertNull(exc.getCause(), "Expected no cause for Option.expect()");
219+
}
220+
221+
@Test
222+
void unwrap() {
223+
assertEquals(314, assertDoesNotThrow(() -> getSome().unwrap()));
224+
OptionPanicException exc = assertThrows(OptionPanicException.class, getNone()::unwrap);
225+
assertEquals("Option.unwrap() got None value", exc.getMessage());
226+
assertNull(exc.getCause(), "Expected no cause for Option.unwrap()");
227+
}
191228
}

0 commit comments

Comments
 (0)