commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Sterijevski <gsterijev...@gmail.com>
Subject Re: [Math] MathUtils.checkOrder
Date Wed, 21 Sep 2011 23:21:45 GMT
I would not want to remove the current implementation (the one with double[]
as an arg). However, I might want to check lists to make sure that they are
monotonically increasing. I want to avoid writing a checkOrder method for
int[], long[], float[],..., if it is possible. Also, one should be able to
check java.lang.Objects for monotonicity (given they implement Comparable).
Using comparable is (probably) not as efficient as comparing primitives, but
the upside is cleaner code.



On Wed, Sep 21, 2011 at 5:53 PM, Gilles Sadowski <
gilles@harfang.homelinux.org> wrote:

> Hi.
>
> >
> > In MathUtils there exists the method:
> >
> >     public static boolean checkOrder(double[] val, OrderDirection dir,
> >                                      boolean strict, boolean abort) {
> > ...code omitted...
> >     }
> >
> >
> > I would like to replace it with the method:
> >
> > public static boolean checkOrder(Comparable[] val, OrderDirection dir,
> >             boolean strict, boolean abort ){
> >         Comparable previous = val[0];
> >         boolean ok = true;
> >         int max = val.length;
> >         int comp;
> >         for (int i = 1; i < max; i++) {
> >             comp = val[i].compareTo(previous);
> >             switch (dir) {
> >             case INCREASING:
> >                 if (strict) {
> >                     if (0 <= comp) {
> >                         ok = false;
> >                     }
> >                 } else {
> >                     if ( comp < 0) {
> >                         ok = false;
> >                     }
> >                 }
> >                 break;
> >             case DECREASING:
> >                 if (strict) {
> >                     if (comp >= 0) {
> >                         ok = false;
> >                     }
> >                 } else {
> >                     if (comp > 0) {
> >                         ok = false;
> >                     }
> >                 }
> >                 break;
> >             default:
> >                 // Should never happen.
> >                 throw new IllegalArgumentException();
> >             }
> >
> >             if (!ok && abort) {
> >                 throw new NonMonotonousSequenceException(val[i],
> previous,
> > i, dir, strict);
> >             }
> >             previous = val[i];
> >         }
> >
> >         return ok;
> >     }
> >
> > Would this be acceptable to all?
>
> Probably not; we would not be able to call the new method with a "double[]"
> argument. Wherever such a call occurs, we would have to create a "Double[]"
> and copy over the contents, which seems a lot of operations just for the
> sake of a precondition test.
>
> Where do you want to use the new method?
> Anyway, if this method is needed within CM, the obvious possibility is to
> create it (and, unfortunately, almost duplicate the other one).
>
> >
> > I would also need to change NonMonotonousSequenceException. It would take
> > comparables in the constructor and look like:
> >
> >  public NonMonotonousSequenceException(Comparable wrong,
> >                                           Comparable previous,
> >                                           int index,
> >                                           MathUtils.OrderDirection
> > direction,
> >                                           boolean strict) {
> >         super(direction == MathUtils.OrderDirection.INCREASING ?
> >               (strict ?
> >                LocalizedFormats.NOT_STRICTLY_INCREASING_SEQUENCE :
> >                LocalizedFormats.NOT_INCREASING_SEQUENCE) :
> >               (strict ?
> >                LocalizedFormats.NOT_STRICTLY_DECREASING_SEQUENCE :
> >                LocalizedFormats.NOT_DECREASING_SEQUENCE),
> >                (Number) previous.compareTo(wrong),
> >                (Number)0, index, index - 1);
> >
> >         this.direction = direction;
> >         this.strict = strict;
> >         this.index = index;
> >         this.previous = null;
> >         this.previousComp = previous;
> >     }
> >
>
> Same here. Better create a new exception that will directly inherit from
> "MathIllegalArgumentException" (instead of "MathIllegalNumberException").
> [I also don't get what are the arguments.]
>
> >
> > [...]
>
>
> Regards,
> Gilles
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message