org.biins.objectbuilder.builder.MapObjectBuilder Maven / Gradle / Ivy
package org.biins.objectbuilder.builder;
import org.apache.commons.lang.Validate;
import org.biins.objectbuilder.builder.strategy.MapGeneratorStrategy;
import org.biins.objectbuilder.types.Types;
import org.biins.objectbuilder.types.map.MapType;
import org.biins.objectbuilder.types.map.MapTypeRegistry;
import org.biins.objectbuilder.util.ClassUtils;
import java.util.HashMap;
import java.util.Map;
/**
* @author Martin Janys
*/
@SuppressWarnings("unchecked")
public class MapObjectBuilder extends AbstractBuilder implements Builder {
private final ObjectBuilder objectBuilder;
private MapGeneratorStrategy mapGeneratorStrategy = MapGeneratorStrategy.DEFAULT;
private int size = 0;
private Types keyType;
private Types valueType;
public MapObjectBuilder(ObjectBuilder objectBuilder) {
this.objectBuilder = objectBuilder;
}
public MapObjectBuilder setSize(int size) {
this.size = size;
validateSize();
return this;
}
private void validateSize() {
if (size < 0) {
throw new IllegalArgumentException("Size must be positive");
}
}
public MapObjectBuilder setGeneratorStrategy(MapGeneratorStrategy mapGeneratorStrategy) {
this.mapGeneratorStrategy = mapGeneratorStrategy;
return this;
}
public MapObjectBuilder ofKey(Types keyType) {
this.keyType = keyType;
return this;
}
public MapObjectBuilder ofValue(Types valueType) {
this.valueType = valueType;
return this;
}
@Override
public T build(Class type) {
return buildMap(type);
}
public T buildMap(Class type) {
return buildMap(type, keyType, valueType, size);
}
private T buildMap(Class type, Types keyType, Types valueType, int size) {
Validate.isTrue(Map.class.isAssignableFrom(type), "Map is required");
MapType mapType = MapTypeRegistry.get((Class
© 2015 - 2025 Weber Informatics LLC | Privacy Policy