You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/04/03 19:51:52 UTC

svn commit: r761739 - in /incubator/pivot/trunk: core-test/src/pivot/core/test/ core/src/pivot/collections/ core/src/pivot/serialization/ core/src/pivot/util/

Author: gbrown
Date: Fri Apr  3 17:51:52 2009
New Revision: 761739

URL: http://svn.apache.org/viewvc?rev=761739&view=rev
Log:
Add additional path-based utility methods to JSONSerializer; update stub version of Preferences class.

Modified:
    incubator/pivot/trunk/core-test/src/pivot/core/test/JSONSerializerTest.java
    incubator/pivot/trunk/core/src/pivot/collections/Dictionary.java
    incubator/pivot/trunk/core/src/pivot/serialization/JSONSerializer.java
    incubator/pivot/trunk/core/src/pivot/util/Preferences.java
    incubator/pivot/trunk/core/src/pivot/util/Resources.java

Modified: incubator/pivot/trunk/core-test/src/pivot/core/test/JSONSerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core-test/src/pivot/core/test/JSONSerializerTest.java?rev=761739&r1=761738&r2=761739&view=diff
==============================================================================
--- incubator/pivot/trunk/core-test/src/pivot/core/test/JSONSerializerTest.java (original)
+++ incubator/pivot/trunk/core-test/src/pivot/core/test/JSONSerializerTest.java Fri Apr  3 17:51:52 2009
@@ -59,10 +59,11 @@
     };
 
     public static void main(String[] args) {
-        test0();
+        // test0();
         // test1();
         // test2();
-    	test3();
+    	// test3();
+        test4();
     }
 
     public static void test0() {
@@ -163,7 +164,7 @@
         JSONSerializer jsonSerializer = new JSONSerializer();
 
         try {
-            jsonSerializer.writeObject(JSONSerializer.getValue(JSONSerializer.parseList(list), path),
+            jsonSerializer.writeObject(JSONSerializer.get(JSONSerializer.parseList(list), path),
                 System.out);
             System.out.println();
         } catch(Exception exception) {
@@ -175,11 +176,35 @@
         JSONSerializer jsonSerializer = new JSONSerializer();
 
         try {
-            jsonSerializer.writeObject(JSONSerializer.getValue(JSONSerializer.parseMap(map), path),
+            jsonSerializer.writeObject(JSONSerializer.get(JSONSerializer.parseMap(map), path),
                 System.out);
             System.out.println();
         } catch(Exception exception) {
             System.out.println(exception);
         }
     }
+
+    public static void test4() {
+        Object root = JSONSerializer.parse("{a:{b:{c:'hello', d:'world'}, e:[1, 2, 3], f:false}, h:null}");
+        testGet(root, "a");
+        testGet(root, "a.b");
+        testGet(root, "a.b.c");
+        testGet(root, "a.b.d");
+        testGet(root, "a['e']");
+        testGet(root, "a['e'][1]");
+        testGet(root, "a['f']");
+        testGet(root, "a['h']");
+
+        JSONSerializer.put(root, "a['h']", 100);
+        testGet(root, "a['h']");
+
+        JSONSerializer.remove(root, "a['h']");
+
+        System.out.println("a['h'] exists: " + JSONSerializer.containsKey(root, "a['h']"));
+    }
+
+    private static void testGet(Object root, String path) {
+        Object value = JSONSerializer.get(root, path);
+        System.out.println(path + ": " + value);
+    }
 }

Modified: incubator/pivot/trunk/core/src/pivot/collections/Dictionary.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/collections/Dictionary.java?rev=761739&r1=761738&r2=761739&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/collections/Dictionary.java (original)
+++ incubator/pivot/trunk/core/src/pivot/collections/Dictionary.java Fri Apr  3 17:51:52 2009
@@ -44,6 +44,9 @@
      *
      * @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);
 

Modified: incubator/pivot/trunk/core/src/pivot/serialization/JSONSerializer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/serialization/JSONSerializer.java?rev=761739&r1=761738&r2=761739&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/serialization/JSONSerializer.java (original)
+++ incubator/pivot/trunk/core/src/pivot/serialization/JSONSerializer.java Fri Apr  3 17:51:52 2009
@@ -29,7 +29,6 @@
 import java.nio.charset.Charset;
 
 import pivot.collections.ArrayList;
-import pivot.collections.Dictionary;
 import pivot.collections.HashMap;
 import pivot.collections.List;
 import pivot.collections.Map;
@@ -579,18 +578,318 @@
      *
      * @return
      * The value at the given path.
+     *
+     * @deprecated
+     * This method is deprecated; use {@link #get(Object, String)} instead.
      */
-    @SuppressWarnings("unchecked")
+    @Deprecated
     public static Object getValue(Object root, String path) {
+        return get(root, path);
+    }
+
+    /**
+     * Returns the value at the given path.
+     *
+     * @param root
+     * The root object; must be an instance of {@link pivot.collections.Map}
+     * or {@link pivot.collections.List}.
+     *
+     * @param path
+     * The path to the value, in JavaScript path notation.
+     *
+     * @return
+     * The value at the given path.
+     */
+    public static Object get(Object root, String path) {
+        if (root == null) {
+            throw new IllegalArgumentException("root is null.");
+        }
+
+        if (path == null) {
+            throw new IllegalArgumentException("path is null.");
+        }
+
+        return get(root, split(path));
+    }
+
+    @SuppressWarnings("unchecked")
+    private static Object get(Object root, Sequence<String> keys) {
         Object value = root;
 
+        for (int i = 0, n = keys.getLength(); i < n; i++) {
+            String key = keys.get(i);
+
+            if (value instanceof Map) {
+                Map<String, Object> map = (Map<String, Object>)value;
+                value = map.get(key);
+            } else if (value instanceof Sequence) {
+                 List<Object> list = (List<Object>)value;
+                 value = list.get(Integer.parseInt(key));
+            } else {
+                throw new IllegalArgumentException("Invalid path.");
+            }
+        }
+
+        return value;
+    }
+
+    /**
+     * Returns the value at the given path as a string.
+     *
+     * @param root
+     * @param path
+     *
+     * @see #getValue(Object, String)
+     */
+    public static String getString(Object root, String path) {
+        return (String)get(root, path);
+    }
+
+    /**
+     * Returns the value at the given path as a number.
+     *
+     * @param root
+     * @param path
+     *
+     * @see #getValue(Object, String)
+     */
+    public static Number getNumber(Object root, String path) {
+        return (Number)get(root, path);
+    }
+
+    /**
+     * Returns the value at the given path as a short.
+     *
+     * @param root
+     * @param path
+     *
+     * @see #getValue(Object, String)
+     */
+    public static Short getShort(Object root, String path) {
+        return (Short)get(root, path);
+    }
+
+    /**
+     * Returns the value at the given path as an integer.
+     *
+     * @param root
+     * @param path
+     *
+     * @see #getValue(Object, String)
+     */
+    public static Integer getInteger(Object root, String path) {
+        return (Integer)get(root, path);
+    }
+
+    /**
+     * Returns the value at the given path as a long.
+     *
+     * @param root
+     * @param path
+     *
+     * @see #getValue(Object, String)
+     */
+    public static Long getLong(Object root, String path) {
+        return (Long)get(root, path);
+    }
+
+    /**
+     * Returns the value at the given path as a float.
+     *
+     * @param root
+     * @param path
+     *
+     * @see #getValue(Object, String)
+     */
+    public static Float getFloat(Object root, String path) {
+        return (Float)get(root, path);
+    }
+
+    /**
+     * Returns the value at the given path as a double.
+     *
+     * @param root
+     * @param path
+     *
+     * @see #getValue(Object, String)
+     */
+    public static Double getDouble(Object root, String path) {
+        return (Double)get(root, path);
+    }
+
+    /**
+     * Returns the value at the given path as a boolean.
+     *
+     * @param root
+     * @param path
+     *
+     * @see #getValue(Object, String)
+     */
+    public static Boolean getBoolean(Object root, String path) {
+        return (Boolean)get(root, path);
+    }
+
+    /**
+     * Returns the value at the given path as a list.
+     *
+     * @param root
+     * @param path
+     *
+     * @see #getValue(Object, String)
+     */
+    public static List<?> getList(Object root, String path) {
+        return (List<?>)get(root, path);
+    }
+
+    /**
+     * Returns the value at the given path as a map.
+     *
+     * @param root
+     * @param path
+     *
+     * @see #getValue(Object, String)
+     */
+    @SuppressWarnings("unchecked")
+    public static Map<String, ?> getMap(Object root, String path) {
+        return (Map<String, ?>)get(root, path);
+    }
+
+    /**
+     * Sets the value at the given path.
+     *
+     * @param root
+     * @param path
+     * @param value
+     *
+     * @return
+     * The value previously associated with the path.
+     */
+    @SuppressWarnings("unchecked")
+    public static Object put(Object root, String path, Object value) {
+        if (root == null) {
+            throw new IllegalArgumentException("root is null.");
+        }
+
+        if (path == null) {
+            throw new IllegalArgumentException("path is null.");
+        }
+
+        Object previousValue;
+
+        Sequence<String> keys = split(path);
+        if (keys.getLength() == 0) {
+            throw new IllegalArgumentException("Bad path.");
+        }
+
+        String key = keys.remove(keys.getLength() - 1, 1).get(0);
+
+        Object parent = get(root, keys);
+        if (parent instanceof Map) {
+            Map<String, Object> map = (Map<String, Object>)parent;
+            previousValue = map.put(key, value);
+        } else if (parent instanceof Sequence) {
+             List<Object> list = (List<Object>)parent;
+             previousValue = list.update(Integer.parseInt(key), value);
+        } else {
+            throw new IllegalArgumentException("Invalid path.");
+        }
+
+        return previousValue;
+    }
+
+    /**
+     * Removes the value at the given path.
+     *
+     * @param root
+     * @param path
+     *
+     * @return
+     * The value that was removed.
+     */
+    @SuppressWarnings("unchecked")
+    public static Object remove(Object root, String path) {
+        if (root == null) {
+            throw new IllegalArgumentException("root is null.");
+        }
+
+        if (path == null) {
+            throw new IllegalArgumentException("path is null.");
+        }
+
+        Object previousValue;
+
+        Sequence<String> keys = split(path);
+        if (keys.getLength() == 0) {
+            throw new IllegalArgumentException("Bad path.");
+        }
+
+        String key = keys.remove(keys.getLength() - 1, 1).get(0);
+
+        Object parent = get(root, keys);
+        if (parent instanceof Map) {
+            Map<String, Object> map = (Map<String, Object>)parent;
+            previousValue = map.remove(key);
+        } else if (parent instanceof Sequence) {
+             List<Object> list = (List<Object>)parent;
+             previousValue = list.remove(Integer.parseInt(key), 1).get(0);
+        } else {
+            throw new IllegalArgumentException("Invalid path.");
+        }
+
+        return previousValue;
+    }
+
+    /**
+     * Tests the existence of a path in a given object.
+     *
+     * @param root
+     * @param path
+     *
+     * @return
+     * <tt>true</tt> if the path exists; <tt>false</tt>, otherwise.
+     */
+    @SuppressWarnings("unchecked")
+    public static boolean containsKey(Object root, String path) {
+        if (root == null) {
+            throw new IllegalArgumentException("root is null.");
+        }
+
+        if (path == null) {
+            throw new IllegalArgumentException("path is null.");
+        }
+
+        boolean containsKey;
+
+        Sequence<String> keys = split(path);
+        if (keys.getLength() == 0) {
+            throw new IllegalArgumentException("Bad path.");
+        }
+
+        String key = keys.remove(keys.getLength() - 1, 1).get(0);
+
+        Object parent = get(root, keys);
+        if (parent instanceof Map) {
+            Map<String, Object> map = (Map<String, Object>)parent;
+            containsKey = map.containsKey(key);
+        } else if (parent instanceof Sequence) {
+             List<Object> list = (List<Object>)parent;
+             containsKey = (list.getLength() > Integer.parseInt(key));
+        } else {
+            throw new IllegalArgumentException("Invalid path.");
+        }
+
+        return containsKey;
+    }
+
+    private static Sequence<String> split(String path) {
+        ArrayList<String> keys = new ArrayList<String>();
+
         int i = 0;
         int n = path.length();
 
         while (i < n) {
             char c = path.charAt(i++);
 
-            boolean keyed = true;
             StringBuilder identifierBuilder = new StringBuilder();
 
             boolean bracketed = (c == '[');
@@ -608,8 +907,6 @@
                     c = path.charAt(i++);
                 }
 
-                keyed = quoted;
-
                 while (i <= n
                     && bracketed) {
                     bracketed = quoted || (c != ']');
@@ -652,8 +949,6 @@
                     }
                 }
             } else {
-                keyed = true;
-
                 while(i <= n
                     && c != '.'
                     && c != '[') {
@@ -684,199 +979,162 @@
                 throw new IllegalArgumentException("Missing identifier.");
             }
 
-            String identifier = identifierBuilder.toString();
-
-            if (keyed) {
-                if (!(value instanceof Dictionary<?, ?>)){
-                    throw new IllegalArgumentException("Invalid path.");
-                }
-
-                String key = identifier;
-                Dictionary<String, Object> dictionary = (Dictionary<String, Object>)value;
-                value = dictionary.get(key);
-            } else {
-                if (!(value instanceof Sequence<?>)){
-                    throw new IllegalArgumentException("Invalid path.");
-                }
-
-                int index = Integer.parseInt(identifier);
-                Sequence<Object> sequence = (Sequence<Object>)value;
-                value = sequence.get(index);
-            }
+            keys.add(identifierBuilder.toString());
         }
 
-        return value;
+        return keys;
     }
 
     /**
-     * Returns the value at the given path as a string.
+     * Converts a JSON value to a Java object.
      *
-     * @param root
-     * @param path
+     * @param json
+     * The JSON value.
      *
-     * @see #getValue(Object, String)
+     * @return
+     * The parsed object.
      */
-    public static String getString(Object root, String path) {
-        return (String)getValue(root, path);
-    }
+    public static Object parse(String json) {
+        JSONSerializer jsonSerializer = new JSONSerializer();
 
-    /**
-     * Returns the value at the given path as a number.
-     *
-     * @param root
-     * @param path
-     *
-     * @see #getValue(Object, String)
-     */
-    public static Number getNumber(Object root, String path) {
-        return (Number)getValue(root, path);
+        Object object;
+        try {
+            object = jsonSerializer.readObject(new StringReader(json));
+        } catch(Exception exception) {
+            throw new RuntimeException(exception);
+        }
+
+        return object;
     }
 
     /**
-     * Returns the value at the given path as a short.
+     * Converts a JSON value to a string.
      *
-     * @param root
-     * @param path
+     * @param json
+     * The JSON value.
      *
-     * @see #getValue(Object, String)
+     * @return
+     * The parsed string.
      */
-    public static Short getShort(Object root, String path) {
-        return (Short)getValue(root, path);
+    public static String parseString(String json) {
+        return (String)parse(json);
     }
 
     /**
-     * Returns the value at the given path as an integer.
+     * Converts a JSON value to a number.
      *
-     * @param root
-     * @param path
+     * @param json
+     * The JSON value.
      *
-     * @see #getValue(Object, String)
+     * @return
+     * The parsed number.
      */
-    public static Integer getInteger(Object root, String path) {
-        return (Integer)getValue(root, path);
+    public static Number parseNumber(String json) {
+        return (Number)parse(json);
     }
 
     /**
-     * Returns the value at the given path as a long.
+     * Converts a JSON value to a short.
      *
-     * @param root
-     * @param path
+     * @param json
+     * The JSON value.
      *
-     * @see #getValue(Object, String)
+     * @return
+     * The parsed short.
      */
-    public static Long getLong(Object root, String path) {
-        return (Long)getValue(root, path);
+    public static Short parseShort(String json) {
+        return (Short)parse(json);
     }
 
     /**
-     * Returns the value at the given path as a float.
+     * Converts a JSON value to a integer.
      *
-     * @param root
-     * @param path
+     * @param json
+     * The JSON value.
      *
-     * @see #getValue(Object, String)
+     * @return
+     * The parsed integer.
      */
-    public static Float getFloat(Object root, String path) {
-        return (Float)getValue(root, path);
+    public static Integer parseInteger(String json) {
+        return (Integer)parse(json);
     }
 
     /**
-     * Returns the value at the given path as a double.
+     * Converts a JSON value to a long.
      *
-     * @param root
-     * @param path
+     * @param json
+     * The JSON value.
      *
-     * @see #getValue(Object, String)
+     * @return
+     * The parsed number.
      */
-    public static Double getDouble(Object root, String path) {
-        return (Double)getValue(root, path);
+    public static Long parseLong(String json) {
+        return (Long)parse(json);
     }
 
     /**
-     * Returns the value at the given path as a boolean.
+     * Converts a JSON value to a float.
      *
-     * @param root
-     * @param path
+     * @param json
+     * The JSON value.
      *
-     * @see #getValue(Object, String)
+     * @return
+     * The parsed float.
      */
-    public static Boolean getBoolean(Object root, String path) {
-        return (Boolean)getValue(root, path);
+    public static Float parseFloat(String json) {
+        return (Float)parse(json);
     }
 
     /**
-     * Returns the value at the given path as a list.
+     * Converts a JSON value to a double.
      *
-     * @param root
-     * @param path
+     * @param json
+     * The JSON value.
      *
-     * @see #getValue(Object, String)
+     * @return
+     * The parsed double.
      */
-    public static List<?> getList(Object root, String path) {
-        return (List<?>)getValue(root, path);
+    public static Double parseDouble(String json) {
+        return (Double)parse(json);
     }
 
     /**
-     * Returns the value at the given path as a map.
+     * Converts a JSON value to a boolean.
      *
-     * @param root
-     * @param path
+     * @param json
+     * The JSON value.
      *
-     * @see #getValue(Object, String)
+     * @return
+     * The parsed boolean.
      */
-    @SuppressWarnings("unchecked")
-    public static Map<String, ?> getMap(Object root, String path) {
-        return (Map<String, ?>)getValue(root, path);
+    public static Boolean parseBoolean(String json) {
+        return (Boolean)parse(json);
     }
 
     /**
-     * Parses a JSON-formatted array value into a list.
+     * Converts a JSON value to a list.
      *
-     * @param string
-     * A string containing a JSON array (e.g. "[1, 2, 3]").
+     * @param json
+     * The JSON value.
      *
      * @return
-     * A {@link List} instance containing the parsed JSON data.
+     * The parsed list.
      */
-    public static List<?> parseList(String string) {
-        List<?> list = null;
-        JSONSerializer jsonSerializer = new JSONSerializer();
-
-        Object object = null;
-        try {
-            object = jsonSerializer.readObject(new StringReader(string));
-        } catch(Exception exception) {
-            throw new RuntimeException(exception);
-        }
-
-        list = (List<?>)object;
-
-        return list;
+    public static List<?> parseList(String json) {
+        return (List<?>)parse(json);
     }
 
     /**
-     * Parses a JSON-formatted object value into a map.
+     * Converts a JSON value to a map.
      *
-     * @param string
-     * A string containing a JSON object (e.g. "{a:1, b:2, c:3}").
+     * @param json
+     * The JSON value.
      *
      * @return
-     * A {@link Map} instance containing the parsed JSON data.
+     * The parsed map.
      */
     @SuppressWarnings("unchecked")
-    public static Map<String, ?> parseMap(String string) {
-        Map<String, ?> map = null;
-        JSONSerializer jsonSerializer = new JSONSerializer();
-
-        Object object = null;
-        try {
-            object = jsonSerializer.readObject(new StringReader(string));
-        } catch(Exception exception) {
-            throw new RuntimeException(exception);
-        }
-
-        map = (Map<String, ?>)object;
-
-        return map;
+    public static Map<String, ?> parseMap(String json) {
+        return (Map<String, ?>)parse(json);
     }
 }

Modified: incubator/pivot/trunk/core/src/pivot/util/Preferences.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/util/Preferences.java?rev=761739&r1=761738&r2=761739&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/util/Preferences.java (original)
+++ incubator/pivot/trunk/core/src/pivot/util/Preferences.java Fri Apr  3 17:51:52 2009
@@ -16,12 +16,19 @@
  */
 package pivot.util;
 
-import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.net.URL;
 import java.util.Iterator;
 
 import pivot.collections.Dictionary;
-import pivot.serialization.SerializationException;
+import pivot.collections.HashMap;
+import pivot.collections.Map;
+import pivot.serialization.JSONSerializer;
+import pivot.util.concurrent.Dispatcher;
+import pivot.util.concurrent.Task;
+import pivot.util.concurrent.TaskExecutionException;
+import pivot.util.concurrent.TaskListener;
 
 /**
  * Provides access to application preference data. Preferences are modeled as
@@ -34,6 +41,99 @@
  * @author gbrown
  */
 public class Preferences implements Dictionary<String, Object>, Iterable<String> {
+    /**
+     * Task that loads a set of user preferences.
+     *
+     * @author gbrown
+     */
+    public static class LoadTask extends Task<Preferences> {
+        private URL url;
+
+        public LoadTask(URL url) {
+            this(url, DEFAULT_DISPATCHER);
+        }
+
+        public LoadTask(URL url, Dispatcher dispatcher) {
+            super(dispatcher);
+
+            if (url == null) {
+                throw new IllegalArgumentException();
+            }
+
+            this.url = url;
+        }
+
+        @Override
+        public Preferences execute() throws TaskExecutionException {
+            Preferences preferences = null;
+
+            try {
+                InputStream inputStream = null;
+
+                try {
+                    // TODO Load the preferences
+                } finally {
+                    if (inputStream != null) {
+                        inputStream.close();
+                    }
+                }
+            } catch(Exception exception) {
+                throw new TaskExecutionException(exception);
+            }
+
+            return preferences;
+        }
+    }
+
+    /**
+     * Task that saves a set of user preferences.
+     *
+     * @author gbrown
+     */
+    public static class SaveTask extends Task<Void> {
+        private URL url;
+        private Preferences preferences;
+
+        public SaveTask(Preferences preferences, URL url) {
+            this(preferences, url, DEFAULT_DISPATCHER);
+        }
+
+        public SaveTask(Preferences preferences, URL url, Dispatcher dispatcher) {
+            super(dispatcher);
+
+            if (url == null) {
+                throw new IllegalArgumentException();
+            }
+
+            this.url = url;
+            this.preferences = preferences;
+        }
+
+        @Override
+        public Void execute() throws TaskExecutionException {
+            try {
+                OutputStream outputStream = null;
+
+                try {
+                    // TODO Save the preferences
+                } finally {
+                    if (outputStream != null) {
+                        outputStream.close();
+                    }
+                }
+            } catch(Exception exception) {
+                throw new TaskExecutionException(exception);
+            }
+
+            return null;
+        }
+    }
+
+    /**
+     * Preferences listener list.
+     *
+     * @author gbrown
+     */
     private static class PreferencesListenerList extends ListenerList<PreferencesListener>
         implements PreferencesListener {
         public void valueAdded(Preferences preferences, String key) {
@@ -55,55 +155,182 @@
         }
     }
 
+    private HashMap<String, Object> preferencesMap;
     private PreferencesListenerList preferencesListeners = new PreferencesListenerList();
 
+    private static Dispatcher DEFAULT_DISPATCHER = new Dispatcher();
+
+    /**
+     * Constructs an empty preferences object.
+     */
+    public Preferences() {
+        this(null);
+    }
+
     /**
-     * Creates a preferences object that provides access to the preferences for
-     * a given URL.
+     * Constructs a preferences object with a set of default values.
      *
-     * @param url
      * @param defaults
      */
-    public Preferences(URL url, Dictionary<String, Object> defaults)
-        throws IOException, SerializationException {
-        // TODO
+    public Preferences(Map<String, Object> defaults) {
+        preferencesMap = new HashMap<String, Object>(defaults);
     }
 
+    /**
+     * Retrieves the preference value for the given key.
+     *
+     * @param key
+     * The key whose value is to be returned.
+     */
     public Object get(String key) {
-        // TODO Auto-generated method stub
-        return null;
+        return JSONSerializer.get(preferencesMap, key);
     }
 
+    /**
+     * Sets the preference value of the given key, creating a new entry or
+     * replacing the existing value.
+     *
+     * @param key
+     * The key whose value is to be set.
+     *
+     * @param value
+     * The value to be associated with the given key.
+     */
     public Object put(String key, Object value) {
-        // TODO Auto-generated method stub
-        return null;
+        boolean update = JSONSerializer.containsKey(preferencesMap, key);
+        Object previousValue = JSONSerializer.put(preferencesMap, key, value);
+
+        if (update) {
+            preferencesListeners.valueUpdated(this, key, previousValue);
+        }
+        else {
+            preferencesListeners.valueAdded(this, key);
+        }
+
+        return previousValue;
     }
 
+    /**
+     * Removes a key/value pair from the map.
+     *
+     * @param key
+     * The key whose mapping is to be removed.
+     *
+     * @return
+     * The value that was removed.
+     */
     public Object remove(String key) {
-        // TODO Auto-generated method stub
-        return null;
+        Object value = null;
+
+        if (JSONSerializer.containsKey(preferencesMap, key)) {
+            value = JSONSerializer.remove(preferencesMap, key);
+            preferencesListeners.valueRemoved(this, key, value);
+        }
+
+        return value;
     }
 
+    /**
+     * Tests the existence of a preference value.
+     *
+     * @param key
+     * The key of the value whose presence is to be tested.
+     *
+     * @return
+     * <tt>true</tt> if the preference value exists; <tt>false</tt>, otherwise.
+     */
     public boolean containsKey(String key) {
-        // TODO Auto-generated method stub
-        return false;
+        return JSONSerializer.containsKey(preferencesMap, key);
     }
 
+    /**
+     * Tests the emptiness of the preferences collection.
+     *
+     * @return
+     * <tt>true</tt> if the dictionary contains no keys; <tt>false</tt>,
+     * otherwise.
+     */
     public boolean isEmpty() {
-        // TODO Auto-generated method stub
-        return false;
-    }
-
-    public void save() throws IOException, SerializationException {
-        // TODO
+        return preferencesMap.isEmpty();
     }
 
+    /**
+     * Returns an iterator on the preference keys.
+     */
     public Iterator<String> iterator() {
-        // TODO
-        return null;
+        return new ImmutableIterator<String>(preferencesMap.iterator());
     }
 
+    /**
+     * Returns the preferences listener list.
+     */
     public ListenerList<PreferencesListener> getPreferencesListeners() {
         return preferencesListeners;
     }
+
+    /**
+     * Loads a set of user preferences.
+     *
+     * @param url
+     */
+    public static Preferences load(URL url) {
+        LoadTask loadTask = new LoadTask(url);
+
+        Preferences preferences = null;
+        try {
+            preferences = loadTask.execute();
+        } catch(TaskExecutionException exception) {
+            throw new RuntimeException(exception);
+        }
+
+        return preferences;
+    }
+
+    /**
+     * Loads a set of user preferences asynchronously.
+     *
+     * @param url
+     * @param loadListener
+     *
+     * @return
+     * The load task that was created.
+     */
+    public static Preferences.LoadTask load(URL url, TaskListener<Preferences> loadListener) {
+        LoadTask loadTask = new LoadTask(url);
+        loadTask.execute(loadListener);
+        return loadTask;
+    }
+
+    /**
+     * Saves a set of user preferences.
+     *
+     * @param preferences
+     * @param url
+     */
+    public static void save(Preferences preferences, URL url) {
+        SaveTask saveTask = new SaveTask(preferences, url);
+
+        try {
+            saveTask.execute();
+        } catch(TaskExecutionException exception) {
+            throw new RuntimeException(exception);
+        }
+    }
+
+    /**
+     * Saves a set of user preferences asynchronously.
+     *
+     * @param preferences
+     * @param url
+     * @param saveListener
+     *
+     * @return
+     * The save task that was created.
+     */
+    public static Preferences.SaveTask save(Preferences preferences, URL url,
+        TaskListener<Void> saveListener) {
+        SaveTask saveTask = new SaveTask(preferences, url);
+        saveTask.execute(saveListener);
+        return saveTask;
+    }
 }

Modified: incubator/pivot/trunk/core/src/pivot/util/Resources.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/pivot/util/Resources.java?rev=761739&r1=761738&r2=761739&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/pivot/util/Resources.java (original)
+++ incubator/pivot/trunk/core/src/pivot/util/Resources.java Fri Apr  3 17:51:52 2009
@@ -143,7 +143,7 @@
     }
 
     public Object get(String key) {
-        return JSONSerializer.getValue(resourceMap, key);
+        return JSONSerializer.get(resourceMap, key);
     }
 
     public Object put(String key, Object value) {