/*******************************************************************************
 * Copyright (c) 2015-2016 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.csstudio.display.builder.editor.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.csstudio.display.builder.model.WidgetDescriptor;
import org.csstudio.display.builder.model.WidgetFactory;

import javafx.scene.image.Image;

/** Cache of widget icons
 *  @author Kay Kasemir
 */
@SuppressWarnings("nls")
public class WidgetIcons
{
    private static final Logger logger = Logger.getLogger(WidgetIcons.class.getName());

    /** Cache of icon images by widget type */
    private static final Map<String, Image> icons = Collections.synchronizedMap(new HashMap<>());

    /** Get icon for widget type
     *  @param type Widget type
     *  @return Icon image, may be <code>null</code>
     */
    public static Image getIcon(final String type)
    {
        // Favoring no locks over locking for each access.
        // Keeping lock on 'icons' while in this method would prevent two threads
        // from concurrently requesting a missing icon, both adding it to the map.
        // -> Pity, but not fatal, and not observed in reality because 'Palette'
        //    requests all icons once on startup.
        Image icon = icons.get(type);
        if (icon == null)
        {
            final Optional<WidgetDescriptor> descriptor = WidgetFactory.getInstance().getWidgetDescriptor(type);
            try
            {
                if (descriptor.isPresent())
                {
                    logger.log(Level.FINE, "Obtaining icon for widget type " + type);
                    icon = new Image(descriptor.get().getIconStream());
                    icons.put(type, icon);
                }
                else
                    logger.log(Level.WARNING, "Unknown widget type " + type);
            }
            catch (Exception ex)
            {
                logger.log(Level.WARNING, "Cannot obtain widget for " + type, ex);
            }
        }
        return icon;
    }
}

@@ -537,27 +537,19 @@
 va.util.
-Collections
+Map
 ;%0D%0Aimpor
@@ -564,66 +564,64 @@
 til.
-HashMap;%0D%0Aimport java.util.Map;%0D%0Aimport java.util.Optional
+Optional;%0D%0Aimport java.util.concurrent.ConcurrentHashMap
 ;%0D%0Ai
@@ -1152,40 +1152,22 @@
 s = 
-Collections.synchronizedMap(new 
+new Concurrent
 Hash
@@ -1173,17 +1173,16 @@
 hMap%3C%3E()
-)
 ;%0D%0A%0D%0A   
@@ -1379,441 +1379,138 @@
     
-// Favoring no locks over locking for each access.%0D%0A        // Keeping lock on 'icons' while in this method would prevent two threads%0D%0A        // from concurrently requesting a missing icon, both adding it to the map.%0D%0A        // -%3E Pity, but not fatal, and not observed in reality because 'Palette'%0D%0A        //    requests all icons once on startup.%0D%0A        Image icon = icons.get(
+return icons.computeIfAbsent(type, WidgetIcons::loadIcon);%0D%0A    %7D%0D%0A%0D%0A    private static Image loadIcon(final String 
 type)
-;
 %0D%0A    
-    if (icon == null)%0D%0A        %7B%0D%0A    
+%7B%0D%0A
     
@@ -1623,20 +1623,16 @@
         
-    
 try%0D%0A   
@@ -1632,35 +1632,27 @@
 ry%0D%0A        
-  
-  
 %7B%0D%0A
-    
             
@@ -1688,35 +1688,27 @@
             
-    %7B%0D%0A    
+%7B%0D%0A
             
@@ -1798,18 +1798,14 @@
     
-    icon =
+return
  new
@@ -1863,63 +1863,11 @@
     
-        icons.put(type, icon);%0D%0A                %7D%0D%0A    
+%7D%0D%0A
     
@@ -1880,20 +1880,16 @@
   else%0D%0A
-    
         
@@ -1947,36 +1947,32 @@
 ype %22 + type);%0D%0A
-    
         %7D%0D%0A     
@@ -1974,20 +1974,16 @@
         
-    
 catch (E
@@ -2000,35 +2000,27 @@
 x)%0D%0A        
-    %7B%0D%0A    
+%7B%0D%0A
             
@@ -2083,39 +2083,24 @@
 type, ex);%0D%0A
-            %7D%0D%0A
         %7D%0D%0A 
@@ -2113,20 +2113,20 @@
  return 
-icon
+null
 ;%0D%0A    %7D

Use concurrent map