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

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

There is a newer version: 2.19.0
Show newest version
package com.redis.riot.gen;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

import com.github.javafaker.Faker;

public class MapGenerator implements Generator> {

	public static final String FIELD_INDEX = "index";

	private final EvaluationContext context;
	private final Map expressions;

	public MapGenerator(EvaluationContext context, Map expressions) {
		this.context = context;
		this.expressions = expressions;
	}

	@Override
	public Map next(int index) {
		Map map = new HashMap<>();
		context.setVariable(FIELD_INDEX, index);
		for (Entry expression : expressions.entrySet()) {
			map.put(expression.getKey(), expression.getValue().getValue(context));
		}
		return map;
	}

	public static MapGeneratorBuilder builder() {
		return new MapGeneratorBuilder();
	}

	public static class MapGeneratorBuilder {

		private final SpelExpressionParser parser = new SpelExpressionParser();
		private Locale locale = Locale.getDefault();
		private final Map expressions = new LinkedHashMap<>();

		public MapGeneratorBuilder locale(Locale locale) {
			this.locale = locale;
			return this;
		}

		public MapGeneratorBuilder field(String field, String expression) {
			this.expressions.put(field, parser.parseExpression(expression));
			return this;
		}

		public MapGeneratorBuilder fields(Map fields) {
			fields.forEach((k, v) -> expressions.put(k, parser.parseExpression(v)));
			return this;
		}

		public MapGenerator build() {
			Faker faker = new Faker(locale);
			SimpleEvaluationContext context = new SimpleEvaluationContext.Builder(new ReflectivePropertyAccessor())
					.withInstanceMethods().withRootObject(faker).build();
			return new MapGenerator(context, expressions);
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy