Changeset 15912 in josm


Ignore:
Timestamp:
2020-02-23T13:40:09+01:00 (4 years ago)
Author:
simon04
Message:

see #18749 - Use efficient/unmodifiable maps for svgSalamander

Location:
trunk/src/com/kitfox/svg
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/com/kitfox/svg/SVGElement.java

    r15904 r15912  
    4747import java.util.LinkedList;
    4848import java.util.List;
     49import java.util.Map;
    4950import java.util.Set;
    5051import java.util.regex.Matcher;
     
    8990     * Styles defined for this elemnt via the <b>style</b> attribute.
    9091     */
    91     protected final HashMap<String, String> inlineStyles = new HashMap<>();
     92    private Map<String, String> inlineStyles = Collections.emptyMap();
    9293    /**
    9394     * Presentation attributes set for this element. Ie, any attribute other
    9495     * than the <b>style</b> attribute.
    9596     */
    96     protected final HashMap<String, String> presAttributes = new HashMap<>();
     97    private Map<String, String> presAttributes = Collections.emptyMap();
    9798    /**
    9899     * A list of presentation attributes to not include in the presentation
     
    273274        if (style != null)
    274275        {
    275             XMLParseUtil.parseStyle(style, inlineStyles);
     276            inlineStyles = XMLParseUtil.parseStyle(style);
    276277        }
    277278
     
    299300            String value = attrs.getValue(i);
    300301
     302            if (i == 0)
     303            {
     304                presAttributes = new HashMap<>();
     305            }
    301306            presAttributes.put(name, value == null ? null : value.intern());
    302307        }
     308        presAttributes = XMLParseUtil.toUnmodifiableMap(presAttributes);
    303309    }
    304310
  • trunk/src/com/kitfox/svg/xml/XMLParseUtil.java

    r15904 r15912  
    3838
    3939import java.awt.Toolkit;
     40import java.lang.reflect.Method;
     41import java.util.Collections;
    4042import java.util.HashMap;
    4143import java.util.Iterator;
    4244import java.util.LinkedList;
     45import java.util.Map;
    4346import java.util.logging.Level;
    4447import java.util.logging.Logger;
     
    5861    static final Matcher quoteMatch = Pattern.compile("^'|'$").matcher("");
    5962
     63    /**
     64     * A reference to {@link Map#ofEntries(Map.Entry[])} available since Java 9
     65     */
     66    static final Method mapOfEntries = mapOfEntriesMethod();
     67
     68    private static Method mapOfEntriesMethod() {
     69        try {
     70            return Map.class.getMethod("ofEntries", Map.Entry[].class);
     71        } catch (NoSuchMethodException e) {
     72            return null;
     73        }
     74    }
     75
    6076    /** Creates a new instance of XMLParseUtil */
    6177    private XMLParseUtil()
     
    300316     * @param styleString - A CSS formatted string of styles.  Eg,
    301317     *     "font-size:12;fill:#d32c27;fill-rule:evenodd;stroke-width:1pt;"
    302      * @param map - A map to which these styles will be added
    303      */
    304     public static void parseStyle(String styleString, HashMap<String, String> map) {
     318     * @return the map with the added styles
     319     */
     320    public static Map<String, String> parseStyle(String styleString) {
     321        final Map<String, String> map = new HashMap<>();
    305322        final Pattern patSemi = Pattern.compile(";");
    306323
     
    315332                    map.put(key, value);
    316333                });
    317     }
     334        return toUnmodifiableMap(map);
     335    }
     336
     337    /**
     338     * Returns an unmodifiable map for the given map.
     339     * Makes use of {@link Collections#emptyMap()} and {@link Collections#singletonMap} and {@link Map#ofEntries(Map.Entry[])} to save memory.
     340     *
     341     * @param map the map for which an unmodifiable map is to be returned
     342     * @param <K> the type of keys maintained by this map
     343     * @param <V> the type of mapped values
     344     * @return an unmodifiable map
     345     * @see <a href="https://dzone.com/articles/preventing-your-java-collections-from-wasting-memo">
     346     * How to Prevent Your Java Collections From Wasting Memory</a>
     347     */
     348    @SuppressWarnings("unchecked")
     349    public static <K, V> Map<K, V> toUnmodifiableMap(Map<K, V> map) {
     350        if (map == null || map.isEmpty()) {
     351            return Collections.emptyMap();
     352        } else if (map.size() == 1) {
     353            final Map.Entry<K, V> entry = map.entrySet().iterator().next();
     354            return Collections.singletonMap(entry.getKey(), entry.getValue());
     355        } else if (mapOfEntries != null) {
     356            try {
     357                // Java 9: use Map.ofEntries(...)
     358                return (Map<K, V>) mapOfEntries.invoke(null, new Object[]{map.entrySet().toArray(new Map.Entry[0])});
     359            } catch (Exception ignore) {
     360            }
     361        }
     362        return Collections.unmodifiableMap(map);
     363    }
     364
    318365}
Note: See TracChangeset for help on using the changeset viewer.