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   * @since   v1.0.7
13   * @author  Erik van Zijst
14   */
15  public class SelfExpandingExpander extends AbstractRecursiveEntityExpander<SelfExpanding>
16  {
17      protected SelfExpanding expandInternal(SelfExpanding selfExpandingObject)
18      {
19          selfExpandingObject.expand();
20          return selfExpandingObject;
21      }
22  
23      /**
24       * To use the self expanding mechanism, make sure you register an instance
25       * of this {@link EntityExpanderResolver} in your application's
26       * {@link ExpandResponseFilter}.
27       */
28      public static class Resolver implements EntityExpanderResolver
29      {
30          private static final SelfExpandingExpander expander = new SelfExpandingExpander();
31  
32          public <T> boolean hasExpander(T instance)
33          {
34              return hasExpander(instance.getClass());
35          }
36  
37          public boolean hasExpander(Class<?> aClass)
38          {
39              return SelfExpanding.class.isAssignableFrom(aClass);
40          }
41  
42          @SuppressWarnings("unchecked")
43          public <T> EntityExpander<T> getExpander(T instance)
44          {
45              return (EntityExpander<T>) getExpander(instance.getClass());
46          }
47  
48          public <T> EntityExpander<T> getExpander(Class<? extends T> aClass)
49          {
50              return hasExpander(aClass) ? (EntityExpander<T>) expander : null;
51          }
52      }
53  }