On Mar 23, 2008, at 10:50 AM, Jerome Louvel wrote:

Paul, the Series class address the lack of a structure maintaining a list of named entries. There is no reusable Parameter class (name, value pair) in the JDK. Only the Map.Entry<K,V> interface comes close to it. Having just a List<Parameter> (via ArrayList or similar) is good but doesn't provide any
facility to lookup the structure by parameter name (like the
getValuesArray(name) method you were looking for). This is what the Series class adds in addition of being a List<Parameter>. I hope that makes sense.


You can use List<Parameter> if you write more generic utility classes:

        public interface CollectionFilter<T> {
            boolean accept( T obj );
        }

        public final class CollectionUtil {

            public static <T> Collection<T> filter( Collection<T> in,
                                                    Collection<T> out,
CollectionFilter<T> filter ) {
                for ( T x : in )
                    if ( filter.accept( x ) )
                        out.add( x );
                return out;
            }
        }

Then you just implement CollectionFilter:

        public ParameterNameFilter implements CollectionFilter<Parameter> {
            public ParameterNameFilter( String name ) {
                m_name = name;
            }
            public boolean accept( Parameter p ) {
                return p.getName().equals( m_name );
            }
            private final String m_name;
        }

Finally:

        List<Parameter> fooParams = CollectionUtil.filter(
            in, new LinkedList<Parameter>, new ParameterNameFilter( "foo" )
        );

The bonuses are:

1. The "Series" class doesn't "leak" into places that shouldn't have a dependency on it since you're using an ordinary List.

2. The solution above is completely generic and can be used for lots of other stuff.

If you want, you can add other "Util" methods that are convenience methods to lessen typing.

- Paul

Reply via email to