From 2c9ef7630340f6b0b008e7eb6c2358a0f45c2f4f Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Sat, 18 Nov 2023 18:32:48 +0100 Subject: [PATCH] fix qodana warnings --- .../com/dua3/utility/swing/SwingFontUtil.java | 3 ++- .../java/com/dua3/utility/text/RichText.java | 8 +++--- .../java/com/dua3/utility/text/TextUtil.java | 6 ++--- .../com/dua3/utility/text/TextUtilTest.java | 25 +++++++++++++++++++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/utility-swing/src/main/java/com/dua3/utility/swing/SwingFontUtil.java b/utility-swing/src/main/java/com/dua3/utility/swing/SwingFontUtil.java index e7fd39c8..9f87f858 100644 --- a/utility-swing/src/main/java/com/dua3/utility/swing/SwingFontUtil.java +++ b/utility-swing/src/main/java/com/dua3/utility/swing/SwingFontUtil.java @@ -30,7 +30,8 @@ public class SwingFontUtil implements FontUtil { private final WeakHashMap fontMap = new WeakHashMap<>(); private static java.awt.Font getAwtFont(String family, float size, boolean bold, boolean italic) { - int style = (bold ? java.awt.Font.BOLD : 0) | (italic ? java.awt.Font.ITALIC : 0); + int style = (bold ? java.awt.Font.BOLD : java.awt.Font.PLAIN) + | (italic ? java.awt.Font.ITALIC : java.awt.Font.PLAIN); return new java.awt.Font(family, style, Math.round(size)); } diff --git a/utility/src/main/java/com/dua3/utility/text/RichText.java b/utility/src/main/java/com/dua3/utility/text/RichText.java index 03305e0a..8d4d8bc8 100644 --- a/utility/src/main/java/com/dua3/utility/text/RichText.java +++ b/utility/src/main/java/com/dua3/utility/text/RichText.java @@ -757,11 +757,11 @@ public RichText[] split(String regex, int limit) { result.add(subSequence(index, length())); } - // remove trailing emoty segments + // remove trailing empty segments if (limit == 0) { - int s; - for (s = result.size(); s>0 && result.get(s-1).isEmpty(); s--) { - // nop + int s = result.size(); + while ( s>0 && result.get(s-1).isEmpty()) { + s--; } result = result.subList(0, s); } diff --git a/utility/src/main/java/com/dua3/utility/text/TextUtil.java b/utility/src/main/java/com/dua3/utility/text/TextUtil.java index cfea0b99..33f23333 100644 --- a/utility/src/main/java/com/dua3/utility/text/TextUtil.java +++ b/utility/src/main/java/com/dua3/utility/text/TextUtil.java @@ -270,9 +270,9 @@ public static void transform(String template, public static float decodeFontSize(String s) { s = s.strip().toLowerCase(Locale.ROOT); - int idxUnit; - for (idxUnit = s.length(); idxUnit>0 && !Character.isDigit(s.charAt(idxUnit-1)); idxUnit--) { - // nop + int idxUnit = s.length(); + while (idxUnit > 0 && !Character.isDigit(s.charAt(idxUnit - 1))) { + idxUnit--; } String unit = s.substring(idxUnit).strip(); String number = s.substring(0,idxUnit).strip(); diff --git a/utility/src/test/java/com/dua3/utility/text/TextUtilTest.java b/utility/src/test/java/com/dua3/utility/text/TextUtilTest.java index 744bd972..8d6c9909 100644 --- a/utility/src/test/java/com/dua3/utility/text/TextUtilTest.java +++ b/utility/src/test/java/com/dua3/utility/text/TextUtilTest.java @@ -6,6 +6,7 @@ package com.dua3.utility.text; import com.dua3.utility.data.Pair; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.List; @@ -86,4 +87,28 @@ void testEscapeHTML() { escapedString = TextUtil.escapeHTML(specialCharactersString); assertEquals("< > & " ' /", escapedString); } + + @Test + void testDecodeFontSize() { + // Test with "pt" + Assertions.assertEquals(10.0f, TextUtil.decodeFontSize("10pt"), 0.001); + + // Test with "em" + Assertions.assertEquals(120.0f, TextUtil.decodeFontSize("10em"), 0.001); + + // Test with "px" + Assertions.assertEquals(7.5f, TextUtil.decodeFontSize("10px"), 0.001); + + // Test with "%" + Assertions.assertEquals(1.2f, TextUtil.decodeFontSize("10%"), 0.001); + + // Test with unknown unit + Assertions.assertThrows(IllegalArgumentException.class, () -> TextUtil.decodeFontSize("10abc")); + + // Test with "vw" + Assertions.assertEquals(120.0f, TextUtil.decodeFontSize("10vw"), 0.001); + + // Test with empty string + Assertions.assertThrows(IllegalArgumentException.class, () -> TextUtil.decodeFontSize("")); + } }