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      private static final List<Class<? extends Collection>> TYPES = Lists.newArrayList(List.class, Collection.class);
24  
25      private static final Map<Class<?>, EntityExpander<?>> EXPANDERS = ImmutableMap.<Class<?>, EntityExpander<?>>builder()
26              .put(List.class, new ListExpander())
27              .put(Collection.class, new CollectionExpander())
28              .build();
29  
30      public boolean hasExpander(Class<?> type) {
31          for (Class<? extends Collection> expandableType : TYPES) {
32              if (expandableType.isAssignableFrom(type)) {
33                  return true;
34              }
35          }
36          return false;
37      }
38  
39      @SuppressWarnings("unchecked")
40      public <T> EntityExpander<T> getExpander(Class<? extends T> type) {
41          for (Class<? extends Collection> expandableType : TYPES) {
42              if (expandableType.isAssignableFrom(type)) {
43                  return (EntityExpander<T>) EXPANDERS.get(expandableType);
44              }
45          }
46          return null;
47      }
48  
49      private static class ListExpander implements EntityExpander<List> {
50          public List expand(ExpandContext<List> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler) {
51              final List list = new LinkedList();
52              for (Object item : context.getEntity()) {
53                  final ExpandContext<Object> itemContext = new DefaultExpandContext<Object>(item, context.getExpandable(), context.getEntityExpandParameter());
54                  final EntityExpander<Object> entityExpander = item != null ? expanderResolver.getExpander(item.getClass()) : null;
55                  list.add(entityExpander != null ? entityExpander.expand(itemContext, expanderResolver, entityCrawler) : item);
56              }
57              return list;
58          }
59      }
60  
61      private static class CollectionExpander implements EntityExpander<Collection> {
62          public Collection expand(ExpandContext<Collection> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler) {
63              final List list = new LinkedList();
64              for (Object item : context.getEntity()) {
65                  final ExpandContext<Object> itemContext = new DefaultExpandContext<Object>(item, context.getExpandable(), context.getEntityExpandParameter());
66                  final EntityExpander<Object> entityExpander = item != null ? expanderResolver.getExpander(item.getClass()) : null;
67                  list.add(entityExpander != null ? entityExpander.expand(itemContext, expanderResolver, entityCrawler) : item);
68              }
69              return list;
70          }
71      }
72  }