/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.fo.properties;

import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

/**
 *  Thin wrapper around a HashMap to implement the property caching idiom
 *  in which a new Property instance is created then tested against cached
 *  instances created previously. If an existing property is found, this is
 *  retained and the newly created one is instantly eligible for garbage
 *  collection.
 */
public class PropertyCache {

    private Map propCache = Collections.synchronizedMap(new WeakHashMap());
    
    
    /**
     *  Checks if the given property is present in the cache - if so, returns
     *  a reference to the cached value. Otherwise the given object is added
     *  to the cache and returned.
     *  @param prop the Property instance to check for
     *  @return the cached instance
     */
    public Property fetch(Property prop) {
        
        Property cacheEntry = (Property) propCache.get(prop);
        if (cacheEntry != null) {
            return cacheEntry;
        } else {
            propCache.put(prop, prop);
            return prop;
        }
    }
}

@@ -850,16 +850,52 @@
 rties;%0A%0A
+import java.lang.ref.WeakReference;%0A
 import j
@@ -916,16 +916,16 @@
 ctions;%0A
-
 import j
@@ -1403,24 +1403,92 @@
 ;%0A    %0A    %0A
+    public int size() %7B%0A        return propCache.size();%0A    %7D%0A    %0A
     /**%0A    
@@ -1820,24 +1820,120 @@
  %7B%0A        %0A
+        WeakReference ref = (WeakReference) propCache.get(prop);%0A        if (ref != null) %7B%0A    
         Prop
@@ -1960,38 +1960,31 @@
 roperty)
- propCache.get(prop);%0A
+ref.get();%0A    
         
@@ -2017,24 +2017,28 @@
             
+ 
+   
 return cache
@@ -2047,30 +2047,49 @@
 try;
-%0A        %7D else %7B%0A    
+                %0A            %7D%0A        %7D%0A
     
@@ -2112,21 +2112,40 @@
 t(prop, 
+new WeakReference(
 prop)
+)
 ;%0A      
@@ -2142,28 +2142,24 @@
 ));%0A        
-   
- 
 return prop;
@@ -2159,26 +2159,16 @@
 n prop;%0A
-        %7D%0A
     %7D%0A%7D%0A

Fix possible memory leak in PropertyCache