From 452832eb738df1ec02f8a35ea3be20c7bb6b3189 Mon Sep 17 00:00:00 2001 From: Michael Schnell Date: Sun, 4 Feb 2024 10:27:37 +0100 Subject: [PATCH] Fixed Sonar issues --- .../org/fuin/objects4j/core/CurrencyAmount.java | 5 +---- .../fuin/objects4j/core/DateStrValidator.java | 2 +- .../org/fuin/objects4j/core/DayOfTheWeek.java | 15 +++------------ .../core/DayOfTheWeekStrValidator.java | 2 +- .../fuin/objects4j/core/DayOpeningHours.java | 5 +---- .../core/DayOpeningHoursStrValidator.java | 2 +- .../main/java/org/fuin/objects4j/core/Hour.java | 4 ++-- .../objects4j/core/HourRangeStrValidator.java | 2 +- .../org/fuin/objects4j/core/HourRanges.java | 17 +++++++---------- .../objects4j/core/HourRangesStrValidator.java | 2 +- .../fuin/objects4j/core/HourStrValidator.java | 2 +- .../java/org/fuin/objects4j/core/KeyValue.java | 3 +-- .../fuin/objects4j/core/MultiDayOfTheWeek.java | 4 +--- .../core/MultiDayOfTheWeekStrValidator.java | 2 +- .../org/fuin/objects4j/core/PasswordSha512.java | 6 +++--- .../objects4j/core/PasswordStrValidator.java | 2 +- .../core/PropertiesContainValidator.java | 2 +- .../core/TrimmedNotEmptyValidator.java | 2 +- .../fuin/objects4j/core/UUIDStrValidator.java | 2 +- .../fuin/objects4j/core/WeeklyOpeningHours.java | 2 +- .../core/WeeklyOpeningHoursStrValidator.java | 2 +- 21 files changed, 32 insertions(+), 53 deletions(-) diff --git a/core/src/main/java/org/fuin/objects4j/core/CurrencyAmount.java b/core/src/main/java/org/fuin/objects4j/core/CurrencyAmount.java index ffa3a87..2452917 100644 --- a/core/src/main/java/org/fuin/objects4j/core/CurrencyAmount.java +++ b/core/src/main/java/org/fuin/objects4j/core/CurrencyAmount.java @@ -130,10 +130,7 @@ public boolean equals(final Object obj) { if (!amount.equals(other.amount)) { return false; } - if (!currency.getCurrencyCode().equals(other.currency.getCurrencyCode())) { - return false; - } - return true; + return currency.getCurrencyCode().equals(other.currency.getCurrencyCode()); } @Override diff --git a/core/src/main/java/org/fuin/objects4j/core/DateStrValidator.java b/core/src/main/java/org/fuin/objects4j/core/DateStrValidator.java index f791462..1e2cdc3 100644 --- a/core/src/main/java/org/fuin/objects4j/core/DateStrValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/DateStrValidator.java @@ -41,7 +41,7 @@ public final boolean isValid(final String value, final ConstraintValidatorContex if (value == null) { return true; } - if (value.length() == 0) { + if (value.isEmpty()) { return false; } try { diff --git a/core/src/main/java/org/fuin/objects4j/core/DayOfTheWeek.java b/core/src/main/java/org/fuin/objects4j/core/DayOfTheWeek.java index 9fa97ea..e6d9b17 100644 --- a/core/src/main/java/org/fuin/objects4j/core/DayOfTheWeek.java +++ b/core/src/main/java/org/fuin/objects4j/core/DayOfTheWeek.java @@ -190,11 +190,8 @@ public boolean equals(Object obj) { if (getClass() != obj.getClass()) { return false; } - DayOfTheWeek other = (DayOfTheWeek) obj; - if (id != other.id) { - return false; - } - return true; + final DayOfTheWeek other = (DayOfTheWeek) obj; + return id == other.id; } /** @@ -208,13 +205,7 @@ public final int length() { @Override public int compareTo(final DayOfTheWeek other) { - if (id > other.id) { - return 1; - } - if (id < other.id) { - return -1; - } - return 0; + return Integer.compare(id, other.id); } @Override diff --git a/core/src/main/java/org/fuin/objects4j/core/DayOfTheWeekStrValidator.java b/core/src/main/java/org/fuin/objects4j/core/DayOfTheWeekStrValidator.java index f54616e..a6bfbbb 100644 --- a/core/src/main/java/org/fuin/objects4j/core/DayOfTheWeekStrValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/DayOfTheWeekStrValidator.java @@ -43,7 +43,7 @@ public final boolean isValid(final String value, final ConstraintValidatorContex * * @return Returns {@literal true} if it's a valid string else {@literal false} is returned. */ - public static final boolean isValid(final String value) { + public static boolean isValid(final String value) { return DayOfTheWeek.isValid(value); } diff --git a/core/src/main/java/org/fuin/objects4j/core/DayOpeningHours.java b/core/src/main/java/org/fuin/objects4j/core/DayOpeningHours.java index 9fb55a3..2a32d95 100644 --- a/core/src/main/java/org/fuin/objects4j/core/DayOpeningHours.java +++ b/core/src/main/java/org/fuin/objects4j/core/DayOpeningHours.java @@ -598,10 +598,7 @@ public final boolean equals(final Object obj) { } else if (!range.equals(other.range)) { return false; } - if (type != other.type) { - return false; - } - return true; + return type == other.type; } @Override diff --git a/core/src/main/java/org/fuin/objects4j/core/DayOpeningHoursStrValidator.java b/core/src/main/java/org/fuin/objects4j/core/DayOpeningHoursStrValidator.java index c10eb62..1049e94 100644 --- a/core/src/main/java/org/fuin/objects4j/core/DayOpeningHoursStrValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/DayOpeningHoursStrValidator.java @@ -50,7 +50,7 @@ public final boolean isValid(final String value, final ConstraintValidatorContex * * @return Returns {@literal true} if it's a valid string representation else {@literal false} is returned. */ - public static final boolean isValid(final String value) { + public static boolean isValid(final String value) { return DayOpeningHours.isValid(value); } diff --git a/core/src/main/java/org/fuin/objects4j/core/Hour.java b/core/src/main/java/org/fuin/objects4j/core/Hour.java index 5b88322..155babf 100644 --- a/core/src/main/java/org/fuin/objects4j/core/Hour.java +++ b/core/src/main/java/org/fuin/objects4j/core/Hour.java @@ -82,8 +82,8 @@ public Hour(@NotNull @HourStr final String hour) { super(); Contract.requireArgNotEmpty("hour", hour); requireArgValid("hour", hour); - this.hourValue = Integer.valueOf(hour.substring(0, 2)); - this.minuteValue = Integer.valueOf(hour.substring(3)); + this.hourValue = Integer.parseInt(hour.substring(0, 2)); + this.minuteValue = Integer.parseInt(hour.substring(3)); } /** diff --git a/core/src/main/java/org/fuin/objects4j/core/HourRangeStrValidator.java b/core/src/main/java/org/fuin/objects4j/core/HourRangeStrValidator.java index 3b0bd2d..29d9816 100644 --- a/core/src/main/java/org/fuin/objects4j/core/HourRangeStrValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/HourRangeStrValidator.java @@ -52,7 +52,7 @@ public final boolean isValid(final String value, final ConstraintValidatorContex * * @return Returns {@literal true} if it's a valid string representation else {@literal false} is returned. */ - public static final boolean isValid(final String value) { + public static boolean isValid(final String value) { return HourRange.isValid(value); } diff --git a/core/src/main/java/org/fuin/objects4j/core/HourRanges.java b/core/src/main/java/org/fuin/objects4j/core/HourRanges.java index 87bd766..a7def30 100644 --- a/core/src/main/java/org/fuin/objects4j/core/HourRanges.java +++ b/core/src/main/java/org/fuin/objects4j/core/HourRanges.java @@ -143,9 +143,9 @@ public final List normalize() { } } final List list = new ArrayList<>(); - list.add(new HourRanges(today.toArray(new HourRange[today.size()]))); - if (tommorrow.size() > 0) { - list.add(new HourRanges(tommorrow.toArray(new HourRange[tommorrow.size()]))); + list.add(new HourRanges(today.toArray(new HourRange[0]))); + if (!tommorrow.isEmpty()) { + list.add(new HourRanges(tommorrow.toArray(new HourRange[0]))); } return list; } @@ -379,7 +379,7 @@ public final String toString() { private static String asStr(final List ranges) { final StringBuilder sb = new StringBuilder(); for (final HourRange range : ranges) { - if (sb.length() > 0) { + if (!sb.isEmpty()) { sb.append("+"); } sb.append(range.toString()); @@ -478,7 +478,7 @@ private static HourRanges valueOf(@Nullable final BitSet minutes, final int max) ranges.add(createHourRange(startHour, startMinute, minutes.length())); } - return new HourRanges(ranges.toArray(new HourRange[ranges.size()])); + return new HourRanges(ranges.toArray(new HourRange[0])); } @@ -518,7 +518,7 @@ public static void requireArgValid(@NotNull final String name, @NotNull final St /** * Types of changes. */ - public static enum ChangeType { + public enum ChangeType { /** An hour range was added. */ ADDED, @@ -599,10 +599,7 @@ public final boolean equals(final Object obj) { } else if (!range.equals(other.range)) { return false; } - if (type != other.type) { - return false; - } - return true; + return type == other.type; } @Override diff --git a/core/src/main/java/org/fuin/objects4j/core/HourRangesStrValidator.java b/core/src/main/java/org/fuin/objects4j/core/HourRangesStrValidator.java index 8cf7d46..cb1c9ed 100644 --- a/core/src/main/java/org/fuin/objects4j/core/HourRangesStrValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/HourRangesStrValidator.java @@ -44,7 +44,7 @@ public final boolean isValid(final String value, final ConstraintValidatorContex * * @return Returns {@literal true} if it's a valid string representation else {@literal false} is returned. */ - public static final boolean isValid(final String value) { + public static boolean isValid(final String value) { return HourRanges.isValid(value); } diff --git a/core/src/main/java/org/fuin/objects4j/core/HourStrValidator.java b/core/src/main/java/org/fuin/objects4j/core/HourStrValidator.java index 3929237..6b4f526 100644 --- a/core/src/main/java/org/fuin/objects4j/core/HourStrValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/HourStrValidator.java @@ -52,7 +52,7 @@ public final boolean isValid(final String value, final ConstraintValidatorContex * * @return Returns {@literal true} if it's a valid Hourd else {@literal false} is returned. */ - public static final boolean isValid(final String value) { + public static boolean isValid(final String value) { return Hour.isValid(value); } diff --git a/core/src/main/java/org/fuin/objects4j/core/KeyValue.java b/core/src/main/java/org/fuin/objects4j/core/KeyValue.java index 13ccda2..ce9fe28 100644 --- a/core/src/main/java/org/fuin/objects4j/core/KeyValue.java +++ b/core/src/main/java/org/fuin/objects4j/core/KeyValue.java @@ -106,9 +106,8 @@ public static String replace(final String message, final KeyValue... keyValue) { } final Map map = new HashMap(); for (final KeyValue kv : keyValue) { - if (kv.getValue() instanceof Collection) { + if (kv.getValue() instanceof Collection coll) { final StringBuilder sb = new StringBuilder(); - final Collection coll = (Collection) kv.getValue(); int count = 0; for (final Object entry : coll) { if (count > 0) { diff --git a/core/src/main/java/org/fuin/objects4j/core/MultiDayOfTheWeek.java b/core/src/main/java/org/fuin/objects4j/core/MultiDayOfTheWeek.java index cbfcdbc..cfdf24e 100644 --- a/core/src/main/java/org/fuin/objects4j/core/MultiDayOfTheWeek.java +++ b/core/src/main/java/org/fuin/objects4j/core/MultiDayOfTheWeek.java @@ -78,9 +78,7 @@ public MultiDayOfTheWeek(@NotNull @MultiDayOfTheWeekStr final String multipleDay if (p > -1) { final DayOfTheWeek from = DayOfTheWeek.valueOf(part.substring(0, p)); final DayOfTheWeek to = DayOfTheWeek.valueOf(part.substring(p + 1)); - for (final DayOfTheWeek dow : DayOfTheWeek.getPart(from, to)) { - this.multipleDayOfTheWeek.add(dow); - } + this.multipleDayOfTheWeek.addAll(DayOfTheWeek.getPart(from, to)); } else { this.multipleDayOfTheWeek.add(DayOfTheWeek.valueOf(part)); } diff --git a/core/src/main/java/org/fuin/objects4j/core/MultiDayOfTheWeekStrValidator.java b/core/src/main/java/org/fuin/objects4j/core/MultiDayOfTheWeekStrValidator.java index 3b6386a..496bdff 100644 --- a/core/src/main/java/org/fuin/objects4j/core/MultiDayOfTheWeekStrValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/MultiDayOfTheWeekStrValidator.java @@ -44,7 +44,7 @@ public final boolean isValid(final String value, final ConstraintValidatorContex * * @return Returns {@literal true} if it's a valid string representation else {@literal false} is returned. */ - public static final boolean isValid(final String value) { + public static boolean isValid(final String value) { return MultiDayOfTheWeek.isValid(value); } diff --git a/core/src/main/java/org/fuin/objects4j/core/PasswordSha512.java b/core/src/main/java/org/fuin/objects4j/core/PasswordSha512.java index 20d175a..f6fe484 100644 --- a/core/src/main/java/org/fuin/objects4j/core/PasswordSha512.java +++ b/core/src/main/java/org/fuin/objects4j/core/PasswordSha512.java @@ -86,9 +86,9 @@ private static String toHex(final byte[] hash) { final int l = hash.length; final char[] out = new char[l << 1]; int j = 0; - for (int i = 0; i < l; i++) { - out[j++] = DIGITS[(0xF0 & hash[i]) >>> 4]; - out[j++] = DIGITS[0x0F & hash[i]]; + for (byte b : hash) { + out[j++] = DIGITS[(0xF0 & b) >>> 4]; + out[j++] = DIGITS[0x0F & b]; } return String.copyValueOf(out); } diff --git a/core/src/main/java/org/fuin/objects4j/core/PasswordStrValidator.java b/core/src/main/java/org/fuin/objects4j/core/PasswordStrValidator.java index 18be895..f1d83b2 100644 --- a/core/src/main/java/org/fuin/objects4j/core/PasswordStrValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/PasswordStrValidator.java @@ -45,7 +45,7 @@ public final boolean isValid(final String value, final ConstraintValidatorContex * * @return Returns {@literal true} if it's an allowed password else {@literal false} is returned. */ - public static final boolean isValid(final String value) { + public static boolean isValid(final String value) { if (value == null) { return true; } diff --git a/core/src/main/java/org/fuin/objects4j/core/PropertiesContainValidator.java b/core/src/main/java/org/fuin/objects4j/core/PropertiesContainValidator.java index 52ace85..25210e8 100644 --- a/core/src/main/java/org/fuin/objects4j/core/PropertiesContainValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/PropertiesContainValidator.java @@ -34,7 +34,7 @@ public final class PropertiesContainValidator implements ConstraintValidator(); + expectedKeys = new HashSet<>(); expectedKeys.addAll(Arrays.asList(constraintAnnotation.value())); } diff --git a/core/src/main/java/org/fuin/objects4j/core/TrimmedNotEmptyValidator.java b/core/src/main/java/org/fuin/objects4j/core/TrimmedNotEmptyValidator.java index 028a68c..2ac846b 100644 --- a/core/src/main/java/org/fuin/objects4j/core/TrimmedNotEmptyValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/TrimmedNotEmptyValidator.java @@ -33,7 +33,7 @@ public final boolean isValid(final String value, final ConstraintValidatorContex if (value == null) { return true; } - return value.trim().length() > 0; + return !value.trim().isEmpty(); } /** diff --git a/core/src/main/java/org/fuin/objects4j/core/UUIDStrValidator.java b/core/src/main/java/org/fuin/objects4j/core/UUIDStrValidator.java index 43eb96e..1811a6d 100644 --- a/core/src/main/java/org/fuin/objects4j/core/UUIDStrValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/UUIDStrValidator.java @@ -47,7 +47,7 @@ public final boolean isValid(final String value, final ConstraintValidatorContex * * @return Returns {@literal true} if it's a valid UUIDd else {@literal false} is returned. */ - public static final boolean isValid(final String value) { + public static boolean isValid(final String value) { if (value == null) { return true; } diff --git a/core/src/main/java/org/fuin/objects4j/core/WeeklyOpeningHours.java b/core/src/main/java/org/fuin/objects4j/core/WeeklyOpeningHours.java index e496452..f4555ec 100644 --- a/core/src/main/java/org/fuin/objects4j/core/WeeklyOpeningHours.java +++ b/core/src/main/java/org/fuin/objects4j/core/WeeklyOpeningHours.java @@ -160,7 +160,7 @@ public WeeklyOpeningHours normalize() { Collections.sort(days); - return new WeeklyOpeningHours(days.toArray(new DayOpeningHours[days.size()])); + return new WeeklyOpeningHours(days.toArray(new DayOpeningHours[0])); } diff --git a/core/src/main/java/org/fuin/objects4j/core/WeeklyOpeningHoursStrValidator.java b/core/src/main/java/org/fuin/objects4j/core/WeeklyOpeningHoursStrValidator.java index 3ebd32f..a0f1422 100644 --- a/core/src/main/java/org/fuin/objects4j/core/WeeklyOpeningHoursStrValidator.java +++ b/core/src/main/java/org/fuin/objects4j/core/WeeklyOpeningHoursStrValidator.java @@ -44,7 +44,7 @@ public final boolean isValid(final String value, final ConstraintValidatorContex * * @return Returns {@literal true} if it's a valid string representation else {@literal false} is returned. */ - public static final boolean isValid(final String value) { + public static boolean isValid(final String value) { return WeeklyOpeningHours.isValid(value); }