From a365b750d96a28154b4c14cf802a584772d80836 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Fri, 25 Feb 2022 21:49:15 +0100 Subject: [PATCH] core: minor code cleanup: - add final where possible - removed "public" from interface methods - simplified conditional expressions - removed unnecessary unboxing - removed unused assignements - removed redundant casts (used IntelliJ IDEA 2021.3 inspections) --- .../src/main/java/com/formdev/flatlaf/FlatLaf.java | 2 +- .../main/java/com/formdev/flatlaf/IntelliJTheme.java | 10 +++++----- .../java/com/formdev/flatlaf/LinuxFontPolicy.java | 4 ++-- .../java/com/formdev/flatlaf/UIDefaultsLoader.java | 12 ++++++------ .../java/com/formdev/flatlaf/ui/FlatComboBoxUI.java | 2 +- .../com/formdev/flatlaf/ui/FlatRadioButtonUI.java | 2 +- .../java/com/formdev/flatlaf/ui/FlatRootPaneUI.java | 6 ++---- .../java/com/formdev/flatlaf/ui/FlatSpinnerUI.java | 4 +--- .../com/formdev/flatlaf/ui/FlatTabbedPaneUI.java | 11 ++++------- .../java/com/formdev/flatlaf/ui/FlatTitlePane.java | 4 +--- .../com/formdev/flatlaf/ui/FlatToggleButtonUI.java | 6 +----- .../java/com/formdev/flatlaf/ui/FlatUIUtils.java | 2 +- .../com/formdev/flatlaf/ui/JBRCustomDecorations.java | 2 +- .../java/com/formdev/flatlaf/util/AnimatedIcon.java | 2 +- .../main/java/com/formdev/flatlaf/util/HSLColor.java | 4 ++-- .../java/com/formdev/flatlaf/util/HiDPIUtils.java | 2 +- .../com/formdev/flatlaf/util/JavaCompatibility.java | 2 +- 17 files changed, 32 insertions(+), 45 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java index 3570322e..2c00070f 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -605,7 +605,7 @@ public abstract class FlatLaf uiFont = ((ActiveFont)defaultFont).derive( baseFont, fontSize -> { return Math.round( fontSize * UIScale.computeFontScaleFactor( baseFont ) ); } ); - }; + } // increase font size if system property "flatlaf.uiScale" is set uiFont = UIScale.applyCustomScaleFactor( uiFont ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/IntelliJTheme.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/IntelliJTheme.java index 0d0491ae..a5b40196 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/IntelliJTheme.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/IntelliJTheme.java @@ -534,12 +534,12 @@ public class IntelliJTheme } /** Rename UI default keys (key --> value). */ - private static Map uiKeyMapping = new HashMap<>(); + private static final Map uiKeyMapping = new HashMap<>(); /** Copy UI default keys (value --> key). */ - private static Map uiKeyCopying = new HashMap<>(); - private static Map uiKeyInverseMapping = new HashMap<>(); - private static Map checkboxKeyMapping = new HashMap<>(); - private static Map checkboxDuplicateColors = new HashMap<>(); + private static final Map uiKeyCopying = new HashMap<>(); + private static final Map uiKeyInverseMapping = new HashMap<>(); + private static final Map checkboxKeyMapping = new HashMap<>(); + private static final Map checkboxDuplicateColors = new HashMap<>(); static { // ComboBox diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java index 66a39200..81b86516 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java @@ -170,7 +170,7 @@ class LinuxFontPolicy Object value = Toolkit.getDefaultToolkit().getDesktopProperty( "gnome.Xft/DPI" ); if( value instanceof Integer ) { - int dpi = ((Integer)value).intValue() / 1024; + int dpi = (Integer) value / 1024; if( dpi == -1 ) dpi = 96; if( dpi < 50 ) @@ -278,7 +278,7 @@ class LinuxFontPolicy // read config file ArrayList lines = new ArrayList<>( 200 ); try( BufferedReader reader = new BufferedReader( new FileReader( file ) ) ) { - String line = null; + String line; while( (line = reader.readLine()) != null ) lines.add( line ); } catch( IOException ex ) { diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java index b27d0b5f..dc47bba7 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/UIDefaultsLoader.java @@ -350,7 +350,7 @@ class UIDefaultsLoader enum ValueType { UNKNOWN, STRING, BOOLEAN, CHARACTER, INTEGER, INTEGERORFLOAT, FLOAT, BORDER, ICON, INSETS, DIMENSION, COLOR, FONT, SCALEDINTEGER, SCALEDFLOAT, SCALEDINSETS, SCALEDDIMENSION, INSTANCE, CLASS, GRAYFILTER, NULL, LAZY } - private static ValueType[] tempResultValueType = new ValueType[1]; + private static final ValueType[] tempResultValueType = new ValueType[1]; private static Map, ValueType> javaValueTypes; private static Map knownValueTypes; @@ -1209,7 +1209,7 @@ class UIDefaultsLoader } Integer integer = parseInteger( value, true ); - if( integer.intValue() < min || integer.intValue() > max ) + if( integer < min || integer > max ) throw new NumberFormatException( "integer '" + value + "' out of range (" + min + '-' + max + ')' ); return integer; } @@ -1250,28 +1250,28 @@ class UIDefaultsLoader private static ActiveValue parseScaledInteger( String value ) { int val = parseInteger( value, true ); - return (ActiveValue) t -> { + return t -> { return UIScale.scale( val ); }; } private static ActiveValue parseScaledFloat( String value ) { float val = parseFloat( value, true ); - return (ActiveValue) t -> { + return t -> { return UIScale.scale( val ); }; } private static ActiveValue parseScaledInsets( String value ) { Insets insets = parseInsets( value ); - return (ActiveValue) t -> { + return t -> { return UIScale.scale( insets ); }; } private static ActiveValue parseScaledDimension( String value ) { Dimension dimension = parseDimension( value ); - return (ActiveValue) t -> { + return t -> { return UIScale.scale( dimension ); }; } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java index 0b74b424..a05cd009 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java @@ -700,7 +700,7 @@ public class FlatComboBoxUI return true; Component editorComponent = comboBox.getEditor().getEditorComponent(); - return (editorComponent != null) ? FlatUIUtils.isPermanentFocusOwner( editorComponent ) : false; + return editorComponent != null && FlatUIUtils.isPermanentFocusOwner( editorComponent ); } else return FlatUIUtils.isPermanentFocusOwner( comboBox ); } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java index 9bb4110d..75aafe7b 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java @@ -199,7 +199,7 @@ public class FlatRadioButtonUI return infos; } - private static Insets tempInsets = new Insets( 0, 0, 0, 0 ); + private static final Insets tempInsets = new Insets( 0, 0, 0, 0 ); @Override public Dimension getPreferredSize( JComponent c ) { diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRootPaneUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRootPaneUI.java index bd21adde..eee1b3c7 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRootPaneUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRootPaneUI.java @@ -510,7 +510,7 @@ public class FlatRootPaneUI return; Container parent = c.getParent(); - boolean active = parent instanceof Window ? ((Window)parent).isActive() : false; + boolean active = parent instanceof Window && ((Window)parent).isActive(); g.setColor( FlatUIUtils.deriveColor( active ? activeBorderColor : inactiveBorderColor, baseBorderColor ) ); HiDPIUtils.paintAtScale1x( (Graphics2D) g, x, y, width, height, this::paintImpl ); @@ -522,9 +522,7 @@ public class FlatRootPaneUI protected boolean isWindowMaximized( Component c ) { Container parent = c.getParent(); - return parent instanceof Frame - ? (((Frame)parent).getExtendedState() & Frame.MAXIMIZED_BOTH) != 0 - : false; + return parent instanceof Frame && (((Frame)parent).getExtendedState() & Frame.MAXIMIZED_BOTH) != 0; } } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java index d8504ec6..0e4605e9 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java @@ -293,9 +293,7 @@ public class FlatSpinnerUI return true; JTextField textField = getEditorTextField( spinner.getEditor() ); - return (textField != null) - ? FlatUIUtils.isPermanentFocusOwner( textField ) - : false; + return textField != null && FlatUIUtils.isPermanentFocusOwner( textField ); } protected Color getBackground( boolean enabled ) { diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java index defa687f..813848c3 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java @@ -650,7 +650,7 @@ public class FlatTabbedPaneUI case "tabIconPlacement": value = parseTabIconPlacement( (String) value ); break; } } else { - Object oldValue = null; + Object oldValue; switch( key ) { // BasicTabbedPaneUI case "tabInsets": oldValue = tabInsets; tabInsets = (Insets) value; return oldValue; @@ -2412,7 +2412,7 @@ public class FlatTabbedPaneUI if( tabPane == null || tabViewport == null ) return; - if( !scrolled || tabViewport == null ) + if( !scrolled ) return; scrolled = false; @@ -2525,9 +2525,7 @@ public class FlatTabbedPaneUI setRolloverTab( tabIndex ); // check whether mouse hit tab close area - boolean hitClose = isTabClosable( tabIndex ) - ? getTabCloseHitArea( tabIndex ).contains( x, y ) - : false; + boolean hitClose = isTabClosable( tabIndex ) && getTabCloseHitArea( tabIndex ).contains( x, y ); if( e.getID() == MouseEvent.MOUSE_PRESSED ) pressedTabIndex = hitClose ? tabIndex : -1; setRolloverTabClose( hitClose ); @@ -2550,8 +2548,7 @@ public class FlatTabbedPaneUI if( tabIndex == lastTipTabIndex ) return; // closeTip already set - if( tabIndex != lastTipTabIndex ) - restoreTabToolTip(); + restoreTabToolTip(); lastTipTabIndex = tabIndex; lastTip = tabPane.getToolTipTextAt( lastTipTabIndex ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java index ced4110d..6acb5bce 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTitlePane.java @@ -883,9 +883,7 @@ debug*/ } protected boolean isWindowMaximized( Component c ) { - return window instanceof Frame - ? (((Frame)window).getExtendedState() & Frame.MAXIMIZED_BOTH) != 0 - : false; + return window instanceof Frame && (((Frame) window).getExtendedState() & Frame.MAXIMIZED_BOTH) != 0; } } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToggleButtonUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToggleButtonUI.java index f6f4b075..38122a70 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToggleButtonUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToggleButtonUI.java @@ -164,11 +164,7 @@ public class FlatToggleButtonUI @Override public Map> getStyleableInfos( JComponent c ) { Map> infos = super.getStyleableInfos( c ); - Iterator it = infos.keySet().iterator(); - while( it.hasNext() ) { - if( it.next().startsWith( "help." ) ) - it.remove(); - } + infos.keySet().removeIf( s -> s.startsWith( "help." ) ); return infos; } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java index 6daf5711..0d298a0d 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java @@ -71,7 +71,7 @@ public class FlatUIUtils public static final boolean MAC_USE_QUARTZ = Boolean.getBoolean( "apple.awt.graphics.UseQuartz" ); private static boolean useSharedUIs = true; - private static WeakHashMap> sharedUIinstances = new WeakHashMap<>(); + private static final WeakHashMap> sharedUIinstances = new WeakHashMap<>(); public static Rectangle addInsets( Rectangle r, Insets insets ) { return new Rectangle( diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/JBRCustomDecorations.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/JBRCustomDecorations.java index 8b2b4c11..38482637 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/JBRCustomDecorations.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/JBRCustomDecorations.java @@ -284,7 +284,7 @@ public class JBRCustomDecorations @Override public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) { Window window = SwingUtilities.windowForComponent( c ); - boolean active = (window != null) ? window.isActive() : false; + boolean active = window != null && window.isActive(); // paint top border // - in light themes diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/AnimatedIcon.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/AnimatedIcon.java index 7501172f..4f6c3c7b 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/AnimatedIcon.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/AnimatedIcon.java @@ -68,7 +68,7 @@ public interface AnimatedIcon extends Icon { @Override - public default void paintIcon( Component c, Graphics g, int x, int y ) { + default void paintIcon( Component c, Graphics g, int x, int y ) { AnimationSupport.paintIcon( this, c, g, x, y ); } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HSLColor.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HSLColor.java index 7c925907..deb4f915 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HSLColor.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HSLColor.java @@ -291,7 +291,7 @@ public class HSLColor // Calculate the Saturation - float s = 0; + float s; if (max == min) s = 0; @@ -386,7 +386,7 @@ public class HSLColor s /= 100f; l /= 100f; - float q = 0; + float q; if (l < 0.5) q = l * (1 + s); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HiDPIUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HiDPIUtils.java index 018bab96..07f3f7c0 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HiDPIUtils.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HiDPIUtils.java @@ -31,7 +31,7 @@ import com.formdev.flatlaf.FlatSystemProperties; public class HiDPIUtils { public interface Painter { - public void paint( Graphics2D g, int x, int y, int width, int height, double scaleFactor ); + void paint( Graphics2D g, int x, int y, int width, int height, double scaleFactor ); } public static void paintAtScale1x( Graphics2D g, JComponent c, Painter painter ) { diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/JavaCompatibility.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/JavaCompatibility.java index 1f94dc55..39887ecc 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/JavaCompatibility.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/JavaCompatibility.java @@ -89,7 +89,7 @@ public class JavaCompatibility getClippedStringMethod = cls.getMethod( SystemInfo.isJava_9_orLater ? "getClippedString" : "clipStringIfNecessary", - new Class[] { JComponent.class, FontMetrics.class, String.class, int.class } ); + JComponent.class, FontMetrics.class, String.class, int.class ); } catch( Exception ex ) { LoggingFacade.INSTANCE.logSevere( null, ex ); throw new RuntimeException( ex );