Coverage Report - org.codeforamerica.open311.facade.data.RelationshipManager
 
Classes in this File Line Coverage Branch Coverage Complexity
RelationshipManager
94%
17/18
75%
9/12
3
 
 1  
 package org.codeforamerica.open311.facade.data;
 2  
 
 3  
 import java.util.HashMap;
 4  
 import java.util.List;
 5  
 import java.util.Map;
 6  
 import java.util.Map.Entry;
 7  
 
 8  
 import org.codeforamerica.open311.facade.APIWrapper;
 9  
 import org.codeforamerica.open311.facade.exceptions.APIWrapperException;
 10  
 import org.codeforamerica.open311.facade.exceptions.APIWrapperException.Error;
 11  
 
 12  
 /**
 13  
  * This class maintains a map between {@link APIWrapper} and {@link Service}
 14  
  * instances. This is useful to allow a service to request its service
 15  
  * definition without knowing the {@link APIWrapper} which created it in
 16  
  * advance.
 17  
  * 
 18  
  * This class is only called by instances of classes from its package.
 19  
  * 
 20  
  * Singleton class.
 21  
  * 
 22  
  * @author Santiago MunĂ­n <santimunin@gmail.com>
 23  
  * 
 24  
  */
 25  
 public class RelationshipManager {
 26  
         /**
 27  
          * Unique instance of the class.
 28  
          */
 29  1
         private static RelationshipManager instance = new RelationshipManager();
 30  
         /**
 31  
          * Maintain a list of pairs (List<{@link Service}>, {@link APIWrapper})
 32  
          * instances.
 33  
          */
 34  1
         private Map<List<Service>, APIWrapper> serviceWrapperRelationships = new HashMap<List<Service>, APIWrapper>();
 35  
 
 36  1
         private RelationshipManager() {
 37  
 
 38  1
         }
 39  
 
 40  
         /**
 41  
          * 
 42  
          * @return Unique instance of the class.
 43  
          */
 44  
         public static RelationshipManager getInstance() {
 45  5
                 return instance;
 46  
         }
 47  
 
 48  
         /**
 49  
          * Adds a relationship between a List<{@link Service}> and the
 50  
          * {@link APIWrapper} which generated it.
 51  
          * 
 52  
          * @param services
 53  
          *            list of services.
 54  
          * @param originator
 55  
          *            {@link Service} generator.
 56  
          */
 57  
         public void addServiceWrapperRelationship(List<Service> services,
 58  
                         APIWrapper originator) {
 59  3
                 if (services != null && originator != null) {
 60  3
                         serviceWrapperRelationships.put(services, originator);
 61  
                 }
 62  3
         }
 63  
 
 64  
         /**
 65  
          * Gets a service definition given a service. This operation could involve a
 66  
          * lot of time because it calls the
 67  
          * {@link APIWrapper#getServiceDefinition(String)} method.
 68  
          * 
 69  
          * @param service
 70  
          *            Instance of a service.
 71  
          * @return Its server definition.
 72  
          * @throws APIWrapperException
 73  
          *             If there is not an {@link APIWrapper} which created it.
 74  
          */
 75  
         /* package */ServiceDefinition getServiceDefinitionFromService(
 76  
                         Service service) throws APIWrapperException {
 77  2
                 if (service == null) {
 78  0
                         throw new NullPointerException();
 79  
                 }
 80  2
                 APIWrapper wrapper = null;
 81  2
                 for (Entry<List<Service>, APIWrapper> entry : serviceWrapperRelationships
 82  
                                 .entrySet()) {
 83  3
                         if (entry.getKey().contains(service)) {
 84  1
                                 wrapper = entry.getValue();
 85  1
                                 break;
 86  
                         }
 87  
                 }
 88  2
                 if (wrapper == null) {
 89  1
                         throw new APIWrapperException("Couldn't obtain a related wrapper",
 90  
                                         Error.NOT_CREATED_BY_A_WRAPPER, null);
 91  
                 }
 92  1
                 return wrapper.getServiceDefinition(service.getServiceCode());
 93  
         }
 94  
 }