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

com.giffing.bucket4j.spring.boot.starter.context.ExecutePredicate Maven / Gradle / Ivy

The newest version!
package com.giffing.bucket4j.spring.boot.starter.context;

import java.util.Map;
import java.util.function.Predicate;

/**
 * The ExecutePredicate is used to conditionally execute a rate limit.
 * 
 * @param  the type of the predicate
 */
public abstract class ExecutePredicate implements Predicate {

	/**
	 * The unique name of the ExecutionPredicate which can be used
	 * in the property configuration.
	 */
	public abstract String name();
	
	/**
	 * Initialize the ExecutionPredicate with the provided
	 * property configuration.
	 */
	public ExecutePredicate init(Map args) {
		if(hasSimpleConfig(args)) {
			parseSimpleConfig(args.get(ExecutePredicateDefinition.SIMPLE_CONFIG_KEY));
		} else {
			parseConfig(args);
		}
		return this;
	}
	
	protected abstract ExecutePredicate parseSimpleConfig(String simpleConfig);
	
	protected ExecutePredicate parseConfig(Map args) {
		throw new UnsupportedOperationException("The ServletRequestExecutionPredicate %s doesn't support arguments"
				.formatted(this.getClass().getSimpleName()));
	}

	private boolean hasSimpleConfig(Map args) {
		return args.containsKey(ExecutePredicateDefinition.SIMPLE_CONFIG_KEY);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy