commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1174153 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
Date Thu, 22 Sep 2011 14:13:10 GMT
Author: erans
Date: Thu Sep 22 14:13:09 2011
New Revision: 1174153

URL: http://svn.apache.org/viewvc?rev=1174153&view=rev
Log:
Using labeled loop to avoid code duplication.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java?rev=1174153&r1=1174152&r2=1174153&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java Thu
Sep 22 14:13:09 2011
@@ -2062,39 +2062,30 @@ public final class MathUtils {
                                      boolean strict, boolean abort) {
         double previous = val[0];
         final int max = val.length;
-        for (int i = 1; i < max; i++) {
+
+        int index;
+        ITEM:
+        for (index = 1; index < max; index++) {
             switch (dir) {
             case INCREASING:
                 if (strict) {
-                    if (val[i] <= previous) {
-                        if (abort) {
-                            throw new NonMonotonousSequenceException(val[i], previous, i,
dir, strict);
-                        }
-                        return false;
+                    if (val[index] <= previous) {
+                        break ITEM;
                     }
                 } else {
-                    if (val[i] < previous) {
-                        if (abort) {
-                            throw new NonMonotonousSequenceException(val[i], previous, i,
dir, strict);
-                        }
-                        return false;
+                    if (val[index] < previous) {
+                        break ITEM;
                     }
                 }
                 break;
             case DECREASING:
                 if (strict) {
-                    if (val[i] >= previous) {
-                        if (abort) {
-                            throw new NonMonotonousSequenceException(val[i], previous, i,
dir, strict);
-                        }
-                        return false;
+                    if (val[index] >= previous) {
+                        break ITEM;
                     }
                 } else {
-                    if (val[i] > previous) {
-                        if (abort) {
-                            throw new NonMonotonousSequenceException(val[i], previous, i,
dir, strict);
-                        }
-                        return false;
+                    if (val[index] > previous) {
+                        break ITEM;
                     }
                 }
                 break;
@@ -2102,9 +2093,20 @@ public final class MathUtils {
                 // Should never happen.
                 throw new IllegalArgumentException();
             }
-            previous = val[i];
+            previous = val[index];
+        }
+
+        if (index == max) {
+            // Loop completed.
+            return true;
+        }
+
+        // Loop early exit means wrong ordering.
+        if (abort) {
+            throw new NonMonotonousSequenceException(val[index], previous, index, dir, strict);
+        } else {
+            return false;
         }
-        return true;
     }
 
     /**



Mime
View raw message