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 2016/09/02 20:46:51 UTC

svn commit: r1759036 - in /pivot/trunk: core/src/org/apache/pivot/util/Utils.java wtk/src/org/apache/pivot/wtk/HyperlinkButton.java wtk/src/org/apache/pivot/wtk/MenuPopup.java

Author: rwhitcomb
Date: Fri Sep  2 20:46:50 2016
New Revision: 1759036

URL: http://svn.apache.org/viewvc?rev=1759036&view=rev
Log:
Code cleanup:  Add two more argument checking methods to Utils.java and use
them in a couple of places to test their utility.

Also, put some "TODO:" comments into HyperlinkButton where it might make
more sense to throw an exception early on rather than much, much later
failing silently.


Modified:
    pivot/trunk/core/src/org/apache/pivot/util/Utils.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/HyperlinkButton.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/MenuPopup.java

Modified: pivot/trunk/core/src/org/apache/pivot/util/Utils.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/Utils.java?rev=1759036&r1=1759035&r2=1759036&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/Utils.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/Utils.java Fri Sep  2 20:46:50 2016
@@ -52,7 +52,7 @@ public class Utils {
      */
     public static void checkNull(Object value, String description) {
         if (value == null) {
-            if (description == null || description.isEmpty()) {
+            if (isNullOrEmpty(description)) {
                 throw new IllegalArgumentException();
             } else {
                 throw new IllegalArgumentException(description + " must not be null.");
@@ -60,4 +60,55 @@ public class Utils {
         }
     }
 
+    /**
+     * Check if the input string is {@code null} or empty (or all whitespace).
+     *
+     * @param value The string to check.
+     * @return {@code true} if the input is {@code null} or empty, {@code false}
+     * otherwise.
+     */
+    public static boolean isNullOrEmpty(String value) {
+        if (value == null) {
+            return true;
+        }
+        return value.trim().isEmpty();
+    }
+
+    /**
+     * Check if the input value is {@code null} or if it is a string and is empty
+     * (or all whitespace).
+     *
+     * @param value The object to check.
+     * @return {@code true} if the input is {@code null} or an empty string,
+     * {@code false} otherwise (which would include a non-null object other
+     * than a string).
+     */
+    public static boolean isNullOrEmpty(Object value) {
+        if (value == null) {
+            return true;
+        }
+        return (value instanceof String) && ((String)value).trim().isEmpty();
+    }
+
+    /**
+     * Check if the input argument is {@code null} and if it is a string
+     * if it is empty, and throw an {@link IllegalArgumentException} if so,
+     * with an optional message derived from the given string.
+     *
+     * @param value The argument value to check for {@code null} or empty.
+     * @param description A description for the value used to
+     * construct a message like {@code "xxx must not be null or empty."}.
+     * Can be {@code null} or an empty string, in which case a plain
+     * {@link IllegalArgumentException} is thrown without any detail message.
+     * @throws IllegalArgumentException if the value is {@code null}.
+     */
+    public static void checkNullOrEmpty(Object value, String description) {
+        if (value == null || (value instanceof String && isNullOrEmpty((String)value))) {
+            if (isNullOrEmpty(description)) {
+                throw new IllegalArgumentException();
+            } else {
+                throw new IllegalArgumentException(description + " must not be null or empty.");
+            }
+        }
+    }
 }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/HyperlinkButton.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/HyperlinkButton.java?rev=1759036&r1=1759035&r2=1759036&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/HyperlinkButton.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/HyperlinkButton.java Fri Sep  2 20:46:50 2016
@@ -24,6 +24,7 @@ import java.net.URL;
 import java.net.URISyntaxException;
 
 import org.apache.pivot.beans.DefaultProperty;
+import org.apache.pivot.util.Utils;
 import org.apache.pivot.wtk.content.ButtonData;
 import org.apache.pivot.wtk.content.LinkButtonDataRenderer;
 
@@ -65,6 +66,9 @@ public class HyperlinkButton extends Lin
             try {
                 if (Desktop.isDesktopSupported()) {
                     Desktop desktop = Desktop.getDesktop();
+                    // TODO: Should there be an exception here instead of silent failure?
+                    // Maybe an exception at constructor time (basically the whole idea
+                    // is unsupported)
                     if (desktop.isSupported(Desktop.Action.BROWSE)) {
                         desktop.browse(uri);
                     }
@@ -160,8 +164,7 @@ public class HyperlinkButton extends Lin
         // If the user has already set the button data in BXML,
         // then don't set the data to this new URI string
         Object buttonData = getButtonData();
-        if (buttonData == null ||
-            ((buttonData instanceof String) && ((String)buttonData).isEmpty())) {
+        if (Utils.isNullOrEmpty(buttonData)) {
             String uriString = uri == null ? "" : uri.toString();
             setButtonData(uriString);
         }
@@ -183,12 +186,7 @@ public class HyperlinkButton extends Lin
     public void setUri(String uriString)
         throws URISyntaxException
     {
-        if (uriString == null) {
-            throw new IllegalArgumentException("URI string must not be null.");
-        }
-        if (uriString.isEmpty()) {
-            throw new IllegalArgumentException("URI string must not be empty.");
-        }
+        Utils.checkNullOrEmpty(uriString, "URI string");
         setUri(new URI(uriString));
     }
 
@@ -215,12 +213,7 @@ public class HyperlinkButton extends Lin
     public void setUrl(String urlString)
         throws MalformedURLException, URISyntaxException
     {
-        if (urlString == null) {
-            throw new IllegalArgumentException("URL string must not be null.");
-        }
-        if (urlString.isEmpty()) {
-            throw new IllegalArgumentException("URL string must not be empty.");
-        }
+        Utils.checkNullOrEmpty(urlString, "URL string");
         setUrl(new URL(urlString));
     }
 

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/MenuPopup.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/MenuPopup.java?rev=1759036&r1=1759035&r2=1759036&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/MenuPopup.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/MenuPopup.java Fri Sep  2 20:46:50 2016
@@ -18,6 +18,7 @@ package org.apache.pivot.wtk;
 
 import org.apache.pivot.beans.DefaultProperty;
 import org.apache.pivot.util.ListenerList;
+import org.apache.pivot.util.Utils;
 import org.apache.pivot.util.Vote;
 
 /**
@@ -102,25 +103,19 @@ public class MenuPopup extends Window {
     }
 
     public final void open(Display display, Point location) {
-        if (location == null) {
-            throw new IllegalArgumentException("location is null.");
-        }
+        Utils.checkNull(location, "location");
 
         open(display, null, location.x, location.y);
     }
 
     public final void open(Window owner, int x, int y) {
-        if (owner == null) {
-            throw new IllegalArgumentException();
-        }
+        Utils.checkNull(owner, "owner");
 
         open(owner.getDisplay(), owner, x, y);
     }
 
     public final void open(Window owner, Point location) {
-        if (location == null) {
-            throw new IllegalArgumentException("location is null.");
-        }
+        Utils.checkNull(location, "location");
 
         open(owner, location.x, location.y);
     }