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

org.devocative.demeter.iservice.template.BaseStringTemplate Maven / Gradle / Ivy

The newest version!
package org.devocative.demeter.iservice.template;

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

public abstract class BaseStringTemplate implements IStringTemplate {
	private Map map = new HashMap<>();

	protected boolean convertValuesToString = false;

	// ------------------------------

	@Override
	public IStringTemplate setConvertValuesToString(boolean convertValuesToString) {
		this.convertValuesToString = convertValuesToString;
		return this;
	}

	@Override
	public  IStringTemplate registerToStringConverter(Class cls, IToStringConverter converter) {
		map.put(cls, converter);
		return this;
	}

	// ------------------------------

	protected Map convertValuesToString(Map data) {
		Map result = new HashMap<>();
		for (Map.Entry entry : data.entrySet()) {
			if (entry.getValue() instanceof Map) {
				result.put(entry.getKey(), convertValuesToString((Map) entry.getValue()));
			} else {
				result.put(entry.getKey(), findAndConvert(entry.getValue()));
			}
		}
		return result;
	}

	// ------------------------------

	private String findAndConvert(Object obj) {
		IToStringConverter converter = IToStringConverter.DEFAULT;
		for (Map.Entry entry : map.entrySet()) {
			if (entry.getKey().isInstance(obj)) {
				converter = entry.getValue();
				break;
			}
		}

		return converter.convertToString(obj);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy