All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.omnifaces.persistence.criteria.Enumerated Maven / Gradle / Ivy

There is a newer version: 0.22.J1
Show newest version
package org.omnifaces.persistence.criteria;

import java.util.Objects;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;

/**
 * Creates path = enum.
 *
 * @author Bauke Scholtz
 */
public final class Enumerated extends Criteria> {

	private Enumerated(Enum value) {
		super(value);
	}

	public static Enumerated value(Enum value) {
		return new Enumerated(value);
	}

	public static Enumerated parse(Object searchValue, Class> targetType) {
		return new Enumerated(parseEnum(searchValue, targetType));
	}

	@Override
	public Predicate build(Expression path, CriteriaBuilder criteriaBuilder, ParameterBuilder parameterBuilder) {
		return criteriaBuilder.equal(path, parameterBuilder.create(getValue()));
	}

	@Override
	public boolean applies(Object modelValue) {
		return modelValue != null && Objects.equals(parseEnum(modelValue, getValue().getClass()), getValue());
	}

	@SuppressWarnings("unchecked")
	private static Enum parseEnum(Object searchValue, Class targetType) throws IllegalArgumentException {
		if (searchValue instanceof Enum) {
			return (Enum) searchValue;
		}
		else if (targetType.isEnum()) {
			for (Enum enumConstant : ((Class>) targetType).getEnumConstants()) {
				if (enumConstant.name().equalsIgnoreCase(searchValue.toString())) {
					return enumConstant;
				}
			}
		}

		throw new IllegalArgumentException(searchValue.toString());
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy