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

com.xlrit.gears.engine.form.CompanionAdapter Maven / Gradle / Ivy

package com.xlrit.gears.engine.form;

import static com.google.common.base.Preconditions.*;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xlrit.gears.base.form.Companion;

class CompanionAdapter {
	private static final Logger LOG = LoggerFactory.getLogger(CompanionAdapter.class);

	private final Companion companion;
	private final Method[] methods;

	public CompanionAdapter(Companion companion) {
		this.companion = checkNotNull(companion);
		this.methods = companion.getClass().getDeclaredMethods();
	}

	public ValueRef getBasedOnRef() {
		return getValueRef("getBasedOn");
	}

	public ValueRef getBasedOnRef(FormPath path) {
		return getValueRef("get" + path.toMethodNameBase() + "BasedOn");
	}

	public ValueRef getDefaultRef(FormPath path) {
		return getValueRef("get" + path.toMethodNameBase() + "Default");
	}

	public ValueRef getChoicesRef(FormPath path) {
		return getValueRef("get" + path.toMethodNameBase() + "Choices");
	}

	public ValueRef getCollectionRef(FormPath path) {
		return getValueRef("get" + path.toMethodNameBase() + "Collection");
	}

	private ValueRef getValueRef(String methodName) {
		//LOG.info("Searching for method '{}' in {} found {}", methodName, companion, r);
		return Arrays.stream(methods)
			.filter(m -> m.getName().equals(methodName))
			.filter(m -> m.getParameterCount() == 1)        // TODO parameter type should be Execution
			.map(m -> new ValueRef(companion, m))
			.findFirst()
			.orElse(null);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy