Hi,

I'm new to the collections mailing list and I have a class I think would be appropriate to be donated to the collections API.

This is an extension of the MultiHashMap and filters a given Collection by a given field. It will put the objects into the map using the field value as the key.

For example, lets say I have a class X which has an integer i. I have four instances of X each with the following names and values of i.

x1 i=1
x2 i=2
x3 i=2
x4 i=5

The resulting MultiHashMap will contain the following after a call to
sortCollection(X.class, "i"); has been made.

key | Objects
1   | x1
2   | x2, x3
5   | x4

I can then get a Collection of sorted objects by asking the map for the key value. I find this very useful in many situations I have come across. I will of course make the required doc, package and src formatting changes to the class before submitting it.

Please can you take a look and tell me if it is worth committing this to the Collections repository. What is the process I need to go through before committing? I'm a bit pressed for time at the mo' so I can't really spend too much time working on the Collections API as a regular developer. I am a big fan of the commons Collections API and commons project in general, just wish I had more time to get involved.

Kind regards,

John Hunsley.
Technical Supervisor, Cy-nap Ltd.
package util;

import org.apache.commons.collections.MultiHashMap;
import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;


/**
 * A given <class>Collection</class> contains objects with a know <class>Field</class>.
 * The value of the <class>Field</class> is unknowm and the collection must be filtered
 * by that unknown value. The given Collection is filtered and the objects within that Collection
 * sorted into collections of matching Field value. These collections are put into a table, where
 * the Field value becomes the key to that Collection. There is no gaurentee to the order in which
 * the collections are put into the Hashtable.
 * 
 * @author John Hunsley
 * Date: 18-May-2006
 * Time: 19:53:13
 */
public class CollectionFilter extends MultiHashMap {
    private Collection sortable;
    private Logger logger = Logger.getLogger(CollectionFilter.class);

    /**
     * Sort the sortable <interface>Collection</interface> by the given declared <class>Field</class>
     * as a <class>String</class>. First find if the field exists in the given class, if not a
     * IllegalAccessException is thrown.
     * @param clazz
     * @param field
     * @throws IllegalAccessException
     */
    public void sortCollection(Class clazz, String field) throws IllegalAccessException, NoSuchFieldException {
        Field declaredField = clazz.getDeclaredField(field);
        sortCollection(clazz, declaredField);
    }

    /**
     * Sort the sortable <interface>Collection</interface> by the given declared <class>Field</class>
     * @param clazz
     * @param field
     * @throws IllegalAccessException
     */
    public void sortCollection(Class clazz, Field field) throws IllegalAccessException {
        if(sortable != null) {

             synchronized(sortable) {
                Iterator itr = sortable.iterator();

                while(itr.hasNext()) {
                    Object obj = itr.next();
                    //if the object doesnt have the field skip it
                    if(!clazz.isInstance(obj)) continue;

                    try {

                        if(obj.getClass().getDeclaredField(field.getName()) != null) {
                            if(!field.isAccessible()) field.setAccessible(true);
                            super.put(field.get(obj), obj);
                        }

                    } catch (NoSuchFieldException e) {
                        if(logger.isDebugEnabled())
                            logger.debug("Collection contains an object which doesn't have the given Field");
                    }
                }
             }
        } else throw new IllegalAccessException("Collection to sort must not be null");
    }

    /**
     *
     * @param sortable
     */
    public CollectionFilter(final Collection sortable) {
        this.sortable =  Collections.synchronizedList(new ArrayList());
        this.sortable.addAll(sortable);
    }

    /**
     *
     * @return collection
     */
    public Collection getSortable() {
        return sortable;
    }

    /**
     *
     * @param sortable
     */
    public void setSortable(Collection sortable) {
        this.sortable = sortable;
    }

    /**
     *
     * @param object
     * @return super.getCollection();
     */
    public Collection getCollection(Object object) {
        return super.getCollection(object);
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to