This is an automated email from the ASF dual-hosted git repository.

ebakke pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git

commit 43e319710e9263390601ce35f7cc2c4d1abdb76b
Author: Eirik Bakke <eba...@ultorg.com>
AuthorDate: Fri Jun 7 10:53:04 2019 -0400

    Make ImageUtilities.loadImageIcon cache its result.
    
    This fixes the test failures I just introduced in ActionsTest. It also 
ensures
    that everything that was cached in ImageUtilities before still remains 
cached
    after the recent changes.
    
    Also use System.identityHashCode(image) rather than image.hashCode in
    ImageUtilities' internal cache key classes, to be consistent with using
    referential equality in equals() implementations.
---
 .../src/org/openide/util/ImageUtilities.java       | 15 +++++++++++----
 .../src/org/openide/util/ImageUtilitiesTest.java   | 22 +++++++++++++++++-----
 2 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/platform/openide.util.ui/src/org/openide/util/ImageUtilities.java 
b/platform/openide.util.ui/src/org/openide/util/ImageUtilities.java
index 5171bd9..4af5cc8 100644
--- a/platform/openide.util.ui/src/org/openide/util/ImageUtilities.java
+++ b/platform/openide.util.ui/src/org/openide/util/ImageUtilities.java
@@ -216,7 +216,7 @@ public final class ImageUtilities {
         if( image == null ) {
             return null;
         }
-        return IconImageIcon.create(image);
+        return image.asImageIcon();
     }
     
     private static boolean isDarkLaF() {
@@ -844,7 +844,7 @@ public final class ImageUtilities {
         @Override
         public int hashCode() {
             int hash = ((x << 3) ^ y) << 4;
-            hash = hash ^ baseImage.hashCode() ^ overlayImage.hashCode();
+            hash = hash ^ System.identityHashCode(baseImage) ^ 
System.identityHashCode(overlayImage);
 
             return hash;
         }
@@ -878,8 +878,7 @@ public final class ImageUtilities {
 
         @Override
         public int hashCode() {
-            int hash = image.hashCode() ^ str.hashCode();
-            return hash;
+            return System.identityHashCode(image) ^ str.hashCode();
         }
 
         @Override
@@ -943,6 +942,8 @@ public final class ImageUtilities {
         // May be null.
         final Icon delegateIcon;
         final URL url;
+        // May be null.
+        ImageIcon imageIconVersion;
 
         public static ToolTipImage createNew(String toolTipText, Image image, 
URL url) {
             ImageUtilities.ensureLoaded(image);
@@ -980,6 +981,12 @@ public final class ImageUtilities {
             this.url = url;
         }
 
+        public synchronized ImageIcon asImageIcon() {
+          if (imageIconVersion == null)
+            imageIconVersion = IconImageIcon.create(this);
+          return imageIconVersion;
+        }
+
         public ToolTipImage(Icon delegateIcon, String toolTipText, int 
imageType) {
             // BufferedImage must have width/height > 0.
             super(Math.max(1, delegateIcon.getIconWidth()),
diff --git 
a/platform/openide.util.ui/test/unit/src/org/openide/util/ImageUtilitiesTest.java
 
b/platform/openide.util.ui/test/unit/src/org/openide/util/ImageUtilitiesTest.java
index de8f537..be2eb29 100644
--- 
a/platform/openide.util.ui/test/unit/src/org/openide/util/ImageUtilitiesTest.java
+++ 
b/platform/openide.util.ui/test/unit/src/org/openide/util/ImageUtilitiesTest.java
@@ -242,6 +242,18 @@ public class ImageUtilitiesTest extends TestCase {
         assertEquals("Tool tip text should be empty, but it is " + str, 
expected, str);
     }
 
+    public void testLoadImageCached() {
+        Image image1 = 
ImageUtilities.loadImage("org/openide/util/testimage.png", false);
+        Image image2 = 
ImageUtilities.loadImage("org/openide/util/testimage.png", false);
+        assertSame("Expected same instance", image1, image2);
+    }
+
+    public void testLoadImageIconCached() {
+        ImageIcon icon1 = 
ImageUtilities.loadImageIcon("org/openide/util/testimage.png", false);
+        ImageIcon icon2 = 
ImageUtilities.loadImageIcon("org/openide/util/testimage.png", false);
+        assertSame("Expected same instance", icon1, icon2);
+    }
+
     public void testConversions() {
         /* Note: these are rather implementation-oriented tests. 
Implementation changes in
         ImageUtilities (addition or removal of caches etc.) might require this 
test to be
@@ -275,18 +287,18 @@ public class ImageUtilitiesTest extends TestCase {
 
             /* An Icon/Image loaded via loadImage can be freely passed through 
icon2Image/image2Icon
             without a new instance being created. */
-            assertEquals("Should be same instance",
+            assertSame("Should be same instance",
                     image,
                     icon2Image(imageIcon));
 
-            assertEquals("Should be same instance",
+            assertSame("Should be same instance",
                     icon2Image(imageIcon),
                     icon2Image(imageIcon));
 
             if (!useExternalImage) {
               /* In the useExternalImage case, the original instance will be 
converted to a
               ToolTipImage, so we won't have the same instance here. */
-              assertEquals("Should be same instance",
+              assertSame("Should be same instance",
                       image,
                       icon2Image(image2Icon(image)));
             }
@@ -298,12 +310,12 @@ public class ImageUtilitiesTest extends TestCase {
                     image2Icon(icon2Image(imageIcon)));
 
             Icon iconFromImage2Icon = image2Icon(image);
-            assertEquals("Should be same instance",
+            assertSame("Should be same instance",
                     iconFromImage2Icon,
                     image2Icon(icon2Image(iconFromImage2Icon)));
 
             Icon iconFromImageIconRoundabout = 
image2Icon(icon2Image(imageIcon));
-            assertEquals("Should be same instance",
+            assertSame("Should be same instance",
                     iconFromImageIconRoundabout,
                     image2Icon(icon2Image(iconFromImageIconRoundabout)));
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to