commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Sterijevski <gsterijev...@gmail.com>
Subject [Math] MathUtils.checkOrder
Date Wed, 21 Sep 2011 22:05:47 GMT
Hello All,

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?

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;
    }



Thoughts?

Thanks!

-Greg

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