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

net.n2oapp.framework.config.io.ElementIOFactoryByMap Maven / Gradle / Ivy

There is a newer version: 7.28.2
Show newest version
package net.n2oapp.framework.config.io;

import net.n2oapp.engine.factory.EngineNotFoundException;
import net.n2oapp.framework.api.metadata.io.ElementIOFactory;
import net.n2oapp.framework.api.metadata.io.ProxyTypedElementIO;
import net.n2oapp.framework.api.metadata.io.TypedElementIO;
import net.n2oapp.framework.api.metadata.persister.TypedElementPersister;
import net.n2oapp.framework.api.metadata.reader.TypedElementReader;
import org.jdom2.Element;

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

/**
 * Фабрика типизированных функций чтения / записи элементов
 */
public class ElementIOFactoryByMap, P extends TypedElementPersister>
        implements ElementIOFactory {
    private Class baseElementClass;
    private Map names = new HashMap<>();
    private Map classes = new HashMap<>();

    public ElementIOFactoryByMap(Class baseElementClass) {
        this.baseElementClass = baseElementClass;
    }

    @Override
    public ElementIOFactory add(TypedElementIO tio) {
        ProxyTypedElementIO proxy = new ProxyTypedElementIO<>(tio);
        names.put(tio.getElementName(), (R) proxy);
        classes.put(tio.getElementClass(), (P) proxy);
        return this;
    }

    @Override
    public Class getBaseElementClass() {
        return baseElementClass;
    }

    @Override
    public P produce(Object model) {
        P engine = classes.get(model.getClass());

        if (engine == null) {
            throw new EngineNotFoundException(model.getClass());
        }
        return engine;
    }

    @Override
    public R produce(Element element) {
        R engine = names.get(element.getName());
        if (engine == null)
            throw new EngineNotFoundException(element.getName());
        return engine;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy