Author: lindner
Date: Wed Sep  3 04:04:58 2008
New Revision: 691567

URL: http://svn.apache.org/viewvc?rev=691567&view=rev
Log:
SHINDIG-565 | Don't use exceptions for set checking in 
UserId/GroupId.fromJson() . Patch inspired by Adam Winer

Modified:
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/spi/GroupId.java
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/spi/UserId.java

Modified: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/spi/GroupId.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/spi/GroupId.java?rev=691567&r1=691566&r2=691567&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/spi/GroupId.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/spi/GroupId.java
 Wed Sep  3 04:04:58 2008
@@ -19,9 +19,26 @@
 
 import org.apache.commons.lang.StringUtils;
 
+import com.google.common.collect.Maps;
+
+import java.util.Map;
+
 public class GroupId {
   public enum Type {
-    all, friends, self, deleted, groupId
+    all, friends, self, deleted, groupId;
+
+    /** A map of JSON strings to Type objects */
+    private static final Map<String, Type> jsonTypeMap = Maps.newHashMap();
+
+    static {
+      for (Type type : Type.values()) {
+        jsonTypeMap.put("@" + type.name(), type);
+      }
+    }
+    /** Return the Type enum value given a specific jsonType **/
+    public static Type jsonValueOf(String jsonType) {
+       return jsonTypeMap.get(jsonType);
+    }
   }
 
   private Type type;
@@ -42,12 +59,12 @@
   }
 
   public static GroupId fromJson(String jsonId) {
-    try {
-      Type idSpecEnum = Type.valueOf(jsonId.substring(1));
+    Type idSpecEnum = Type.jsonValueOf(jsonId);
+    if (idSpecEnum != null) {
       return new GroupId(idSpecEnum, null);
-    } catch (IllegalArgumentException e) {
-      return new GroupId(Type.groupId, jsonId);
     }
+
+    return new GroupId(Type.groupId, jsonId);
   }
 
   // These are overriden so that EasyMock doesn't throw a fit

Modified: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/spi/UserId.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/spi/UserId.java?rev=691567&r1=691566&r2=691567&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/spi/UserId.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/spi/UserId.java
 Wed Sep  3 04:04:58 2008
@@ -21,9 +21,26 @@
 
 import org.apache.commons.lang.StringUtils;
 
+import com.google.common.collect.Maps;
+
+import java.util.Map;
+
 public class UserId {
   public enum Type {
-    me, viewer, owner, userId
+    me, viewer, owner, userId;
+
+    /** A map of JSON strings to Type objects */
+    private static final Map<String, Type> jsonTypeMap = Maps.newHashMap();
+
+    static {
+      for (Type type : Type.values()) {
+        jsonTypeMap.put("@" + type.name(), type);
+      }
+    }
+    /** Return the Type enum value given a specific jsonType **/
+    public static Type jsonValueOf(String jsonType) {
+       return jsonTypeMap.get(jsonType);
+    }
   }
 
   private Type type;
@@ -57,12 +74,12 @@
   }
 
   public static UserId fromJson(String jsonId) {
-    try {
-      Type idSpecEnum = Type.valueOf(jsonId.substring(1));
+    Type idSpecEnum = Type.jsonValueOf(jsonId);
+    if (idSpecEnum != null) {
       return new UserId(idSpecEnum, null);
-    } catch (IllegalArgumentException e) {
-      return new UserId(Type.userId, jsonId);
     }
+
+    return new UserId(Type.userId, jsonId);
   }
 
   // These are overriden so that EasyMock doesn't throw a fit


Reply via email to