Last active
September 10, 2023 08:49
-
-
Save cboudereau/4e4668cb7ceac2c65ec5e9571f20e7dd to your computer and use it in GitHub Desktop.
ord trait java adt (rust backport)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.adt; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import static org.junit.jupiter.api.Assertions.assertTrue; | |
import org.junit.jupiter.api.Test; | |
public class AdtTest { | |
sealed interface Value<T extends Comparable<T>> extends Comparable<Value<T>> permits Value.Fixed, Value.Infinite { | |
static record Tuple<L, R>(L fst, R snd) { | |
} | |
public static final record Fixed<T extends Comparable<T>>(T value) implements Value<T> { | |
} | |
public static final record Infinite<T extends Comparable<T>>() implements Value<T> { | |
} | |
public static <T extends Comparable<T>> Fixed<T> fixed(final T value) { | |
return new Fixed<>(value); | |
} | |
public static <T extends Comparable<T>> Infinite<T> infinite() { | |
return new Infinite<>(); | |
} | |
@Override | |
default int compareTo(final Value<T> o) { | |
return switch (new Tuple<>(this, o)) { | |
case Tuple<Value<T>, Value<T>>(Value.Infinite<T> fst, Value.Infinite<T> snd) -> 0; | |
case Tuple<Value<T>, Value<T>>(Value.Infinite<T> fst, Value.Fixed<T> snd) -> 1; | |
case Tuple<Value<T>, Value<T>>(Value.Fixed<T> fst, Value.Infinite<T> snd) -> -1; | |
case Tuple<Value<T>, Value<T>>(Value.Fixed<T> fst, Value.Fixed<T> snd) -> | |
fst.value.compareTo(snd.value); | |
}; | |
} | |
default boolean isGreaterThan(final Value<T> o) { | |
return this.compareTo(o) > 0; | |
} | |
default boolean isLessThan(final Value<T> o) { | |
return this.compareTo(o) < 0; | |
} | |
} | |
@Test | |
public void compareToTest() { | |
assertEquals(0, Value.infinite().compareTo(Value.infinite())); | |
assertEquals(0, Value.fixed(1).compareTo(Value.fixed(1))); | |
assertTrue(Value.fixed(1).isLessThan(Value.infinite())); | |
assertTrue(Value.fixed(2).isGreaterThan(Value.fixed(1))); | |
assertTrue(Value.fixed(1).isLessThan(Value.fixed(2))); | |
} | |
} |
This version works perfectly with JDK 21 but the vscode extension "Language Support for Java(TM) by Red Hat" is complaining (it might be compatible with the JDK 20 even if it is a pre-release version v1.22.2023090904)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is also jacoco exhaustiveness issue already tracked : jacoco/jacoco#1211