Package com.codepoetics.octarine.deserialisation

Source Code of com.codepoetics.octarine.deserialisation.ParserMapperFactory

package com.codepoetics.octarine.deserialisation;

import com.codepoetics.octarine.records.Key;
import com.codepoetics.octarine.records.Value;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public interface ParserMapperFactory<T> {

    void configure(ParserMappingsConfigurator<T> configurator);

    default public ParserMapper<T> createMapper() {
        Map<String, Key<?>> keyMap = new HashMap<>();
        Map<String, Function<T, ?>> deserialiserMap = new HashMap<>();
        configure(new ParserMappingsConfigurator<T>() {
            @SuppressWarnings("unchecked")
            @Override
            public <V> ParserMappingsConfigurator<T> add(Key<? extends V> key, String fieldName, Function<T, ? extends V> deserialiser) {
                keyMap.put(fieldName, key);
                deserialiserMap.put(fieldName, (Function) deserialiser);
                return this;
            }
        });

        return new ParserMapper<T>() {
            @Override
            public boolean hasKeyFor(String fieldName) {
                return keyMap.containsKey(fieldName);
            }

            @SuppressWarnings("unchecked")
            @Override
            public Value getValue(String fieldName, T parser) {
                Key key = keyMap.get(fieldName);
                Function<T, ?> deserialiser = deserialiserMap.get(fieldName);
                return key.of(deserialiser.apply(parser));
            }
        };
    }
}
TOP

Related Classes of com.codepoetics.octarine.deserialisation.ParserMapperFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.