Created
January 5, 2023 14:33
-
-
Save bozzelliandrea/7599defa3a0d80dd33d700182f4943e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.NonNull; | |
import lombok.SneakyThrows; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.lang.reflect.Field; | |
import java.util.*; | |
public final class Resource { | |
public static final Map<Class<?>, RandomCallback<?>> PRIMITIVE_WRAPPER_TYPES = getWrapperTypes(); | |
private static final Logger LOGGER = LoggerFactory.getLogger(Resource.class); | |
public static <T> List<T> randomList(final Class<T> type, final PageModel pagination) { | |
return randomList(type, pagination.getTotal()); | |
} | |
public static <T> List<T> randomList(final Class<T> type, final int size) { | |
final List<T> elements = new ArrayList<>(); | |
for (int i = 0; i < size; i++) { | |
try { | |
elements.add(randomizer(type.newInstance())); | |
} catch (Exception e) { | |
LOGGER.error(e.getMessage()); | |
e.printStackTrace(); | |
return elements; | |
} | |
} | |
return elements; | |
} | |
@SneakyThrows | |
public static <T> T randomizer(@NonNull T obj) { | |
Class<?> clazz = obj.getClass(); | |
for (Field field : clazz.getDeclaredFields()) { | |
field.setAccessible(true); | |
if (!field.getType().isPrimitive()) { | |
if (PRIMITIVE_WRAPPER_TYPES.containsKey(field.getType())) { | |
field.set(obj, PRIMITIVE_WRAPPER_TYPES.get(field.getType()).execute()); | |
} else { | |
try { | |
if (field.getType().getName().contains("List")) | |
field.set(obj, Collections.singletonList(randomizer(field.getType().newInstance()))); | |
else | |
field.set(obj, randomizer(field.getType().newInstance())); | |
} catch (Exception e) { | |
//wildcart fallback | |
field.set(obj, null); | |
} | |
} | |
} | |
} | |
return obj; | |
} | |
private static Map<Class<?>, RandomCallback<?>> getWrapperTypes() { | |
Map<Class<?>, RandomCallback<?>> ret = new HashMap<>(); | |
ret.put(Boolean.class, () -> new Random().nextBoolean()); | |
ret.put(Character.class, () -> generateRandomWords(1)[0].subSequence(0, 1)); | |
ret.put(Byte.class, () -> Byte.MIN_VALUE); | |
ret.put(Short.class, () -> Short.MAX_VALUE); | |
ret.put(Integer.class, () -> Integer.MAX_VALUE); | |
ret.put(Long.class, () -> Long.MAX_VALUE); | |
ret.put(Float.class, () -> Float.MAX_VALUE); | |
ret.put(Double.class, () -> Double.MAX_VALUE); | |
ret.put(Void.class, () -> Void.TYPE); | |
ret.put(String.class, () -> generateRandomPhrase(4)); | |
return ret; | |
} | |
public static String generateRandomPhrase(int numberOfWords) { | |
return String.join(" ", generateRandomWords(numberOfWords)); | |
} | |
public static String[] generateRandomWords(int numberOfWords) { | |
String[] randomStrings = new String[numberOfWords]; | |
Random random = new Random(); | |
for (int i = 0; i < numberOfWords; i++) { | |
char[] word = new char[random.nextInt(8) + 3]; | |
for (int j = 0; j < word.length; j++) { | |
word[j] = (char) ('a' + random.nextInt(26)); | |
} | |
randomStrings[i] = new String(word); | |
} | |
return randomStrings; | |
} | |
@FunctionalInterface | |
public interface RandomCallback<T> { | |
T execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment