Skip to content

Commit ddbe8e4

Browse files
committed
Improve FrameIconUtil code
Before icons would be loaded multiple times, as loadFrameIcons didn't cache the result. Now the icons are loaded lazily once.
1 parent 6857107 commit ddbe8e4

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/main/java/com/itextpdf/rups/view/icons/FrameIconUtil.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,49 @@ This file is part of the iText (R) project.
4646

4747
import java.awt.Image;
4848
import java.awt.Toolkit;
49-
import java.util.ArrayList;
5049
import java.util.List;
50+
import java.util.stream.Collectors;
51+
import java.util.stream.IntStream;
5152

52-
public class FrameIconUtil {
53-
53+
public final class FrameIconUtil {
54+
/**
55+
* Scale between two subsequent icon sizes.
56+
*/
57+
private static final int DIM_SCALE = 2;
58+
/**
59+
* Minimum width/height of an icon.
60+
*/
61+
private static final int MIN_DIM = 16;
62+
/**
63+
* Maximum width/height of an icon.
64+
*/
65+
private static final int MAX_DIM = 1024;
66+
/**
67+
* Path in resources to the logo.
68+
*/
5469
private static final String LOGO_ICON = "logo.png";
5570

5671
private FrameIconUtil() {
72+
// static class
5773
}
5874

5975
public static List<Image> loadFrameIcons() {
60-
List<Image> images = new ArrayList<>();
61-
final Image image = Toolkit.getDefaultToolkit().getImage(Rups.class.getResource(LOGO_ICON));
62-
// Add several scaled instances of the image to let the platform decide which ones to use
63-
for (int i = 16; i <= 1024; i *= 2) {
64-
images.add(image.getScaledInstance(i, i, Image.SCALE_SMOOTH));
76+
return LazyHolder.SCALED_ICONS;
77+
}
78+
79+
/**
80+
* Wrapper to lazily load the logo from resource and prepare all scaled
81+
* versions of it.
82+
*/
83+
private static final class LazyHolder {
84+
private static final List<Image> SCALED_ICONS;
85+
86+
static {
87+
final Image image = Toolkit.getDefaultToolkit()
88+
.getImage(Rups.class.getResource(LOGO_ICON));
89+
SCALED_ICONS = IntStream.iterate(MIN_DIM, dim -> dim <= MAX_DIM, dim -> dim * DIM_SCALE)
90+
.mapToObj(dim -> image.getScaledInstance(dim, dim, Image.SCALE_SMOOTH))
91+
.collect(Collectors.toUnmodifiableList());
6592
}
66-
return images;
6793
}
6894
}

0 commit comments

Comments
 (0)