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