You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2017/10/05 22:06:36 UTC

svn commit: r1811265 - in /pivot/trunk/wtk: src/org/apache/pivot/wtk/ test/org/apache/pivot/wtk/test/

Author: rwhitcomb
Date: Thu Oct  5 22:06:36 2017
New Revision: 1811265

URL: http://svn.apache.org/viewvc?rev=1811265&view=rev
Log:
Implement new allowed formats for Bounds, CornerRadii, Limits and Span
along with tests.
The new formats are basically simple lists of integers separated by commas
or semicolons.  Same order for the list as the JSON list formats.
Some other formats are accepted in some cases (as in "start-end" for Limits).
These cases basically match the format presented in the "toString()" methods
for each class.
Tweak some "toString()" formats (use "getSimpleName" and add/remove some spaces).

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/CornerRadii.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Limits.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Span.java
    pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java
    pivot/trunk/wtk/test/org/apache/pivot/wtk/test/CornerRadiiTest.java
    pivot/trunk/wtk/test/org/apache/pivot/wtk/test/LimitsTest.java
    pivot/trunk/wtk/test/org/apache/pivot/wtk/test/SpanTest.java

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java?rev=1811265&r1=1811264&r2=1811265&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java Thu Oct  5 22:06:36 2017
@@ -405,6 +405,8 @@ public final class Bounds implements Ser
      * <pre>{ "x": nnn, "y": nnn, "width": nnn, "height": nnn }</pre>
      * <p> The format of a JSON list format will be:
      * <pre>[ x, y, width, height ]</pre>
+     * <p> Also accepted is a simple list (comma- or semi-colon-separated) of four
+     * integer values.
      *
      * @param boundsValue The JSON string containing the map or list of bounds values
      * (must not be {@code null}).
@@ -433,7 +435,17 @@ public final class Bounds implements Ser
                 throw new IllegalArgumentException(exception);
             }
         } else {
-            throw new IllegalArgumentException("Invalid format for Bounds.");
+            String[] parts = boundsValue.split("\\s*[,;]\\s*");
+            if (parts.length != 4) {
+                throw new IllegalArgumentException("Invalid format for Bounds: " + boundsValue);
+            }
+            try {
+                bounds = new Bounds(
+                    Integer.parseInt(parts[0]), Integer.parseInt(parts[1]),
+                    Integer.parseInt(parts[2]), Integer.parseInt(parts[3]));
+            } catch (NumberFormatException ex) {
+                throw new IllegalArgumentException(ex);
+            }
         }
 
         return bounds;

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/CornerRadii.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/CornerRadii.java?rev=1811265&r1=1811264&r2=1811265&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/CornerRadii.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/CornerRadii.java Thu Oct  5 22:06:36 2017
@@ -49,27 +49,26 @@ public final class CornerRadii implement
         this(radius, radius, radius, radius);
     }
 
