Skip to content

Commit

Permalink
fix qodana warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Nov 19, 2023
1 parent ee97170 commit 2c9ef76
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class SwingFontUtil implements FontUtil<java.awt.Font> {
private final WeakHashMap<Font, java.awt.Font> 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));
}

Expand Down
8 changes: 4 additions & 4 deletions utility/src/main/java/com/dua3/utility/text/RichText.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions utility/src/main/java/com/dua3/utility/text/TextUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions utility/src/test/java/com/dua3/utility/text/TextUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,4 +87,28 @@ void testEscapeHTML() {
escapedString = TextUtil.escapeHTML(specialCharactersString);
assertEquals("&lt; &gt; &amp; &quot; &apos; /", 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(""));
}
}

0 comments on commit 2c9ef76

Please sign in to comment.