From bf635989c050cf0cdb6b3921781b44c8a0e66f96 Mon Sep 17 00:00:00 2001 From: dvgaba Date: Thu, 4 Aug 2022 13:57:35 -0400 Subject: [PATCH] Fixed minor code smell and bugs (#8) --- .../org/jeasy/random/EasyRandomParameters.java | 3 +-- .../org/jeasy/random/RandomizationContext.java | 18 ++++++++++++------ .../org/jeasy/random/util/CharacterUtils.java | 4 ++-- .../org/jeasy/random/util/PrimitiveEnum.java | 4 ++-- .../org/jeasy/random/util/ReflectionUtils.java | 2 +- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/easy-random-core/src/main/java/org/jeasy/random/EasyRandomParameters.java b/easy-random-core/src/main/java/org/jeasy/random/EasyRandomParameters.java index 99164768..b93ff8c0 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/EasyRandomParameters.java +++ b/easy-random-core/src/main/java/org/jeasy/random/EasyRandomParameters.java @@ -35,7 +35,6 @@ import java.util.function.Predicate; import static java.lang.String.format; -import static java.time.ZonedDateTime.of; /** * Parameters of an {@link EasyRandom} instance. @@ -82,7 +81,7 @@ public class EasyRandomParameters { /** * Reference date around which random dates will be generated. */ - private static final ZonedDateTime REFERENCE_DATE = of(2020, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")); + private static final ZonedDateTime REFERENCE_DATE = ZonedDateTime.of(2020, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")); /** * Default dates range. diff --git a/easy-random-core/src/main/java/org/jeasy/random/RandomizationContext.java b/easy-random-core/src/main/java/org/jeasy/random/RandomizationContext.java index 537eeac1..d92dc629 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/RandomizationContext.java +++ b/easy-random-core/src/main/java/org/jeasy/random/RandomizationContext.java @@ -40,9 +40,9 @@ class RandomizationContext implements RandomizerContext { private final EasyRandomParameters parameters; - private final Map, List> populatedBeans; + private final IdentityHashMap, List> populatedBeans; - private final Stack stack; + private final Deque stack; private final Class type; @@ -53,7 +53,7 @@ class RandomizationContext implements RandomizerContext { RandomizationContext(final Class type, final EasyRandomParameters parameters) { this.type = type; populatedBeans = new IdentityHashMap<>(); - stack = new Stack<>(); + stack = new ArrayDeque<>(); this.parameters = parameters; this.random = new Random(parameters.getSeed()); } @@ -100,7 +100,13 @@ boolean hasExceededRandomizationDepth() { } private List getStackedFieldNames() { - return stack.stream().map(i -> i.getField().getName()).collect(toList()); + + List collect = new ArrayList<>(); + Iterator it = stack.descendingIterator(); + while(it.hasNext()) { + collect.add(it.next().getField().getName()); + } + return collect; } private List toLowerCase(final List strings) { @@ -120,11 +126,11 @@ public Class getTargetType() { @Override public Object getCurrentObject() { - if (stack.empty()) { + if (stack.isEmpty()) { return rootObject; } else { - return stack.lastElement().getObject(); + return stack.peek().getObject(); } } diff --git a/easy-random-core/src/main/java/org/jeasy/random/util/CharacterUtils.java b/easy-random-core/src/main/java/org/jeasy/random/util/CharacterUtils.java index 5ed38f00..5af52fb9 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/util/CharacterUtils.java +++ b/easy-random-core/src/main/java/org/jeasy/random/util/CharacterUtils.java @@ -75,7 +75,7 @@ public static List filterLetters(List characters) { private static boolean isPrintable(char character) { Character.UnicodeBlock block = Character.UnicodeBlock.of(character); - return (!Character.isISOControl(character)) && block != null && block != Character.UnicodeBlock.SPECIALS; + return !Character.isISOControl(character) && !Character.UnicodeBlock.SPECIALS.equals(block); } -} +} \ No newline at end of file diff --git a/easy-random-core/src/main/java/org/jeasy/random/util/PrimitiveEnum.java b/easy-random-core/src/main/java/org/jeasy/random/util/PrimitiveEnum.java index 9e4a4edd..b3dd24c5 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/util/PrimitiveEnum.java +++ b/easy-random-core/src/main/java/org/jeasy/random/util/PrimitiveEnum.java @@ -39,8 +39,8 @@ enum PrimitiveEnum { BOOLEAN(Boolean.TYPE, Boolean.class), CHARACTER(Character.TYPE, Character.class); - private Class type; - private Class clazz; + private final Class type; + private final Class clazz; PrimitiveEnum(Class type, Class clazz) { this.type = type; diff --git a/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java b/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java index c7b078ee..0630b80f 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java +++ b/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java @@ -447,7 +447,7 @@ public static T getAnnotation(Field field, Class annot */ public static boolean isAnnotationPresent(Field field, Class annotationType) { final Optional readMethod = getReadMethod(field); - return field.isAnnotationPresent(annotationType) || readMethod.isPresent() && readMethod.get().isAnnotationPresent(annotationType); + return field.isAnnotationPresent(annotationType) || (readMethod.isPresent() && readMethod.get().isAnnotationPresent(annotationType)); } /**