- Timestamp:
- 2021-07-18T23:40:47+02:00 (3 years ago)
- Location:
- trunk/src
- Files:
-
- 1 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Utils.java
r18014 r18070 16 16 import java.io.InputStream; 17 17 import java.io.UnsupportedEncodingException; 18 import java.lang.reflect.Method; 18 19 import java.net.MalformedURLException; 19 20 import java.net.URI; … … 69 70 import org.openstreetmap.josm.spi.preferences.Config; 70 71 71 import com.kitfox.svg.xml.XMLParseUtil;72 73 72 /** 74 73 * Basic utils, that can be useful in different parts of the program. … … 98 97 private static final double TO_DEGREES = 180.0 / Math.PI; 99 98 private static final double TO_RADIANS = Math.PI / 180.0; 99 100 /** 101 * A reference to {@code Map.ofEntries()} available since Java 9 102 */ 103 static final Method mapOfEntries = mapOfEntriesMethod(); 104 105 private static Method mapOfEntriesMethod() { 106 try { 107 return Map.class.getMethod("ofEntries", Map.Entry[].class); 108 } catch (NoSuchMethodException e) { 109 return null; 110 } 111 } 100 112 101 113 private Utils() { … … 659 671 */ 660 672 public static <K, V> Map<K, V> toUnmodifiableMap(Map<K, V> map) { 661 return XMLParseUtil.toUnmodifiableMap(map); 673 if (map == null || map.isEmpty()) { 674 return Collections.emptyMap(); 675 } else if (map.size() == 1) { 676 final Map.Entry<K, V> entry = map.entrySet().iterator().next(); 677 return Collections.singletonMap(entry.getKey(), entry.getValue()); 678 } else if (mapOfEntries != null) { 679 try { 680 // Java 9: use Map.ofEntries(...) 681 return (Map<K, V>) mapOfEntries.invoke(null, new Object[]{map.entrySet().toArray(new Map.Entry[0])}); 682 } catch (Exception ignore) { 683 Logging.trace(ignore); 684 } 685 } 686 return Collections.unmodifiableMap(map); 662 687 } 663 688
Note:
See TracChangeset
for help on using the changeset viewer.