Browse Source

Utils: Fix errorprone warnings

pull/270/head
weisj 3 years ago
parent
commit
8dd0e958d3
No known key found for this signature in database
GPG Key ID: 31124CB75461DA2A
  1. 4
      macos/src/main/java/com/github/weisj/darklaf/platform/macos/MacOSLibrary.java
  2. 1
      utils/build.gradle.kts
  3. 20
      utils/src/main/java/com/github/weisj/darklaf/util/Alignment.java
  4. 8
      utils/src/main/java/com/github/weisj/darklaf/util/AlignmentHelper.java
  5. 13
      utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java
  6. 6
      utils/src/main/java/com/github/weisj/darklaf/util/ImageUtil.java
  7. 1
      utils/src/main/java/com/github/weisj/darklaf/util/MutableLazyValue.java
  8. 2
      utils/src/main/java/com/github/weisj/darklaf/util/StreamUtil.java
  9. 29
      utils/src/main/java/com/github/weisj/darklaf/util/StringUtil.java
  10. 11
      utils/src/main/java/com/github/weisj/darklaf/util/SystemInfo.java
  11. 2
      utils/src/main/java/com/github/weisj/darklaf/util/graphics/GraphicsContext.java
  12. 2
      utils/src/main/java/com/github/weisj/darklaf/util/graphics/GraphicsUtil.java
  13. 7
      utils/src/main/java/com/github/weisj/darklaf/util/graphics/ImagePainter.java
  14. 22
      utils/src/main/java/com/github/weisj/darklaf/util/log/LogFormatter.java

4
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() {

1
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)

20
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<Dimension, Rectangle, Point> alignInside;
private final BiFunction<Dimension, Rectangle, Point> alignOutside;
private final AlignFunction alignInside;
private final AlignFunction alignOutside;
Alignment(final BiFunction<Dimension, Rectangle, Point> alignInside,
final BiFunction<Dimension, Rectangle, Point> alignOutside) {
Alignment(final AlignFunction alignInside, final AlignFunction alignOutside) {
this.alignInside = alignInside;
this.alignOutside = alignOutside;
}

8
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<Dimension, Rectangle, Point> 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<Dimension, Rectangle, Integer> {
}
@Immutable
interface AlignFunction extends BiFunction<Dimension, Rectangle, Point> {
}
}

13
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);
}
/**

6
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() {}

1
utils/src/main/java/com/github/weisj/darklaf/util/MutableLazyValue.java

@ -33,6 +33,7 @@ public class MutableLazyValue<T> extends LazyValue<T> {
super(supplier);
}
@Override
public void set(final T value) {
super.set(value);
}

2
utils/src/main/java/com/github/weisj/darklaf/util/StreamUtil.java

@ -34,10 +34,12 @@ public final class StreamUtil {
public static <T> Stream<T> enumerationAsStream(final Enumeration<T> e) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<T>() {
@Override
public T next() {
return e.nextElement();
}
@Override
public boolean hasNext() {
return e.hasMoreElements();
}

29
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<String> split(final String s, final String separator) {
public static Iterable<String> split(final String s, final String separator) {
return split(s, separator, true);
}
public static List<String> split(final String s, final String separator, final boolean excludeSeparator) {
public static Iterable<String> split(final String s, final String separator, final boolean excludeSeparator) {
return split(s, separator, excludeSeparator, true);
}
public static List<String> split(final String s, final String separator, final boolean excludeSeparator,
public static Iterable<String> 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);
}

11
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]);
}

2
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
*/

2
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
*/

7
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()) {

22
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;

Loading…
Cancel
Save