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

org.appdapter.gui.util.FunctionalClassRegistry Maven / Gradle / Ivy

Go to download

Appdapter Maven project including Java and Scala, produces jar, not bundle. Excludes concrete SLF4J binding.

The newest version!
package org.appdapter.gui.util;

import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.io.IOException;
import java.util.HashMap;

import org.appdapter.core.log.Debuggable;
import org.appdapter.gui.browse.Utility;
import static org.appdapter.gui.util.CollectionSetUtils.*;
import static org.appdapter.gui.util.PromiscuousClassUtilsA.*;

class FunctionalClassRegistry extends FunctionalRegistryMap> {

	/**
	 * Finds a Class that implements a delegateType that processes the  objClass
	 * 
	 * @param resultType
	 * @param srchCiteria
	 * @return
	 * @throws Throwable
	 */
	public static  Class findImplmentingForMatch(Class resultType, Class srchCiteria) {
		FunctionalClassRegistry fcbk = findFinder(resultType);
		return (Class) fcbk.findCustomizerClass(srchCiteria);
	}

	public static  void addDelegateClass(Class delegateType, Class customizerClass, Class... objClass) {
		FunctionalClassRegistry fcbk = findFinder(delegateType);
		fcbk.registerCustomizerDef(customizerClass, objClass);
	}

	static HashMap classFinders = new HashMap();

	/**
	 *  Get a finder that when searched later returns a specified resultType
	 *  
	 * @param resultType
	 * @return
	 */
	static  FunctionalClassRegistry findFinder(Class resultType) {
		FunctionalClassRegistry fcbk = classFinders.get(resultType);
		if (fcbk == null) {
			fcbk = new FunctionalClassRegistry(resultType);
			classFinders.put(resultType, fcbk);
		}
		return fcbk;
	}

	FunctionalClassRegistry(Class baseClass) {
		this.baseClass = baseClass;
		classFinders.put(baseClass, this);
	}

	final Class baseClass;

	@Override public Class tryLast(Class c) {
		if (true)
			return null;
		String fnd = getLowerSimplestNameMinusBeanInfo(c);
		try {
			for (Class possible : ClassFinder.getClasses(baseClass)) {
				if (!isCreateable(possible))
					continue;
				String cn = getLowerSimplestNameMinusBeanInfo(possible);
				if (cn.toLowerCase().contains(fnd.toLowerCase()))
					return (Class) possible;
			}
		} catch (IOException e) {
			Debuggable.UnhandledException(e);
			return null;
		}
		return null;
	}

	private String getLowerSimplestNameMinusBeanInfo(Class c) {
		String sn = c.getSimpleName();
		if (sn == null)
			sn = c.getName();
		return sn.replace("Bean", "").replace("Info", "").replace("CustomizerClass", "").replace("Customizer", "").replace("Editor", "");
	}

	@SuppressWarnings("unchecked") public Class tryFirst(Class c) {
		BeanInfo objectInfo;
		try {
			objectInfo = Utility.getBeanInfoNoF(c, false);
		} catch (IntrospectionException e) {
			return null;
		}
		BeanDescriptor descriptor = objectInfo.getBeanDescriptor();
		Class customizerClass = null;
		if (descriptor != null) {
			customizerClass = (Class) descriptor.getCustomizerClass();
		}
		return customizerClass;
	}

	public @Override int meetSearchCriteria(Class t, Class c) {
		return t.isAssignableFrom(c) ? 1 : 0;
	}

	@Override public Class nextInListToTry(Class c) {
		return c.getSuperclass();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy