package org.xillium.data;

import java.util.*;


/**
 * A data binder
 */
public class DataBinder extends HashMap<String, String> {
    private final Map<String, CachedResultSet> _rsets = new HashMap<String, CachedResultSet>();
    private final Map<String, Object> _named = new HashMap<String, Object>();

    /**
     * Puts a new result set into this binder.
     */
    public CachedResultSet putResultSet(String name, CachedResultSet rs) {
        return _rsets.put(name, rs);
    }

    /**
     * Retrieves a result set from this binder.
     */
    public CachedResultSet getResultSet(String name) {
        return _rsets.get(name);
    }

    /**
     * Retrieves the set of all result set names.
     */
    public Set<String> getResultSetNames() {
        return _rsets.keySet();
    }

    /**
     * Puts a named object into this binder.
     */
    public Object putNamedObject(String name, Object object) {
        return _named.put(name, object);
    }

    /**
     * Retrieves a named object from this binder.
     */
    public Object getNamedObject(String name) {
        return _named.get(name);
    }

    /**
     * Removes all auto-values (those whose name starts and ends with '#').
     *
     * @return the number of auto-values removed
     */
    public int clearAutoValues() {
        int count = 0;
        for (String key: keySet()) {
            if (key.charAt(0) == '#' && key.charAt(key.length()-1) == '#') {
                remove(key);
                ++count;
            }
        }
        return count;
    }

    public int estimateMaximumBytes() {
        int count = this.size();
        for (String key: _rsets.keySet()) {
            CachedResultSet crs = _rsets.get(key);
            if (crs.rows != null) {
                count += crs.columns.length*crs.rows.size();
            }
        }
        return count * 64;
    }
}

@@ -1389,36 +1389,149 @@
     
-for (String key: keySet()) %7B
+Iterator%3CMap.Entry%3CString, String%3E%3E it = entrySet().iterator();%0D%0A        while (it.hasNext()) %7B%0D%0A            String key = it.next().getKey();
 %0D%0A  
@@ -1626,18 +1626,18 @@
     
+it.
 remove(
-key
 );%0D%0A

use an Iterator to delete auto-values