Skip to content

Instantly share code, notes, and snippets.

@jldupont
Created September 2, 2011 11:58
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jldupont/1188437 to your computer and use it in GitHub Desktop.
GWT Map
/**
* Map
*
* @author jldupont
*/
package com.cdf.front.types.client;
import java.util.Iterator;
import java.util.Set;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
public class Map {
JSONObject jso=new JSONObject();
public Map() {}
public void putFromJso(JSONObject jso) {
Set<String> keys=jso.keySet();
Iterator<String> it=keys.iterator();
while(it.hasNext()) {
String key=it.next();
this.store(key, jso.get(key).toString());
}
}
public void store(String key, String value) {
jso.put(key, new JSONString(value));
}
public String retrieve(String key) {
return jso.get(key).isString().stringValue();
}
/*
* @todo write native method
*/
public final void iterate(IMapVisitor<String> visitor) {
Set<String> keys=jso.keySet();
Iterator<String> it=keys.iterator();
while (it.hasNext()) {
String key=it.next();
visitor.visit(key, this.retrieve(key));
}
}
}///
@tbroyer
Copy link

tbroyer commented Sep 2, 2011

Why extend JSONObject when you can wrap it? Also, "doing" things in a constructor is generally seen as bad practice (use a factory method or a distinct putAll(JSONObject) method instead).
Oh, and if you're going to "store" directly into a JSONObject, then I'd rather use JSNI (rather than wrapping everything in JSONValue-s, which adds unnecessary overhead; have a look at how JsoSplittable –which backs AutoBean on the client-side– works)

@jldupont
Copy link
Author

jldupont commented Sep 2, 2011

  1. Updated the gist based on your comment - Thanks!
  2. I am not using JSNI because it was causing me grief with regards to the generic IMapVisitor. Anyhow this class doesn't need to be very optimized.

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment