View Javadoc

1   package com.atlassian.marketplace.client.util;
2   
3   import java.net.URI;
4   
5   import com.atlassian.fugue.Option;
6   import com.atlassian.marketplace.client.model.Entity;
7   import com.atlassian.marketplace.client.model.Links;
8   
9   import com.google.common.base.Function;
10  import com.google.common.collect.ImmutableList;
11  
12  /**
13   * Helper methods for resource entities in the Marketplace v2 API.
14   * @since 2.0.0
15   */
16  public abstract class EntityFunctions
17  {
18      private EntityFunctions()
19      {
20      }
21      
22      public static <T extends Entity> Function<T, Links> links()
23      {
24          return new Function<T, Links>()
25          {
26              public Links apply(T t)
27              {
28                  return t.getLinks();
29              }
30          };
31      }
32  
33      public static <T extends Entity> Option<URI> selfUri(T entity)
34      {
35          return entity.getLinks().getUri("self");
36      }
37      
38      public static <T extends Entity> Function<T, Option<URI>> selfUri()
39      {
40          return new Function<T, Option<URI>>()
41          {
42              public Option<URI> apply(T e)
43              {
44                  return selfUri(e);
45              }
46          };
47      }
48      
49      public static <T extends Entity> Iterable<URI> entityLinks(Iterable<T> entities)
50      {
51          ImmutableList.Builder<URI> ret = ImmutableList.builder();
52          for (T e: entities)
53          {
54              for (URI u: selfUri(e))
55              {
56                  ret.add(u);
57              }
58          }
59          return ret.build();
60      }
61  }