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

org.marid.runtime.converter.AbstractValueConverters Maven / Gradle / Ivy

There is a newer version: 0.9.8.10
Show newest version
/*-
 * #%L
 * marid-runtime
 * %%
 * Copyright (C) 2012 - 2017 MARID software development group
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 * #L%
 */

package org.marid.runtime.converter;

import org.marid.annotation.MetaInfo;
import org.marid.annotation.MetaLiteral;
import org.marid.misc.Casts;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.function.BiFunction;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * @author Dmitry Ovchinnikov
 */
public abstract class AbstractValueConverters implements ValueConverters {

    public static final Pattern COMMA = Pattern.compile(",");

    protected final HashMap metaMap = new HashMap<>();
    protected final HashMap map = new HashMap<>();
    protected final HashMap typeMap = new HashMap<>();

    public AbstractValueConverters() {
        Stream.of(getClass().getMethods())
                .filter(m -> m.isAnnotationPresent(MetaInfo.class))
                .filter(m -> BiFunction.class == m.getReturnType())
                .filter(m -> m.getGenericReturnType() instanceof ParameterizedType)
                .filter(m -> ((ParameterizedType) m.getGenericReturnType()).getActualTypeArguments().length == 3)
                .filter(m -> ((ParameterizedType) m.getGenericReturnType()).getActualTypeArguments()[0] == String.class)
                .forEach(method -> {
                    final MetaInfo info = method.getAnnotation(MetaInfo.class);
                    final ParameterizedType pt = (ParameterizedType) method.getGenericReturnType();
                    final Type type = pt.getActualTypeArguments()[2];
                    metaMap.put(info.name(), new MetaLiteral(info));
                    typeMap.put(info.name(), type);
                    try {
                        final BiFunction, ?> function = Casts.cast(method.invoke(this));
                        map.put(info.name(), function::apply);
                    } catch (ReflectiveOperationException x) {
                        throw new IllegalStateException(x);
                    }
                });
    }

    protected  void register(MetaLiteral info, Class type, BiFunction, T> converter) {
        metaMap.put(info.name, info);
        typeMap.put(info.name, type);
        map.put(info.name, (v, c) -> converter.apply(v, type));
    }

    protected void redirect(MetaLiteral info, Class type, BiFunction, ?> converter) {
        metaMap.put(info.name, info);
        typeMap.put(info.name, type);
        map.put(info.name, (v, c) -> converter.apply(v, type));
    }

    @Override
    public ValueConverter getConverter(String name) {
        return map.get(name);
    }

    @Override
    public HashMap getMetaMap() {
        return metaMap;
    }

    @Override
    public HashMap getTypeMap() {
        return typeMap;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy