| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.shindig.social.opensocial.spi; |
| 19 | |
|
| 20 | |
import org.apache.shindig.auth.SecurityToken; |
| 21 | |
|
| 22 | |
import org.apache.commons.lang.StringUtils; |
| 23 | |
|
| 24 | |
import com.google.common.collect.Maps; |
| 25 | |
|
| 26 | |
import java.util.Map; |
| 27 | |
|
| 28 | |
public class UserId { |
| 29 | 7 | public enum Type { |
| 30 | 1 | me, viewer, owner, userId; |
| 31 | |
|
| 32 | |
|
| 33 | 1 | private static final Map<String, Type> jsonTypeMap = Maps.newHashMap(); |
| 34 | |
|
| 35 | |
static { |
| 36 | 5 | for (Type type : Type.values()) { |
| 37 | 4 | jsonTypeMap.put("@" + type.name(), type); |
| 38 | |
} |
| 39 | 1 | } |
| 40 | |
|
| 41 | |
public static Type jsonValueOf(String jsonType) { |
| 42 | 75 | return jsonTypeMap.get(jsonType); |
| 43 | |
} |
| 44 | |
} |
| 45 | |
|
| 46 | |
private Type type; |
| 47 | |
private String userId; |
| 48 | |
|
| 49 | 90 | public UserId(Type type, String userId) { |
| 50 | 90 | this.type = type; |
| 51 | 90 | this.userId = userId; |
| 52 | 90 | } |
| 53 | |
|
| 54 | |
public Type getType() { |
| 55 | 6 | return type; |
| 56 | |
} |
| 57 | |
|
| 58 | |
public String getUserId() { |
| 59 | 4 | return userId; |
| 60 | |
} |
| 61 | |
|
| 62 | |
public String getUserId(SecurityToken token) { |
| 63 | 64 | switch(type) { |
| 64 | |
case owner: |
| 65 | 1 | return token.getOwnerId(); |
| 66 | |
case viewer: |
| 67 | |
case me: |
| 68 | 2 | return token.getViewerId(); |
| 69 | |
case userId: |
| 70 | 61 | return userId; |
| 71 | |
default: |
| 72 | 0 | throw new IllegalStateException("The type field is not a valid enum: " + type); |
| 73 | |
} |
| 74 | |
} |
| 75 | |
|
| 76 | |
public static UserId fromJson(String jsonId) { |
| 77 | 75 | Type idSpecEnum = Type.jsonValueOf(jsonId); |
| 78 | 75 | if (idSpecEnum != null) { |
| 79 | 5 | return new UserId(idSpecEnum, null); |
| 80 | |
} |
| 81 | |
|
| 82 | 70 | return new UserId(Type.userId, jsonId); |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
@Override |
| 87 | |
public boolean equals(Object o) { |
| 88 | 25 | if (!(o instanceof UserId)) { |
| 89 | 0 | return false; |
| 90 | |
} |
| 91 | |
|
| 92 | 25 | UserId actual = (UserId) o; |
| 93 | 25 | return this.type == actual.type |
| 94 | |
&& StringUtils.equals(this.userId, actual.userId); |
| 95 | |
} |
| 96 | |
|
| 97 | |
@Override |
| 98 | |
public int hashCode() { |
| 99 | 107 | int userHashCode = this.userId == null ? 0 : this.userId.hashCode(); |
| 100 | 107 | return this.type.hashCode() + userHashCode; |
| 101 | |
} |
| 102 | |
} |