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

storm.parser.StormParserFactory Maven / Gradle / Ivy

package storm.parser;

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

import storm.parser.converter.StormConverter;
import storm.parser.converter.StormConverterAptClassNameBuilder;
import storm.parser.metadata.StormMetadata;
import storm.parser.metadata.StormMetadataAptClassNameBuilder;
import storm.parser.scheme.StormScheme;
import storm.parser.scheme.StormSchemeAptNameBuilder;

/**
 * Created by Dimitry Ivanov on 02.01.2016.
 */
public class StormParserFactory {

    private static final StormParserItemFactory FACTORY = new StormParserItemFactory();

    private final Map, StormParser> mCachedParsers;

    public StormParserFactory() {
        this.mCachedParsers = new HashMap<>();
    }

    public synchronized  StormParser provide(Class cl) {
        StormParser parser = mCachedParsers.get(cl);
        if (parser == null) {
            parser = parser(cl);
            mCachedParsers.put(cl, parser);
        }
        //noinspection unchecked
        return (StormParser) parser;
    }

    private synchronized  StormParser parser(final Class cl) {

        final ParserLazy scheme = new ParserLazy<>(new ParserLazy.ParserLazyProvider() {
            @Override
            public StormScheme provide() throws StormParserException {
                final StormScheme stormScheme = FACTORY.provide(StormSchemeAptNameBuilder.getInstance(), cl);
                if (stormScheme == null) {
                    throw StormParserException.newInstance("Unable to obtain StormScheme generated class for a class: " + cl.getName());
                }
                return stormScheme;
            }
        });

        final ParserLazy> converter = new ParserLazy<>(new ParserLazy.ParserLazyProvider>() {
            @Override
            public StormConverter provide() throws StormParserException {
                final StormConverter stormConverter = FACTORY.provide(StormConverterAptClassNameBuilder.getInstance(), cl);
                if (stormConverter == null) {
                    throw StormParserException.newInstance("Unable to obtain StormConverter generated class for a class: " + cl.getName());
                }
                return stormConverter;
            }
        });

        final ParserLazy> metadata = new ParserLazy<>(new ParserLazy.ParserLazyProvider>() {
            @Override
            public StormMetadata provide() throws StormParserException {
                final StormMetadata stormMetadata = FACTORY.provide(StormMetadataAptClassNameBuilder.getInstance(), cl);
                if (stormMetadata == null) {
                    throw StormParserException.newInstance("Unable to obtain StormMetadata generated class for a class: " + cl.getName());
                }
                return stormMetadata;
            }
        });

        return new StormParserImpl<>(scheme, converter, metadata);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy