From 8dd0e958d37b6c1dd11b95adb1e54883f66426f6 Mon Sep 17 00:00:00 2001 From: weisj <31143295+weisJ@users.noreply.github.com> Date: Tue, 17 Aug 2021 00:01:26 +0200 Subject: [PATCH] Utils: Fix errorprone warnings --- .../darklaf/platform/macos/MacOSLibrary.java | 4 +-- utils/build.gradle.kts | 1 + .../github/weisj/darklaf/util/Alignment.java | 20 ++++++++----- .../weisj/darklaf/util/AlignmentHelper.java | 8 ++++- .../github/weisj/darklaf/util/ColorUtil.java | 13 +++++---- .../github/weisj/darklaf/util/ImageUtil.java | 6 +++- .../weisj/darklaf/util/MutableLazyValue.java | 1 + .../github/weisj/darklaf/util/StreamUtil.java | 2 ++ .../github/weisj/darklaf/util/StringUtil.java | 29 ++++--------------- .../github/weisj/darklaf/util/SystemInfo.java | 11 +++++-- .../util/graphics/GraphicsContext.java | 2 ++ .../darklaf/util/graphics/GraphicsUtil.java | 2 ++ .../darklaf/util/graphics/ImagePainter.java | 7 ++--- .../weisj/darklaf/util/log/LogFormatter.java | 22 +++++++++----- 14 files changed, 74 insertions(+), 54 deletions(-) diff --git a/macos/src/main/java/com/github/weisj/darklaf/platform/macos/MacOSLibrary.java b/macos/src/main/java/com/github/weisj/darklaf/platform/macos/MacOSLibrary.java index eb3347ca..33a31190 100644 --- a/macos/src/main/java/com/github/weisj/darklaf/platform/macos/MacOSLibrary.java +++ b/macos/src/main/java/com/github/weisj/darklaf/platform/macos/MacOSLibrary.java @@ -35,8 +35,8 @@ public class MacOSLibrary extends AbstractLibrary { private static final String x86_64_PATH = PATH + "/libdarklaf-macos-x86-64.dylib"; private static final String arm64_PATH = PATH + "/libdarklaf-macos-arm64.dylib"; - private static final String FRAMEWORK_TARGET_PATH = "/JavaNativeFoundation.framework"; - private static final String FRAMEWORK_PATH = PATH + FRAMEWORK_TARGET_PATH + "/JavaNativeFoundation"; + private static final String FRAMEWORK_TARGET_PATH = "/JavaNativeFoundation.framework/"; + private static final String FRAMEWORK_PATH = PATH + FRAMEWORK_TARGET_PATH + "JavaNativeFoundation"; private static final MacOSLibrary instance = new MacOSLibrary(); public static MacOSLibrary get() { diff --git a/utils/build.gradle.kts b/utils/build.gradle.kts index 40185bb7..6a7c7b8b 100644 --- a/utils/build.gradle.kts +++ b/utils/build.gradle.kts @@ -5,6 +5,7 @@ plugins { dependencies { compileOnly(libs.nullabilityAnnotations) + compileOnly(libs.tools.errorprone.annotations) testImplementation(libs.test.junit.api) testRuntimeOnly(libs.test.junit.engine) diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/Alignment.java b/utils/src/main/java/com/github/weisj/darklaf/util/Alignment.java index 909fa0ce..9eb409c5 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/Alignment.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/Alignment.java @@ -23,10 +23,17 @@ package com.github.weisj.darklaf.util; import static com.github.weisj.darklaf.util.AlignmentHelper.*; -import java.awt.*; -import java.util.function.BiFunction; +import java.awt.Dimension; +import java.awt.Insets; +import java.awt.Point; +import java.awt.Rectangle; -/** @author Jannis Weis */ +/** + * Utility class for aligning points and rectangles inside other rectangles. It can also be used as + * a orientation marker value. + * + * @author Jannis Weis + */ public enum Alignment { NORTH(AlignmentHelper.align(HOR_CENTER_INSIDE, VERT_TOP_INSIDE), AlignmentHelper.align(HOR_CENTER_OUTSIDE, VERT_TOP_OUTSIDE)), @@ -47,11 +54,10 @@ public enum Alignment { CENTER(AlignmentHelper.align(HOR_CENTER_INSIDE, VERT_CENTER_INSIDE), AlignmentHelper.align(HOR_CENTER_OUTSIDE, VERT_CENTER_OUTSIDE)); - private final BiFunction alignInside; - private final BiFunction alignOutside; + private final AlignFunction alignInside; + private final AlignFunction alignOutside; - Alignment(final BiFunction alignInside, - final BiFunction alignOutside) { + Alignment(final AlignFunction alignInside, final AlignFunction alignOutside) { this.alignInside = alignInside; this.alignOutside = alignOutside; } diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/AlignmentHelper.java b/utils/src/main/java/com/github/weisj/darklaf/util/AlignmentHelper.java index b91f28ae..4f3afc71 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/AlignmentHelper.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/AlignmentHelper.java @@ -24,6 +24,8 @@ package com.github.weisj.darklaf.util; import java.awt.*; import java.util.function.BiFunction; +import com.google.errorprone.annotations.Immutable; + /** @author Jannis Weis */ final class AlignmentHelper { @@ -50,11 +52,15 @@ final class AlignmentHelper { * @param mapperY y component mapper. * @return mapper that aligns a rectangle relative to other rectangle. */ - static BiFunction align(final Mapper mapperX, final Mapper mapperY) { + static AlignFunction align(final Mapper mapperX, final Mapper mapperY) { return (d, p) -> new Point(mapperX.apply(d, p), mapperY.apply(d, p)); } /** Helper interface to avoid long type names. */ private interface Mapper extends BiFunction { } + + @Immutable + interface AlignFunction extends BiFunction { + } } diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java b/utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java index f7c1a9a4..87eaabe1 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java @@ -26,7 +26,11 @@ import java.awt.*; import javax.swing.plaf.UIResource; -/** @author Jannis Weis */ +/** + * Utility class for handling colors. + * + * @author Jannis Weis + */ public final class ColorUtil { public static Color blendColors(final Color color1, final Color color2, final double percent) { @@ -45,7 +49,7 @@ public final class ColorUtil { private static int shift(final int colorComponent, final double d) { int n = (int) Math.round((double) colorComponent * d); - return n > 255 ? 255 : (Math.max(n, 0)); + return n > 255 ? 255 : Math.max(n, 0); } public static Color toAlpha(final Color color, final double alpha) { @@ -114,10 +118,9 @@ public final class ColorUtil { } public static int rgbNoAlpha(final Color color) { - return ((0) << 24) | - ((color.getRed() & 0xFF) << 16) | + return ((color.getRed() & 0xFF) << 16) | ((color.getGreen() & 0xFF) << 8) | - ((color.getBlue() & 0xFF)); + (color.getBlue() & 0xFF); } /** diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/ImageUtil.java b/utils/src/main/java/com/github/weisj/darklaf/util/ImageUtil.java index d5ef97f4..4c9304d8 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/ImageUtil.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/ImageUtil.java @@ -28,7 +28,11 @@ import com.github.weisj.darklaf.util.graphics.GraphicsUtil; import com.github.weisj.darklaf.util.graphics.ScaledImage; -/** @author Jannis Weis */ +/** + * Utility class for creating images from components. + * + * @author Jannis Weis + */ public final class ImageUtil { private ImageUtil() {} diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/MutableLazyValue.java b/utils/src/main/java/com/github/weisj/darklaf/util/MutableLazyValue.java index a3988ff7..5084009d 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/MutableLazyValue.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/MutableLazyValue.java @@ -33,6 +33,7 @@ public class MutableLazyValue extends LazyValue { super(supplier); } + @Override public void set(final T value) { super.set(value); } diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/StreamUtil.java b/utils/src/main/java/com/github/weisj/darklaf/util/StreamUtil.java index 00ee05cc..3516c939 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/StreamUtil.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/StreamUtil.java @@ -34,10 +34,12 @@ public final class StreamUtil { public static Stream enumerationAsStream(final Enumeration e) { return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator() { + @Override public T next() { return e.nextElement(); } + @Override public boolean hasNext() { return e.hasMoreElements(); } diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/StringUtil.java b/utils/src/main/java/com/github/weisj/darklaf/util/StringUtil.java index 312c45f2..308c5f45 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/StringUtil.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/StringUtil.java @@ -26,6 +26,8 @@ import java.util.Collections; import java.util.List; /** + * Provides some additional utility functions for handling {@link String}s. + * * @author Konstantin Bulenkov * @author Jannis Weis */ @@ -71,15 +73,15 @@ public final class StringUtil { } } - public static List split(final String s, final String separator) { + public static Iterable split(final String s, final String separator) { return split(s, separator, true); } - public static List split(final String s, final String separator, final boolean excludeSeparator) { + public static Iterable split(final String s, final String separator, final boolean excludeSeparator) { return split(s, separator, excludeSeparator, true); } - public static List split(final String s, final String separator, final boolean excludeSeparator, + public static Iterable split(final String s, final String separator, final boolean excludeSeparator, final boolean excludeEmptyStrings) { if (separator.isEmpty()) { return Collections.singletonList(s); @@ -90,7 +92,7 @@ public final class StringUtil { while (true) { int index = s.indexOf(separator, pos); if (index == -1) { - if (pos < s.length() || !excludeEmptyStrings && pos == s.length()) { + if (pos < s.length() || (!excludeEmptyStrings && pos == s.length())) { result.add(s.substring(pos)); } @@ -108,25 +110,6 @@ public final class StringUtil { } } - public static int indexOfIgnoreCase(final String where, final char what, int fromIndex) { - int sourceCount = where.length(); - if (fromIndex >= sourceCount) { - return -1; - } else { - if (fromIndex < 0) { - fromIndex = 0; - } - - for (int i = fromIndex; i < sourceCount; ++i) { - if (charsEqualIgnoreCase(where.charAt(i), what)) { - return i; - } - } - - return -1; - } - } - public static boolean charsEqualIgnoreCase(final char a, final char b) { return a == b || toUpperCase(a) == toUpperCase(b) || toLowerCase(a) == toLowerCase(b); } diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/SystemInfo.java b/utils/src/main/java/com/github/weisj/darklaf/util/SystemInfo.java index d42fde12..8a27a34e 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/SystemInfo.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/SystemInfo.java @@ -24,7 +24,12 @@ package com.github.weisj.darklaf.util; import java.util.Locale; import java.util.regex.Pattern; -/** @author Konstantin Bulenkov */ +/** + * Provides basic information about the host jvm and operating system + * + * @author Konstantin Bulenkov + * @author Jannis Weis + */ public final class SystemInfo { public static final String X86 = "32"; public static final String X64 = "64"; @@ -59,7 +64,7 @@ public final class SystemInfo { public static final boolean isJava9OrGreater; public static final boolean isJava16OrGreater; - protected static final String _OS_NAME; + private static final String _OS_NAME; public static final boolean isMacOSMojave; public static final boolean isMacOSCatalina; @@ -121,7 +126,7 @@ public final class SystemInfo { String p2 = part2[idx]; int cmp; if (p1.matches("\\d+") && p2.matches("\\d+")) { - cmp = (Integer.valueOf(p1)).compareTo(Integer.valueOf(p2)); + cmp = Integer.valueOf(p1).compareTo(Integer.valueOf(p2)); } else { cmp = part1[idx].compareTo(part2[idx]); } diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/graphics/GraphicsContext.java b/utils/src/main/java/com/github/weisj/darklaf/util/graphics/GraphicsContext.java index 68e54a67..9ac76fe2 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/graphics/GraphicsContext.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/graphics/GraphicsContext.java @@ -26,6 +26,8 @@ import java.awt.geom.AffineTransform; import java.util.Map; /** + * Utility class for scoping changes to a {@link Graphics2D} object. + * * @author Konstantin Bulenkov * @author Jannis Weis */ diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/graphics/GraphicsUtil.java b/utils/src/main/java/com/github/weisj/darklaf/util/graphics/GraphicsUtil.java index d761daea..ff76fab8 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/graphics/GraphicsUtil.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/graphics/GraphicsUtil.java @@ -30,6 +30,8 @@ import com.github.weisj.darklaf.util.PropertyValue; import com.github.weisj.darklaf.util.SystemInfo; /** + * Utility class for creating {@link GraphicsContext}s. + * * @author Konstantin Bulenkov * @author Jannis Weis */ diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/graphics/ImagePainter.java b/utils/src/main/java/com/github/weisj/darklaf/util/graphics/ImagePainter.java index 014ba250..2b2ab49b 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/graphics/ImagePainter.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/graphics/ImagePainter.java @@ -58,9 +58,6 @@ public final class ImagePainter { drawImage(g, image, dstBounds, null, null, observer); } - /** - * @see #drawImage(Graphics, Image, Rectangle, Rectangle, BufferedImageOp, ImageObserver) - */ public static void drawImage(final Graphics g, final Image image, final Rectangle dstBounds, @@ -109,8 +106,8 @@ public final class ImagePainter { scaleX = scaledImage.getScaleX(); scaleY = scaledImage.getScaleY(); - userWidth /= scaleX; - userHeight /= scaleY; + userWidth = (int) (userWidth / scaleX); + userHeight = (int) (userHeight / scaleY); AffineTransform tx = ((Graphics2D) g).getTransform(); if (scaleX == tx.getScaleX() && scaleY == tx.getScaleY()) { diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/log/LogFormatter.java b/utils/src/main/java/com/github/weisj/darklaf/util/log/LogFormatter.java index 01eaae6b..ef0e1511 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/log/LogFormatter.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/log/LogFormatter.java @@ -21,10 +21,13 @@ */ package com.github.weisj.darklaf.util.log; -import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; import java.util.Collections; -import java.util.Date; import java.util.IdentityHashMap; +import java.util.Locale; import java.util.Set; import java.util.logging.Formatter; import java.util.logging.Level; @@ -33,6 +36,8 @@ import java.util.logging.LogRecord; import com.github.weisj.darklaf.util.StringUtil; /** + * The default log formatter for darklaf loggers. + * * @author Jannis Weis */ public class LogFormatter extends Formatter { @@ -48,6 +53,11 @@ public class LogFormatter extends Formatter { public static final String ANSI_BOLD_ON = "\u001B[01m"; public static final String ANSI_BOLD_OFF = "\u001B[2m"; + private final DateTimeFormatter dateTimeFormatter = + DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT) + .withLocale(Locale.UK) + .withZone(ZoneId.systemDefault()); + @Override public String format(final LogRecord record) { StringBuilder builder = new StringBuilder(); @@ -171,15 +181,13 @@ public class LogFormatter extends Formatter { } private String calculateDateString(final long milliseconds) { - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Date date = new Date(milliseconds); - return dateFormat.format(date); + return dateTimeFormatter.format(Instant.ofEpochMilli(milliseconds)); } private String getMessageColor(final LogRecord record) { - if (record.getLevel() == Level.SEVERE) { + if (record.getLevel().intValue() >= Level.SEVERE.intValue()) { return ANSI_RED; - } else if (record.getLevel() == Level.WARNING) { + } else if (record.getLevel().intValue() >= Level.WARNING.intValue()) { return ANSI_YELLOW; } else { return ANSI_BLACK;