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

org.unlaxer.parser.ParserFactoryByClass Maven / Gradle / Ivy

package org.unlaxer.parser;

import java.util.HashMap;
import java.util.Map;

import org.unlaxer.util.FactoryBoundCache;

public class ParserFactoryByClass{
	
	static FactoryBoundCache, Parser>//
		singletonsByClass = new FactoryBoundCache<>((clazz) -> {
			try {
				Parser parser = clazz.getDeclaredConstructor().newInstance();
				return parser;
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		});
	
	@SuppressWarnings("unchecked")
	public static  T get(Class clazz) {
		T parser = (T) singletonsByClass.get(clazz);
		if(false == initialized(clazz)) {
			initilizedByClass.put(clazz, true);
			parser.initialize();
		}
		return parser;
	}
	
	static Map , Boolean> 
		initilizedByClass = new HashMap<>();
	
	static boolean initialized(Class clazz) {
		return initilizedByClass.getOrDefault(clazz, false);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy