View Javadoc

1   package com.atlassian.plugins.rest.common.expand.resolver;
2   
3   import com.atlassian.plugins.rest.common.expand.DefaultExpandContext;
4   import com.atlassian.plugins.rest.common.expand.EntityCrawler;
5   import com.atlassian.plugins.rest.common.expand.EntityExpander;
6   import com.atlassian.plugins.rest.common.expand.ExpandContext;
7   import com.google.common.collect.ImmutableMap;
8   import com.google.common.collect.Lists;
9   
10  import java.util.Collection;
11  import java.util.LinkedList;
12  import java.util.List;
13  import java.util.Map;
14  
15  /**
16   * An {@link EntityExpanderResolver} that can find {@link EntityExpander entity expanders} for:
17   * <ul>
18   * <li>{@link List}</li>
19   * <li>{@link Collection}</li>
20   * </ul>
21   */
22  public class CollectionEntityExpanderResolver implements EntityExpanderResolver
23  {
24      private static final List<Class<? extends Collection>> TYPES = Lists.newArrayList(List.class, Collection.class);
25  
26      private static final Map<Class<?>, EntityExpander<?>> EXPANDERS = ImmutableMap.<Class<?>, EntityExpander<?>>builder()
27              .put(List.class, new ListExpander())
28              .put(Collection.class, new CollectionExpander())
29              .build();
30  
31      public boolean hasExpander(Class<?> type)
32      {
33          for (Class<? extends Collection> expandableType : TYPES)
34          {
35              if (expandableType.isAssignableFrom(type))
36              {
37                  return true;
38              }
39          }
40          return false;
41      }
42  
43      @SuppressWarnings("unchecked")
44      public <T> EntityExpander<T> getExpander(Class<? extends T> type)
45      {
46          for (Class<? extends Collection> expandableType : TYPES)
47          {
48              if (expandableType.isAssignableFrom(type))
49              {
50                  return (EntityExpander<T>) EXPANDERS.get(expandableType);
51              }
52          }
53          return null;
54      }
55  
56      private static class ListExpander implements EntityExpander<List>
57      {
58          public List expand(ExpandContext<List> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler)
59          {
60              final List list = new LinkedList();
61              for (Object item : context.getEntity())
62              {
63                  final ExpandContext<Object> itemContext = new DefaultExpandContext<Object>(item, context.getExpandable(), context.getEntityExpandParameter());
64                  final EntityExpander<Object> entityExpander = item != null ? expanderResolver.getExpander(item.getClass()) : null;
65                  list.add(entityExpander != null ? entityExpander.expand(itemContext, expanderResolver, entityCrawler) : item);
66              }
67              return list;
68          }
69      }
70  
71      private static class CollectionExpander implements EntityExpander<Collection>
72      {
73          public Collection expand(ExpandContext<Collection> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler)
74          {
75              final List list = new LinkedList();
76              for (Object item : context.getEntity())
77              {
78                  final ExpandContext<Object> itemContext = new DefaultExpandContext<Object>(item, context.getExpandable(), context.getEntityExpandParameter());
79                  final EntityExpander<Object> entityExpander = item != null ? expanderResolver.getExpander(item.getClass()) : null;
80                  list.add(entityExpander != null ? entityExpander.expand(itemContext, expanderResolver, entityCrawler) : item);
81              }
82              return list;
83          }
84      }
85  }