felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject svn commit: r1788773 - /felix/trunk/framework/src/main/java/org/apache/felix/framework/util/ImmutableMap.java
Date Sun, 26 Mar 2017 19:52:39 GMT
Author: pauls
Date: Sun Mar 26 19:52:39 2017
New Revision: 1788773

URL: http://svn.apache.org/viewvc?rev=1788773&view=rev
Log:
Make the empty ImmutableMap a singleton to improve memory usage (This closes #97).

Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/util/ImmutableMap.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/util/ImmutableMap.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/util/ImmutableMap.java?rev=1788773&r1=1788772&r2=1788773&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/util/ImmutableMap.java
(original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/util/ImmutableMap.java
Sun Mar 26 19:52:39 2017
@@ -20,19 +20,25 @@ package org.apache.felix.framework.util;
 
 import java.util.AbstractMap;
 import java.util.AbstractSet;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
 
 public class ImmutableMap<K, V> extends AbstractMap<K, V>
 {
+    @SuppressWarnings({ "rawtypes" })
+    private static final ImmutableMap EMPTY_MAP = new ImmutableMap();
+
     final Entry<K, V>[] entries;
 
+    @SuppressWarnings("unchecked")
     public static <K, V> ImmutableMap<K, V> newInstance(Entry<K, V>...
entries)
     {
-        return new ImmutableMap<K, V>(entries);
+        return entries.length == 0 ? EMPTY_MAP : new ImmutableMap<K, V>(entries);
     }
 
+    @SuppressWarnings("unchecked")
     public static <K, V> ImmutableMap<K, V> newInstance(Map<K, V> entries)
     {
         if (entries instanceof ImmutableMap)
@@ -41,15 +47,22 @@ public class ImmutableMap<K, V> extends
         }
         else
         {
-            return new ImmutableMap<K, V>(entries);
+            return entries.isEmpty() ? EMPTY_MAP : new ImmutableMap<K, V>(entries);
         }
     }
 
+    @SuppressWarnings("unchecked")
+    private ImmutableMap()
+    {
+        this.entries = new Entry[0];
+    }
+
     protected ImmutableMap(Entry<K, V>[] entries)
     {
         this.entries = entries.clone();
     }
 
+    @SuppressWarnings("unchecked")
     protected ImmutableMap(Map<K, V> map)
     {
         this.entries = map.entrySet().toArray(new Entry[map.size()]);



Mime
View raw message