
com.arsframework.util.Randoms Maven / Gradle / Ivy
package com.arsframework.util;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.lang.reflect.Field;
import java.lang.reflect.Array;
import com.arsframework.annotation.Lt;
import com.arsframework.annotation.Min;
import com.arsframework.annotation.Nonnull;
/**
* 随机数处理工具类
*
* @author yongqiang.wu
* @version 2019-03-22 09:38
*/
public abstract class Randoms {
/**
* 当前线程随机处理对象
*/
private static final ThreadLocal random = ThreadLocal.withInitial(() -> new Random());
/**
* 随机数生成接口
*
* @param 数据类型
* @author yongqiangwu
*/
public interface Generator {
/**
* 生成随机数
*
* @return 随机数
*/
T generate();
}
/**
* 随机生成对象属性排器接口
*
* @author yongqiangwu
*/
public interface Excluder {
/**
* 判断是否排除
*
* @param type 对象类型
* @param field 字段对象
* @return true/false
*/
boolean excluded(Class> type, Field field);
}
/**
* 对象属性随机数生成接口工厂
*
* @author yongqiangwu
*/
public interface GeneratorBuilder {
/**
* 构建随机数生成接口
*
* @param 数据类型
* @param type 对象类型
* @param field 字段对象
* @return 随机数生成接口
*/
Generator buildGenerator(Class type, Field field);
}
/**
* 随机对象实例生成工厂
*
* @param 对象类型
* @author yongqiangwu
*/
public static class RandomBeanFactory {
protected final Class type; // 对象类型
private Excluder excluder;
private GeneratorBuilder generatorBuilder;
private final LinkedList> executed = new LinkedList<>(); // 已执行对象类型
@Nonnull
public RandomBeanFactory(Class type) {
this.type = type;
}
/**
* 执行对象实例构建
*
* @param 对象类型
* @param type 对象类型
* @return 对象实例
*/
@Nonnull
protected M execute(Class type) {
if (this.excluder != null && this.excluder.excluded(type, null)) {
return null;
}
Generator> generator = this.generatorBuilder == null ? null : this.generatorBuilder.buildGenerator(type, null);
if (generator != null) {
return (M) generator.generate();
}
if (Enum.class.isAssignableFrom(type)) {
return (M) randomEnum((Class>) type);
} else if (Date.class.isAssignableFrom(type)) {
return (M) randomDate();
} else if (LocalDate.class.isAssignableFrom(type)) {
return (M) Dates.adapter(randomDate()).toLocalDate();
} else if (LocalDateTime.class.isAssignableFrom(type)) {
return (M) Dates.adapter(randomDate());
} else if (type == byte.class || type == Byte.class) {
return (M) Byte.valueOf((byte) randomInteger());
} else if (type == char.class || type == Character.class) {
return (M) randomCharacter();
} else if (type == short.class || type == Short.class) {
return (M) Short.valueOf((short) randomInteger());
} else if (type == float.class || type == Float.class) {
return (M) Float.valueOf(randomInteger());
} else if (type == double.class || type == Double.class) {
return (M) Double.valueOf(randomInteger());
} else if (type == int.class || type == Integer.class) {
return (M) Integer.valueOf(randomInteger());
} else if (type == BigInteger.class) {
return (M) new BigInteger(String.valueOf(randomInteger()));
} else if (type == BigDecimal.class) {
return (M) new BigDecimal(String.valueOf(randomInteger()));
} else if (type == long.class || type == Long.class) {
return (M) Long.valueOf(randomInteger());
} else if (type == boolean.class || type == Boolean.class) {
return (M) Boolean.valueOf(randomBoolean());
} else if (type == String.class) {
return (M) randomString();
} else if (type.isArray()) {
Class> component = type.getComponentType();
Object[] array = (Object[]) Array.newInstance(component, 1);
array[0] = this.execute(component);
return (M) array;
}
if (this.executed.contains(type)) {
return null;
}
this.executed.add(type);
M instance = Objects.initialize(type);
for (Field field : Objects.getFields(type)) {
if (this.excluder != null && this.excluder.excluded(type, field)) {
continue;
}
Object value;
generator = this.generatorBuilder == null ? null : this.generatorBuilder.buildGenerator(type, field);
if (generator != null) {
value = generator.generate();
} else if (Map.class.isAssignableFrom(field.getType())) {
Class>[] genericTypes = Objects.getGenericTypes(field);
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy