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/05/29 18:41:19 UTC

svn commit: r1796670 - in /pivot/trunk: core/src/org/apache/pivot/collections/Dictionary.java wtk/src/org/apache/pivot/wtk/Bounds.java

Author: rwhitcomb
Date: Mon May 29 18:41:19 2017
New Revision: 1796670

URL: http://svn.apache.org/viewvc?rev=1796670&view=rev
Log:
PIVOT-999: First update to take advantage of Java 8 features:
* Make a couple "default" methods in the Dictionary interface to
  get and set Integer values (which is a semi-common operation
  especially in the skin classes for styles).
* Use these methods in Bounds.

Also make some other changes / cleanup in Bounds (such as using
Utils.checkNull as appropriate).

Modified:
    pivot/trunk/core/src/org/apache/pivot/collections/Dictionary.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java

Modified: pivot/trunk/core/src/org/apache/pivot/collections/Dictionary.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/Dictionary.java?rev=1796670&r1=1796669&r2=1796670&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/collections/Dictionary.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/collections/Dictionary.java Mon May 29 18:41:19 2017
@@ -72,7 +72,7 @@ public interface Dictionary<K, V> {
      * not exist. Will also return null if the key refers to a null value. Use
      * <tt>containsKey()</tt> to distinguish between these two cases.
      */
-    public V get(K key);
+    V get(K key);
 
     /**
      * Sets the value of the given key, creating a new entry or replacing the
@@ -82,7 +82,7 @@ public interface Dictionary<K, V> {
      * @param value The value to be associated with the given key.
      * @return The value previously associated with the key.
      */
-    public V put(K key, V value);
+    V put(K key, V value);
 
     /**
      * Removes a key/value pair from the map.
@@ -90,7 +90,7 @@ public interface Dictionary<K, V> {
      * @param key The key whose mapping is to be removed.
      * @return The value that was removed.
      */
-    public V remove(K key);
+    V remove(K key);
 
     /**
      * Tests the existence of a key in the dictionary.
@@ -99,5 +99,35 @@ public interface Dictionary<K, V> {
      * @return <tt>true</tt> if the key exists in the dictionary; <tt>false</tt>,
      * otherwise.
      */
-    public boolean containsKey(K key);
+    boolean containsKey(K key);
+
+    /**
+     * Using the other methods in this interface, retrieve an integer value
+     * from this dictionary; returning 0 if the key does not exist.
+     *
+     * @param key The key for the (supposed) <tt>Integer</tt>
+     * value to retrieve (actually any {@link Number} will work).
+     * @return The integer value, or 0 if the key is not present.
+     */
+    default int getIntValue(K key) {
+        if (containsKey(key)) {
+            return ((Number)get(key)).intValue();
+        } else {
+            return 0;
+        }
+    }
+
+    /**
+     * Using the other methods in this interface, put an integer value
+     * into this dictionary.
+     *
+     * @param key The key for the <tt>Integer</tt> value to save.
+     * @param value The int value to be saved.
+     * @return The previous value for this key.
+     */
+    @SuppressWarnings("unchecked")
+    default V putIntValue(K key, int value) {
+        return put(key, (V)Integer.valueOf(value));
+    }
+
 }

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=1796670&r1=1796669&r2=1796670&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java Mon May 29 18:41:19 2017
@@ -19,6 +19,7 @@ package org.apache.pivot.wtk;
 import java.io.Serializable;
 
 import org.apache.pivot.collections.Dictionary;
+import org.apache.pivot.collections.List;
 import org.apache.pivot.json.JSONSerializer;
 import org.apache.pivot.serialization.SerializationException;
 import org.apache.pivot.util.Utils;
