package com.thoughtworks.xstream.core.util;

import java.beans.PropertyEditor;

/**
 * Wrapper around {@link PropertyEditor} that can
 * be called by multiple threads concurrently.
 * <p>A PropertyEditor is not thread safe. To make best 
 * use of resources, the PropertyEditor provides a dynamically
 * sizing pool of instances, each of which will only
 * be called by a single thread at a time.</p>
 * <p>The pool has a maximum capacity, to limit overhead.
 * If all instances in the pool are in use and another is
 * required, it shall block until one becomes available.</p>
 *
 * @author J&ouml;rg Schaible
 * @since upcoming
 */
public class ThreadSafePropertyEditor {

    private final Class editorType;
    private final Pool pool;

    public ThreadSafePropertyEditor(Class type, int initialPoolSize, int maxPoolSize) {
        if (!PropertyEditor.class.isAssignableFrom(type)) {
            throw new IllegalArgumentException(type.getName() + " is not a " + PropertyEditor.class.getName());
        }
        editorType = type;
        pool = new Pool(initialPoolSize, maxPoolSize, new Pool.Factory() {
            public Object newInstance() {
                try {
                    return editorType.newInstance();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            
        });
    }
    
    public String getAsText(Object object) {
        PropertyEditor editor = fetchFromPool();
        try {
            editor.setValue(object);
            return editor.getAsText();
        } finally {
            pool.putInPool(editor);
        }
    }

    public Object setAsText(String str) {
        PropertyEditor editor = fetchFromPool();
        try {
            editor.setAsText(str);
            return editor.getValue();
        } finally {
            pool.putInPool(editor);
        }
    }

    private PropertyEditor fetchFromPool() {
        PropertyEditor editor = (PropertyEditor)pool.fetchFromPool();
        return editor;
    }
}

@@ -38,16 +38,94 @@
 .util;%0A%0A
+import com.thoughtworks.xstream.converters.reflection.ObjectAccessException;%0A%0A
 import j
@@ -151,16 +151,17 @@
 ditor;%0A%0A
+%0A
 /**%0A * W
@@ -205,19 +205,16 @@
 that can
-%0A *
  be call
@@ -256,16 +256,20 @@
 .%0A * %3Cp%3E
+%0A * 
 A Proper
@@ -310,20 +310,16 @@
 ke best 
-%0A * 
 use of r
@@ -346,16 +346,19 @@
 tyEditor
+%0A *
  provide
@@ -372,19 +372,16 @@
 amically
-%0A *
  sizing 
@@ -422,19 +422,16 @@
 ill only
-%0A *
  be call
@@ -437,16 +437,19 @@
 led by a
+%0A *
  single 
@@ -465,16 +465,20 @@
  a time.
+%0A * 
 %3C/p%3E%0A * 
@@ -480,16 +480,20 @@
 %3E%0A * %3Cp%3E
+%0A * 
 The pool
@@ -535,19 +535,16 @@
 verhead.
-%0A *
  If all 
@@ -575,16 +575,19 @@
 e in use
+%0A *
  and ano
@@ -593,19 +593,16 @@
 other is
-%0A *
  require
@@ -647,23 +647,28 @@
 ailable.
+%0A * 
 %3C/p%3E%0A *
+ 
 %0A * @aut
@@ -1033,23 +1033,55 @@
 me()
- + %22 is not a %22
+%0A                + %22 is not a %22%0A               
  + P
@@ -1366,16 +1366,29 @@
  catch (
+Instantiation
 Exceptio
@@ -1428,25 +1428,317 @@
 new 
-RuntimeException(
+ObjectAccessException(%22Could not call default constructor of %22%0A                        + editorType.getName(), e);%0A                %7D catch (IllegalAccessException e) %7B%0A                    throw new ObjectAccessException(%22Could not call default constructor of %22%0A                        + editorType.getName(), 
 e);%0A
@@ -1769,28 +1769,16 @@
       %7D%0A
-            
 %0A       
@@ -1788,20 +1788,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub

Use proper Exception and make JDK 1.3 compatible.