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

com.redislabs.riot.gen.MapGenerator Maven / Gradle / Ivy

The newest version!
package com.redislabs.riot.gen;

import com.github.javafaker.Faker;
import lombok.Builder;
import lombok.NonNull;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

public class MapGenerator implements Generator> {

    public final static String FIELD_INDEX = "index";

    private final SimpleEvaluationContext context;
    private final Map expressions;

    @Builder
    private MapGenerator(Locale locale, @NonNull Map fields) {
        Faker faker = new Faker(locale == null ? Locale.getDefault() : locale);
        this.context = new SimpleEvaluationContext.Builder(new ReflectivePropertyAccessor()).withInstanceMethods().withRootObject(faker).build();
        SpelExpressionParser parser = new SpelExpressionParser();
        this.expressions = fields.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> parser.parseExpression(e.getValue())));
    }

    @Override
    public Map next(long index) {
        Map map = new HashMap<>();
        context.setVariable(FIELD_INDEX, index);
        for (String field : expressions.keySet()) {
            map.put(field, expressions.get(field).getValue(context));
        }
        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy