com.bandwidth.sdk.model.bxml.utils.EnumFinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bandwidth-sdk Show documentation
Show all versions of bandwidth-sdk Show documentation
The official client SDK for Bandwidth's Voice, Messaging, MFA, and WebRTC APIs
The newest version!
package com.bandwidth.sdk.model.bxml.utils;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
/**
* Allows for convenient finding of enums based on custom enum fields.
*
* @param the type of the enum
*/
public class EnumFinder> {
private final List enums;
public EnumFinder(Class enumClass) {
this.enums = Arrays.asList(enumClass.getEnumConstants());
}
/**
* Finds an {@code Optional} enum based on a {@code Predicate}.
* Example: {@code find(thing -> thing.code == code)}
* Note: Finds the first enum that matches the predicate using the order the enums are declared in.
*
* @param predicate the predicate to match the enum against
* @return the {@code Optional} enum matching the predicate
*/
public Optional findOptional(Predicate super E> predicate) {
return enums.stream().filter(predicate).findFirst();
}
/**
* Finds an enum based on a {@code Predicate} that must match otherwise an {@code IllegalArgumentException} will be
* thrown.
*
* @param predicate the predicate to match the enum against
* @return the enum matching the predicate
* @throws IllegalArgumentException thrown if predicate doesn't match
* @see #find(Predicate)
* @see #findOptional(Predicate)
*/
public E findRequired(Predicate super E> predicate) {
return findOptional(predicate).orElseThrow(IllegalArgumentException::new);
}
/**
* Finds an enum based on a {@code Predicate}.
*
* @param predicate the predicate to match the enum against
* @return the enum matching the predicate, {@code null} if no match
* @see #findRequired(Predicate)
* @see #findOptional(Predicate)
*/
public E find(Predicate super E> predicate) {
return findOptional(predicate).orElse(null);
}
/**
* Convenience method to create an enum finder with less generics mess.
*
* {@code AnEnum anEnum = EnumFinder.of(AnEnum.class).find(e -> e.code == code).orElse(null)}
*
* @param enumClass the type of the enum
* @return the enum finder
*/
public static > EnumFinder of(Class enumClass) {
return new EnumFinder<>(enumClass);
}
}