commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scott Wells (JIRA)" <>
Subject [jira] Commented: (CONFIGURATION-342) DatabaseConfiguration.copy() loses list/array values
Date Thu, 23 Oct 2008 14:42:44 GMT


Scott Wells commented on CONFIGURATION-342:

Yeah, it's definitely a totally denormalized form of an otherwise normalized structure, especially
if you follow it to its logical end of storing entire HierarchicalConfiguration objects. 
On the other hand, if there were a good mechanism to externalize arbitrarily complex Configuration
instances in the simplest of storage formats, e.g., PropertiesConfiguration, I can only imagine
it would be incredibly useful.

I could see adding one more column to your current storage table for a parent key such that
you'd get proper hierarchical storage in a quite simple table, though.  Well, I guess technically
if order were important, as it is in lists, then you'd want key, parent key, and index in
parent, so a total of two extra columns.

Anyway, here's the rough version of flatten()/unflatten().  This is for a proof-of-concept,
so don't take this as final code, but it does allow me to successfully marshal/unmarshal multi-value

    protected static final String FLATTENED_LIST_ITEM_SUFFIX = ".flattenedListItem";

    public static Configuration flatten(Configuration configuration)
        BaseConfiguration flattenedConfiguration = new BaseConfiguration();

        for (Iterator it = configuration.getKeys(); it.hasNext();)
            String key = (String);
            Object value = configuration.getProperty(key);
            if (value instanceof Collection)
                Collection collection = (Collection) value;

                // Create a formatting string to ensure that numbers are zero-padded properly
                int numPlaces = (int) Math.log10(collection.size()) + 1;
                String formatString = "%0" + numPlaces + "d";

                // Add each item in the collection distinctly
                int i = 0;
                for (Object item : collection)
                    String index = String.format(formatString, i);
                    String flattendedListItemKey = key + FLATTENED_LIST_ITEM_SUFFIX + index;
                    flattenedConfiguration.setProperty(flattendedListItemKey, item);
                flattenedConfiguration.setProperty(key, value);

        return flattenedConfiguration;

    public static Configuration unflatten(Configuration configuration)
        BaseConfiguration unflattenedConfiguration = new BaseConfiguration();

        // Add to a list that can be sorted by keys first
        Map<String, Object> sortedByKeys = new TreeMap<String, Object>();
        for (Iterator it = configuration.getKeys(); it.hasNext();)
            String key = (String);
            Object value = configuration.getProperty(key);
            sortedByKeys.put(key, value);

        for (Map.Entry<String, Object> entry : sortedByKeys.entrySet())
            String key = entry.getKey();
            Object value = entry.getValue();

            if (key.contains(FLATTENED_LIST_ITEM_SUFFIX))
                int chopHere = key.indexOf(FLATTENED_LIST_ITEM_SUFFIX);
                String originalKey = key.substring(0, chopHere);
                unflattenedConfiguration.addProperty(originalKey, value);
                unflattenedConfiguration.setProperty(key, value);

> DatabaseConfiguration.copy() loses list/array values
> ----------------------------------------------------
>                 Key: CONFIGURATION-342
>                 URL:
>             Project: Commons Configuration
>          Issue Type: Bug
>          Components: Type conversion
>    Affects Versions: 1.5
>            Reporter: Scott Wells
> I've found a bug where adding a list property to a BaseConfiguration, then copying that
full BaseConfiguration to a DatabaseConfiguration, the list is lost and only the first element
is copied to the destination DatabaseConfiguration.  For example:
> BaseConfiguration bc = new BaseConfiguration();
> bc.addProperty("myList", Arrays.asList("1", "2", "3", "4");
> DatabaseConfiguration dc = new DatabaseConfiguration(...);
> dc.copy(bc);
> List list = dc.getList("myList");
> // At this point, you'll get a single element list containing only "1"

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message