Coverage Report - org.apache.shindig.social.opensocial.spi.UserId
 
Classes in this File Line Coverage Branch Coverage Complexity
UserId
90%
19/21
67%
10/15
0
UserId$1
100%
1/1
N/A
0
UserId$Type
100%
7/7
N/A
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 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  
     /** A map of JSON strings to Type objects */
 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  
     /** Return the Type enum value given a specific jsonType **/
 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  
   // These are overriden so that EasyMock doesn't throw a fit
 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  
 }