+    public CornerRadii(Number radius) {
+        Utils.checkNull(radius, "radius");
+        int radii = radius.intValue();
+        Utils.checkNonNegative(radii, "radii");
+
+        this.topLeft = radii;
+        this.topRight = radii;
+        this.bottomLeft = radii;
+        this.bottomRight = radii;
+    }
+
     private void check(CornerRadii radii) {
         check(radii.topLeft, radii.topRight, radii.bottomLeft, radii.bottomRight);
     }
 
     private void check(int topLeft, int topRight, int bottomLeft, int bottomRight) {
-        if (topLeft < 0) {
-            throw new IllegalArgumentException("topLeft is negative.");
-        }
-
-        if (topRight < 0) {
-            throw new IllegalArgumentException("topRight is negative.");
-        }
-
-        if (bottomLeft < 0) {
-            throw new IllegalArgumentException("bottomLeft is negative.");
-        }
-
-        if (bottomRight < 0) {
-            throw new IllegalArgumentException("bottomRight is negative.");
-        }
-
+        Utils.checkNonNegative(topLeft, "topLeft");
+        Utils.checkNonNegative(topRight, "topRight");
+        Utils.checkNonNegative(bottomLeft, "bottomLeft");
+        Utils.checkNonNegative(bottomRight, "bottomRight");
     }
 
     public CornerRadii(CornerRadii cornerRadii) {
@@ -150,6 +149,9 @@ public final class CornerRadii implement
      * and construct using the first four values as top left, top right,
      * bottom left, and bottom right respectively, using the
      * {@link #CornerRadii(int, int, int, int)} constructor.
+     * <p> A form of 4 integers values separate by commas or semicolons
+     * is also accepted, as in "n, n; n, n", where the values are in the
+     * same order as the JSON list form.
      * <p> Otherwise the string should be a single integer value
      * that will be used to construct the radii using the {@link #CornerRadii(int)}
      * constructor.
@@ -162,7 +164,7 @@ public final class CornerRadii implement
      * as a JSON list.
      */
     public static CornerRadii decode(String value) {
-        Utils.checkNull(value, "value");
+        Utils.checkNullOrEmpty(value, "value");
 
         CornerRadii cornerRadii;
         if (value.startsWith("{")) {
@@ -180,7 +182,22 @@ public final class CornerRadii implement
                 throw new IllegalArgumentException(exception);
             }
         } else {
-            cornerRadii = new CornerRadii(Integer.parseInt(value));
+            String[] parts = value.split("\\s*[,;]\\s*");
+            if (parts.length == 4) {
+                try {
+                    cornerRadii = new CornerRadii(
+                        Integer.parseInt(parts[0]),
+                        Integer.parseInt(parts[1]),
+                        Integer.parseInt(parts[2]),
+                        Integer.parseInt(parts[3]));
+                } catch (NumberFormatException ex) {
+                    throw new IllegalArgumentException(ex);
+                }
+            } else if (parts.length == 1) {
+                cornerRadii = new CornerRadii(Integer.parseInt(value));
+            } else {
+                throw new IllegalArgumentException("Bad format for corner radii value: " + value);
+            }
         }
 
         return cornerRadii;

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Limits.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Limits.java?rev=1811265&r1=1811264&r2=1811265&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Limits.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Limits.java Thu Oct  5 22:06:36 2017
@@ -151,7 +151,7 @@ public final class Limits implements Ser
     public String toString() {
         StringBuilder buf = new StringBuilder();
 
-        buf.append(getClass().getName());
+        buf.append(getClass().getSimpleName());
         buf.append(" [");
 
         if (minimum == Integer.MIN_VALUE) {
@@ -179,6 +179,9 @@ public final class Limits implements Ser
      * <pre>{ "minimum": nnn, "maximum": nnn }</pre>
      * <p> The format of a JSON list format will be:
      * <pre>[ minimum, maximum ]</pre>
+     * <p> Also accepted is a simple comma- or semicolon-separated list of two
+     * integer values, as in: <pre>min, max</pre>, or as <pre>min-max</pre> (as
+     * in the format produced by {@link #toString}).
      *
      * @param value The JSON string containing the map or list of limits values
      * (must not be {@code null}).
@@ -207,7 +210,16 @@ public final class Limits implements Ser
                 throw new IllegalArgumentException(exception);
             }
         } else {
-            throw new IllegalArgumentException("Invalid format for Limits.");
+            String[] parts = value.split("\\s*[,;\\-]\\s*");
+            if (parts.length != 2) {
+                throw new IllegalArgumentException("Invalid format for Limits: " + value);
+            }
+            try {
+                limits = new Limits(
+                    Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
+            } catch (NumberFormatException ex) {
+                throw new IllegalArgumentException(ex);
+            }
         }
 
         return limits;

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Span.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Span.java?rev=1811265&r1=1811264&r2=1811265&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Span.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Span.java Thu Oct  5 22:06:36 2017
@@ -312,7 +312,7 @@ public final class Span {
 
     @Override
     public String toString() {
-        return getClass().getSimpleName() + " {start: " + start + ", end: " + end + "}";
+        return getClass().getSimpleName() + " {start:" + start + ", end:" + end + "}";
     }
 
     /**
@@ -322,6 +322,8 @@ public final class Span {
      * <p> If the string value is a JSON list, then parse the list
      * and construct using the first two values as start and end
      * respectively, using the {@link #Span(int, int)} constructor.
+     * <p> Also accepted is a simple list of two integer values
+     * separated by comma or semicolon.
      * <p> Otherwise the string should be a single integer value
      * that will be used to construct the span using the {@link #Span(int)}
      * constructor.
@@ -352,7 +354,18 @@ public final class Span {
                 throw new IllegalArgumentException(exception);
             }
         } else {
-            span = new Span(Integer.parseInt(value));
+            String[] parts = value.split("\\s*[,;]\\s*");
+            try {
+                if (parts.length == 2) {
+                    span = new Span(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
+                } else if (parts.length == 1) {
+                    span = new Span(Integer.parseInt(value));
+                } else {
+                    throw new IllegalArgumentException("Unknown format for Span: " + value);
+                }
+            } catch (NumberFormatException ex) {
+                throw new IllegalArgumentException(ex);
+            }
         }
 
         return span;

Modified: pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java?rev=1811265&r1=1811264&r2=1811265&view=diff
==============================================================================
--- pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java (original)
+++ pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java Thu Oct  5 22:06:36 2017
@@ -11,7 +11,7 @@
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the bndecific language governing permissions and
+ * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.pivot.wtk.test;
@@ -60,6 +60,9 @@ public class BoundsTest {
         Bounds bndN = new Bounds(0, 0, 8, 9);
         Bounds bndAll = bnd1.union(bnd0).union(bnd2).union(bnd3).union(bnd4);
 
+        Bounds bnd6 = Bounds.decode("2, 3;  4,  5");
+        Bounds bnd6a = new Bounds(2, 3, 4, 5);
+
         assertNotEquals(bnd_1, bnd0);
         assertNotEquals(bnd0, bnd1);
         assertEquals(bnd10a, bnd10b);
@@ -85,6 +88,9 @@ public class BoundsTest {
 
         assertTrue(bnd5.equals(bnd5a));
         assertEquals(bndN, bndAll);
+
+        assertEquals(bnd6, bnd6a);
+        assertEquals(bnd6a.toString(), "Bounds [2,3;4x5]");
     }
 
 }

Modified: pivot/trunk/wtk/test/org/apache/pivot/wtk/test/CornerRadiiTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/CornerRadiiTest.java?rev=1811265&r1=1811264&r2=1811265&view=diff
==============================================================================
--- pivot/trunk/wtk/test/org/apache/pivot/wtk/test/CornerRadiiTest.java (original)
+++ pivot/trunk/wtk/test/org/apache/pivot/wtk/test/CornerRadiiTest.java Thu Oct  5 22:06:36 2017
@@ -37,12 +37,16 @@ public class CornerRadiiTest {
         CornerRadii r2 = new CornerRadii(2, 2, 2, 2);
         CornerRadii r2a = new CornerRadii(2);
         CornerRadii r2b = CornerRadii.decode("{topLeft:2, bottomLeft:2, topRight:2, bottomRight:2}");
+        CornerRadii r3 = new CornerRadii(2, 3, 4, 5);
+        CornerRadii r3a = CornerRadii.decode("2, 3; 4, 5");
 
         assertEquals(r0, r0a);
         assertEquals(r1, r1a);
         assertNotEquals(r0, r1);
         assertEquals(r2, r2a);
         assertEquals(r2a, r2b);
+        assertEquals(r3, r3a);
+        assertEquals(r3a.toString(), "CornerRadii [2,3; 4,5]");
     }
 
 }

Modified: pivot/trunk/wtk/test/org/apache/pivot/wtk/test/LimitsTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/LimitsTest.java?rev=1811265&r1=1811264&r2=1811265&view=diff
==============================================================================
--- pivot/trunk/wtk/test/org/apache/pivot/wtk/test/LimitsTest.java (original)
+++ pivot/trunk/wtk/test/org/apache/pivot/wtk/test/LimitsTest.java Thu Oct  5 22:06:36 2017
@@ -44,6 +44,10 @@ public class LimitsTest {
         Limits lm5a = new Limits(3, 4);
         Limits lm5b = Limits.decode("[3, 4]");
         Limits lmN = new Limits(0, 4);
+        Limits lm6 = Limits.decode("5; 6");
+        Limits lm6a = new Limits(5, 6);
+        Limits lm7 = Limits.decode("9 - 10");
+        Limits lm7a = new Limits(9, 10);
 
         assertEquals(lm_1.range(), 2);
         assertEquals(lm0.range(), 1);
@@ -64,6 +68,11 @@ public class LimitsTest {
 
         assertEquals(lmN.constrain(5), 4);
         assertEquals(lmN.constrain(-2), 0);
+
+        assertEquals(lm6, lm6a);
+        assertEquals(lm6.toString(), "Limits [5-6]");
+        assertEquals(lm7, lm7a);
+        assertEquals(lm7.toString(), "Limits [9-10]");
     }
 
 }

Modified: pivot/trunk/wtk/test/org/apache/pivot/wtk/test/SpanTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/SpanTest.java?rev=1811265&r1=1811264&r2=1811265&view=diff
==============================================================================
--- pivot/trunk/wtk/test/org/apache/pivot/wtk/test/SpanTest.java (original)
+++ pivot/trunk/wtk/test/org/apache/pivot/wtk/test/SpanTest.java Thu Oct  5 22:06:36 2017
@@ -45,6 +45,8 @@ public class SpanTest {
         Span sp5b = new Span(4, 3);
         Span spN = new Span(0, 4);
         Span spAll = sp1.union(sp0).union(sp2).union(sp3).union(sp4);
+        Span sp6 = Span.decode("4, 6");
+        Span sp6a = new Span(4, 6);
 
         assertEquals(sp_1.getLength(), 2);
         assertEquals(sp0.getLength(), 1);
@@ -76,6 +78,10 @@ public class SpanTest {
 
         assertTrue(sp4.after(sp1));
         assertTrue(sp3a.before(sp4));
+
+        assertEquals(sp6, sp6a);
+        assertEquals(sp6.getLength(), 3);
+        assertEquals(sp6.toString(), "Span {start:4, end:6}");
     }
 
 }
\ No newline at end of file