You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Stephen Colebourne <sc...@btopenworld.com> on 2003/02/15 12:11:05 UTC

[collections] FixedOrderComparator, bug #16823

http://issues.apache.org/bugzilla/show_bug.cgi?id=16823

I believe that a FixedOrderComparator would be a useful addition to
[collections]. It seems to fill an obvious gap.

I would welcome an implementation in the correct package, with test cases
following the style of collections.

For your example code, I would suggest adding a List constructor to
complement the array one.

I believe on furhter thinking that the Map constructor and methods are
probably implementation detail. They should not be exposed at present.

Are you still able to submit this ;-) ?

Stephen

>From david@leppik.net:
Among the simple classes I use frequently is a Comparator which imposes a
fixed
and arbitrary order on a group of objects.  As with so many things, it's not
hard to coerce this behavior, but it's really convenient to have a class
which
does it for you.  It's also something that can be hard to live without once
you
have it, assuming the appropriate problem space.  (Then again, what isn't?)

I wrote such a class for my current employer (http://www.vocalabs.com) which
has
graciously given me permission to donate it and its unit tests to Commons,
as
well as spend work hours on this project.  It will need some cleaning up
(add
the Apache copyright, probably change the name, and tighten error handling),
which I am prepared to do.

If others consider this valuable, I'd like some advice on how to proceed to
best
integrate it into Commons.  But first some details.

The class is currently named MapOrderComparator, since it uses a map to
enforce
the underlying order.  I don't like the name because it obscures its
purpose.
I'm still fishing for a better name; FixedOrderComparator and/or
ArbitraryComparator don't get the point across, though.  A sample usage is
as
follows:

    String[] days = { "Monday", "Tuesday", "Wednesday", /* etc...*/ };

    /* Construct a Comparator which uses the order of the array */
    Comparator dayComparator = new MapOrderComparator(days);

    TreeMap stuffThatHappensDuringTheWeek = new TreeMap(dayComparator);

    myThingie.populate(stuffThatHappensDuringTheWeek);
    myViewer.displaySomeMappedData(stuffThatHappensDuringTheWeek);


I wrote the class to do arbitrary order comparisons, using a Map
implementation,
but included some general-purpose Map functionality, including a constructor
which takes a Map.  I've never populated the map values with anything other
than
Integers to keep track of the initial order.  So one way to proceed would be
to
hide the Map as an implementation detail.




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [collections] FixedOrderComparator, bug #16823

Posted by David Leppik <dl...@vocalabs.com>.
Stephen Colebourne wrote:
> http://issues.apache.org/bugzilla/show_bug.cgi?id=16823
> 
> I believe that a FixedOrderComparator would be a useful addition to
> [collections]. It seems to fill an obvious gap.

Since you've latched onto FixedOrderComparator as the name, I'll run 
with it.

> 
> I would welcome an implementation in the correct package, with test cases
> following the style of collections.
> 
> For your example code, I would suggest adding a List constructor to
> complement the array one.

Makes sense, and it's only 3 lines.

> 
> I believe on furhter thinking that the Map constructor and methods are
> probably implementation detail. They should not be exposed at present.
> 
> Are you still able to submit this ;-) ?
> 
> Stephen

I'll get to work on it just as soon as deadlines allow.

Thanks for your input!


David

> 
>>>From david@leppik.net:
> Among the simple classes I use frequently is a Comparator which imposes a
> fixed
> and arbitrary order on a group of objects.  As with so many things, it's not
> hard to coerce this behavior, but it's really convenient to have a class
> which
> does it for you.  It's also something that can be hard to live without once
> you
> have it, assuming the appropriate problem space.  (Then again, what isn't?)
> 
> I wrote such a class for my current employer (http://www.vocalabs.com) which
> has
> graciously given me permission to donate it and its unit tests to Commons,
> as
> well as spend work hours on this project.  It will need some cleaning up
> (add
> the Apache copyright, probably change the name, and tighten error handling),
> which I am prepared to do.
> 
> If others consider this valuable, I'd like some advice on how to proceed to
> best
> integrate it into Commons.  But first some details.
> 
> The class is currently named MapOrderComparator, since it uses a map to
> enforce
> the underlying order.  I don't like the name because it obscures its
> purpose.
> I'm still fishing for a better name; FixedOrderComparator and/or
> ArbitraryComparator don't get the point across, though.  A sample usage is
> as
> follows:
> 
>     String[] days = { "Monday", "Tuesday", "Wednesday", /* etc...*/ };
> 
>     /* Construct a Comparator which uses the order of the array */
>     Comparator dayComparator = new MapOrderComparator(days);
> 
>     TreeMap stuffThatHappensDuringTheWeek = new TreeMap(dayComparator);
> 
>     myThingie.populate(stuffThatHappensDuringTheWeek);
>     myViewer.displaySomeMappedData(stuffThatHappensDuringTheWeek);
> 
> 
> I wrote the class to do arbitrary order comparisons, using a Map
> implementation,
> but included some general-purpose Map functionality, including a constructor
> which takes a Map.  I've never populated the map values with anything other
> than
> Integers to keep track of the initial order.  So one way to proceed would be
> to
> hide the Map as an implementation detail.
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org



-- 
David Leppik
Senior Software Engineer
Vocal Laboratories Inc.
dleppik@vocalabs.com


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org