Skip to content

Instantly share code, notes, and snippets.

@rbaul
Created January 3, 2019 09:22
Show Gist options
  • Save rbaul/0eb634c44efcf3ad3a9e3f1b0e4c5d69 to your computer and use it in GitHub Desktop.
Save rbaul/0eb634c44efcf3ad3a9e3f1b0e4c5d69 to your computer and use it in GitHub Desktop.
Enum multi fields per different clients
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Predicate;
public final class EnumUtils {
public static <E extends Enum<E>> Optional<E> getByPredicate(final Class<E> enumClass, Predicate<E> predicate){
return Arrays.stream(enumClass.getEnumConstants())
.filter(predicate)
.findFirst();
}
}
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public enum NumberType {
ONE(1, "one", "I"),
TWO(2, "two", "II"),
THREE(3, "three", "III");
@Getter(onMethod_ = @JsonValue) // default
private final int client1Value;
private final String client2Value;
private final String client3Value;
public static NumberType byClient1Value(int value) {
return EnumUtils.getByPredicate(NumberType.class, numberType -> numberType.getClient1Value() == value).orElse(null);
}
public static NumberType byClient2Value(String value) {
return EnumUtils.getByPredicate(NumberType.class, numberType -> numberType.getClient2Value().equals(value)).orElse(null);
}
public static NumberType byClient3Value(String value) {
return EnumUtils.getByPredicate(NumberType.class, numberType -> numberType.getClient3Value().equals(value)).orElse(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment