You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by jw...@apache.org on 2010/03/16 22:56:43 UTC

svn commit: r924017 - /myfaces/trinidad/branches/1.2.12.1.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java

Author: jwaldman
Date: Tue Mar 16 21:56:43 2010
New Revision: 924017

URL: http://svn.apache.org/viewvc?rev=924017&view=rev
Log:
TRINIDAD-1702 decrease memory of FileSystemStyleCache by reusing CSSStyle objects
for branch 1.2.12.1.2-branch

Modified:
    myfaces/trinidad/branches/1.2.12.1.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java

Modified: myfaces/trinidad/branches/1.2.12.1.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.12.1.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java?rev=924017&r1=924016&r2=924017&view=diff
==============================================================================
--- myfaces/trinidad/branches/1.2.12.1.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java (original)
+++ myfaces/trinidad/branches/1.2.12.1.2-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/style/cache/FileSystemStyleCache.java Tue Mar 16 21:56:43 2010
@@ -1514,8 +1514,66 @@ public class FileSystemStyleCache implem
         if (name != null && value != null)
           styleProperties.put(name, value);
       }
+      
+      // To save memory, we reuse CSSStyle objects if 
+      // they have the same list of style property names and values.
+      // StyleKey is the key into the StyleKey, CSSStyle map.
+      StyleKey key = new StyleKey(styleProperties);
+      Style cachedStyle = _styleNodeToStyleMap.get(key);
+      if (cachedStyle == null)
+      {
+        // no match is cached yet, so create a new CSSStyle and cache in the map.
+        Style style = new CSSStyle(styleProperties);
+        _styleNodeToStyleMap.put(key, style);
+        return style;         
+      }
+      else
+      {
+        return cachedStyle;
+      }
+    }
+    
+    /**
+     * A StyleKey object is used as a key into a map so that we can share CSSStyle objects
+     * if they are equal and they have the same hashCode.
+     */
+    private static class StyleKey
+    {
+      public StyleKey(Map<String, String> styleProperties)
+      {
+        _styleProperties = styleProperties;
+      }
+      
+      @Override
+      public int hashCode()
+      {
+        int hash = 17;
+        // take each style property name and value and create a hashCode from it.
+        for (Map.Entry<String, String> e : _styleProperties.entrySet())
+        {
+          String name = e.getKey();
+          hash = 37*hash + ((null == name) ? 0 : name.hashCode());
+
+          String value = e.getValue();
+          hash = 37*hash + ((null == value) ? 0 : value.hashCode());
 
-      return new CSSStyle(styleProperties);
+        }
+        return hash;
+      }
+      @Override  
+      public boolean equals(Object obj)
+      {
+        if (this == obj)
+          return true;
+        if (!(obj instanceof StyleKey))
+          return false;
+          
+        // obj at this point must be a StyleKey
+        StyleKey test = (StyleKey)obj;
+        return test._styleProperties.equals(this._styleProperties);
+      }
+      
+      Map<String, String> _styleProperties;
 
     }
 
@@ -1524,6 +1582,7 @@ public class FileSystemStyleCache implem
     private final String[]             _namespacePrefixArray;
     private final Map<String, String>  _shortStyleClassMap;
     private final boolean              _compress;
+    private Map<StyleKey, Style> _styleNodeToStyleMap = new ConcurrentHashMap<StyleKey, Style>();
   }
 
   private class StyleWriterFactoryImpl
@@ -1553,7 +1612,7 @@ public class FileSystemStyleCache implem
       }
 
       File outputFile = _getOutputFile(_baseFilename, _files.size() + 1);
-      // We never want to do anything other then read it or delete it:
+      // We never want to do anything other than read it or delete it:
       outputFile.setReadOnly();
 
       _files.add(outputFile);
@@ -1628,7 +1687,6 @@ public class FileSystemStyleCache implem
    * names do not contain html, whereas our internal style selector
    * names may. We write out the shortened version of the mapped
    * selector names to the css file.
-   * jmw.
    * @todo Need to find a better spot for this, like the skin?
    */
   private static final Map<String, String> _STYLE_KEY_MAP;