Created
November 23, 2023 17:53
-
-
Save fercomunello/a8b92bc3a6ab7c85305e8f4cdc86c4ff to your computer and use it in GitHub Desktop.
OOP way of computing enum sets
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
import java.util.Optional; | |
public final class Decode<T> implements Expression<T> { | |
private final T value; | |
private final boolean expression; | |
public Decode(final boolean expression, final T value) { | |
this.expression = expression; | |
this.value = value; | |
} | |
@Override | |
public Optional<T> compute() { | |
return this.expression ? Optional.of(this.value) : Optional.empty(); | |
} | |
} |
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
import java.util.EnumSet; | |
import java.util.Set; | |
public final class DecodedEnum<E extends Enum<E>> implements Expressions<E> { | |
private final Class<E> enumClass; | |
public DecodedEnum(final Class<E> enumClass) { | |
this.enumClass = enumClass; | |
} | |
@SafeVarargs | |
public final Set<E> compute(final Expression<E>... expressions) { | |
final var enumSet = EnumSet.noneOf(this.enumClass); | |
for (final var exp : expressions) { | |
exp.compute().ifPresent(enumSet::add); | |
} | |
return enumSet; | |
} | |
} |
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
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.DisplayName; | |
import org.junit.jupiter.api.Test; | |
import java.util.Optional; | |
final class DecodedEnumTest { | |
private enum Shinobi { | |
JIRAYA, TSUNADE, OROCHIMARU, SARUTOBI | |
} | |
@Test | |
@DisplayName("Create an enum set with only successfully decoded entries") | |
void testEnumSetCreation() { | |
final var sennins = new DecodedEnum<>(Shinobi.class) | |
.compute( | |
new Decode<>(true, Shinobi.TSUNADE), | |
new Decode<>(true, Shinobi.JIRAYA), | |
new Decode<>(true, Shinobi.OROCHIMARU), | |
new Decode<>(false, Shinobi.SARUTOBI) | |
); | |
Assertions.assertEquals(3, sennins.size()); | |
} | |
@Test | |
@DisplayName("Create the enum set but skipping empty entries") | |
void testEnumSetCreationSkippingEmptyEntries() { | |
Assertions.assertEquals(1, | |
new DecodedEnum<>(Shinobi.class).compute( | |
new Decode<>(true, Shinobi.JIRAYA), | |
Optional::empty, Optional::empty | |
).size() | |
); | |
} | |
} |
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
import java.util.Optional; | |
public interface Expression<T> { | |
Optional<T> compute(); | |
} |
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
import java.util.Set; | |
public interface Expressions<T> { | |
Set<T> compute(Expression<T>... expressions); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment