View Javadoc

1   package com.atlassian.marketplace.client.model;
2   
3   import java.net.URI;
4   
5   import com.atlassian.fugue.Option;
6   
7   import static com.atlassian.fugue.Option.none;
8   
9   /**
10   * Represents an image hosted by Marketplace, such as an add-on logo.
11   * @since 2.0.0
12   */
13  public final class ImageInfo implements Entity
14  {
15      Links _links;
16      @RequiredLink(rel = "self") URI selfUri;
17      @RequiredLink(rel = "image") URI imageUri;
18      
19      /**
20       * Image size constants for use with {@link ImageInfo#getImageUri(Size, Resolution)}).
21       */
22      public static enum Size
23      {
24          /**
25           * Represents the default size for whatever type of image this is.  For instance, for an add-on
26           * logo the default size is 72x72px at standard resolution.
27           */
28          DEFAULT_SIZE,
29          /**
30           * Represents a standard smaller size that may be defined for some types of images.  For instance,
31           * for an add-on logo the small size is 16x16px at standard resolution.
32           */
33          SMALL_SIZE
34      }
35      
36      /**
37       * Image resolution constants for use with {@link ImageInfo#getImageUri(Size, Resolution)}).
38       */
39      public static enum Resolution
40      {
41          /**
42           * Represents the default resolution for the image.
43           */
44          DEFAULT_RESOLUTION,
45          /**
46           * Represents a higher-resolution version of the image that may or may not be available.  This
47           * normally is twice as wide and twice as high in pixel count.
48           */
49          HIGH_RESOLUTION
50      }
51      
52      @Override
53      public Links getLinks()
54      {
55          return _links;
56      }
57      
58      /**
59       * Returns the URI of the image at its default size, for displaying at standard resolution.
60       */
61      public URI getImageUri()
62      {
63          return imageUri;
64      }
65      
66      /**
67       * Returns a URI for the image at a different size, or for displaying at higher resolution.
68       * This may not exist for all images.
69       */
70      public Option<URI> getImageUri(Size size, Resolution resolution)
71      {
72          return _links.getUri(getImageLinkRel(size, resolution));
73      }
74      
75      /**
76       * Returns the content type for the image, if known.
77       */
78      public Option<String> getImageContentType(Size size, Resolution resolution)
79      {
80          for (Link link: _links.getLink(getImageLinkRel(size, resolution)))
81          {
82              return link.getType();
83          }
84          return none();
85      }
86      
87      /**
88       * Returns the self link to the image resource in the {@link com.atlassian.marketplace.client.api.Assets}
89       * API.  This is not the URI of the image itself.
90       */
91      public URI getSelfUri()
92      {
93          return selfUri;
94      }
95      
96      static String getImageLinkRel(Size size, Resolution resolution)
97      {
98          if (resolution == Resolution.HIGH_RESOLUTION)
99          {
100             return (size == Size.SMALL_SIZE) ? "smallHighResImage" : "highRes";
101         }
102         else
103         {
104             return (size == Size.SMALL_SIZE) ? "smallImage" : "image";
105         }
106     }
107 }