@@ -97,34 +98,14 @@ public final class Bounds implements Ser
     public Bounds(Dictionary<String, ?> bounds) {
         Utils.checkNull(bounds, "bounds");
 
-        if (bounds.containsKey(X_KEY)) {
-            x = ((Integer) bounds.get(X_KEY)).intValue();
-        } else {
-            x = 0;
-        }
-
-        if (bounds.containsKey(Y_KEY)) {
-            y = ((Integer) bounds.get(Y_KEY)).intValue();
-        } else {
-            y = 0;
-        }
-
-        if (bounds.containsKey(WIDTH_KEY)) {
-            width = ((Integer) bounds.get(WIDTH_KEY)).intValue();
-        } else {
-            width = 0;
-        }
-
-        if (bounds.containsKey(HEIGHT_KEY)) {
-            height = ((Integer) bounds.get(HEIGHT_KEY)).intValue();
-        } else {
-            height = 0;
-        }
+        x = bounds.getIntValue(X_KEY);
+        y = bounds.getIntValue(Y_KEY);
+        width = bounds.getIntValue(WIDTH_KEY);
+        height = bounds.getIntValue(HEIGHT_KEY);
     }
 
     /**
-     * Convert a {@link java.awt.Rectangle} to one of our bounds
-     * objects.
+     * Convert a {@link java.awt.Rectangle} to one of our bounds objects.
      * @param rectangle The existing rectangle to convert (cannot
      * be {@code null}).
      * @throws IllegalArgumentException if the rectangle is {@code null}.
@@ -357,7 +338,8 @@ public final class Bounds implements Ser
 
         if (object instanceof Bounds) {
             Bounds bounds = (Bounds) object;
-            equals = (x == bounds.x && y == bounds.y && width == bounds.width && height == bounds.height);
+            equals = (x == bounds.x && y == bounds.y &&
+                      width == bounds.width && height == bounds.height);
         }
 
         return equals;
@@ -383,33 +365,52 @@ public final class Bounds implements Ser
 
     /**
      * @return A more-or-less human-readable representation of this object, which looks like:
-     * <pre>org.apache.pivot.wtk.Bounds [X,Y;WxH]</pre>
+     * <pre>Bounds [X,Y;WxH]</pre>
      */
     @Override
     public String toString() {
-        return getClass().getName() + " [" + x + "," + y + ";" + width + "x" + height + "]";
+        return getClass().getSimpleName() + " [" + x + "," + y + ";" + width + "x" + height + "]";
     }
 
     /**
-     * Decode a JSON-encoded string (map) that contains the values for a new
+     * Decode a JSON-encoded string (map or list) that contains the values for a new
      * bounded area.
-     * @param boundsValue The JSON string containing the map of bounds values
+     * <p> The format of a JSON map format will be:
+     * <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>
+     *
+     * @param boundsValue The JSON string containing the map or list of bounds values
      * (must not be {@code null}).
      * @return The new bounds object if the string can be successfully decoded.
      * @throws IllegalArgumentException if the given string is {@code null} or
-     * the string could not be parsed as a JSON map.
+     * empty or the string could not be parsed as a JSON map or list.
      * @see #Bounds(Dictionary)
+     * @see #Bounds(int, int, int, int)
      */
     public static Bounds decode(String boundsValue) {
-        Utils.checkNull(boundsValue, "boundsValue");
+        Utils.checkNullOrEmpty(boundsValue, "boundsValue");
 
         Bounds bounds;
-        try {
-            bounds = new Bounds(JSONSerializer.parseMap(boundsValue));
-        } catch (SerializationException exception) {
-            throw new IllegalArgumentException(exception);
+        if (boundsValue.startsWith("{")) {
+            try {
+                bounds = new Bounds(JSONSerializer.parseMap(boundsValue));
+            } catch (SerializationException exception) {
+                throw new IllegalArgumentException(exception);
+            }
+        } else if (boundsValue.startsWith("[")) {
+            try {
+                @SuppressWarnings("unchecked")
+                List<Integer> values = (List<Integer>)JSONSerializer.parseList(boundsValue);
+                bounds = new Bounds(values.get(0), values.get(1), values.get(2), values.get(3));
+            } catch (SerializationException exception) {
+                throw new IllegalArgumentException(exception);
+            }
+        } else {
+            throw new IllegalArgumentException("Invalid format for Bounds.");
         }
 
         return bounds;
     }
+
 }