View Javadoc

1   package com.atlassian.plugins.rest.common.expand;
2   
3   import com.atlassian.plugins.rest.common.expand.resolver.EntityExpanderResolver;
4   
5   /**
6    * This is a general-purpose expander for atlassian-rest that delegates the
7    * expand process to the entity that is to be expanded (instead of having that
8    * knowledge in a separate {@link com.atlassian.plugins.rest.common.expand.EntityExpander}.
9    * As a result, this expander can be used for every entity that implements
10   * {@link SelfExpanding}.
11   *
12   * @author Erik van Zijst
13   * @since v1.0.7
14   */
15  public class SelfExpandingExpander extends AbstractRecursiveEntityExpander<SelfExpanding> {
16      protected SelfExpanding expandInternal(SelfExpanding selfExpandingObject) {
17          selfExpandingObject.expand();
18          return selfExpandingObject;
19      }
20  
21      /**
22       * To use the self expanding mechanism, make sure you register an instance
23       * of this {@link EntityExpanderResolver} in your application's
24       * {@link ExpandResponseFilter}.
25       */
26      public static class Resolver implements EntityExpanderResolver {
27          private static final SelfExpandingExpander expander = new SelfExpandingExpander();
28  
29          public <T> boolean hasExpander(T instance) {
30              return hasExpander(instance.getClass());
31          }
32  
33          public boolean hasExpander(Class<?> aClass) {
34              return SelfExpanding.class.isAssignableFrom(aClass);
35          }
36  
37          @SuppressWarnings("unchecked")
38          public <T> EntityExpander<T> getExpander(T instance) {
39              return (EntityExpander<T>) getExpander(instance.getClass());
40          }
41  
42          public <T> EntityExpander<T> getExpander(Class<? extends T> aClass) {
43              return hasExpander(aClass) ? (EntityExpander<T>) expander : null;
44          }
45      }
46  }