View Javadoc

1   package com.atlassian.marketplace.client.impl;
2   
3   import java.net.URI;
4   
5   import com.atlassian.fugue.Option;
6   import com.atlassian.marketplace.client.api.Page;
7   import com.atlassian.marketplace.client.api.PageReader;
8   import com.atlassian.marketplace.client.api.PageReference;
9   import com.atlassian.marketplace.client.api.QueryBounds;
10  import com.atlassian.marketplace.client.model.Links;
11  
12  import static com.atlassian.fugue.Option.none;
13  import static com.atlassian.fugue.Option.some;
14  import static com.google.common.base.Preconditions.checkNotNull;
15  
16  final class PageImpl<T> extends Page<T>
17  {
18      public static final String NEXT_REL = "next";
19      public static final String PREVIOUS_REL = "prev";
20      
21      private final PageReference<T> reference;
22      private final Option<URI> previousUri;
23      private final Option<URI> nextUri;
24      
25      PageImpl(PageReference<T> reference, Links links, Iterable<T> items, int count, PageReader<T> reader)
26      {
27          super(items, count, reader);
28          this.reference = checkNotNull(reference, "reference");
29          checkNotNull(links, "links");
30          this.previousUri = links.getUri(PREVIOUS_REL);
31          this.nextUri = links.getUri(NEXT_REL);
32      }
33  
34      @Override
35      public Option<PageReference<T>> getReference()
36      {
37          return some(reference);
38      }
39      
40      @Override
41      public Option<PageReference<T>> getPrevious()
42      {
43          for (URI p: previousUri)
44          {
45              return some(new PageReference<T>(p,
46                  QueryBounds.offset(getBounds().getOffset() - getBounds().getLimit().getOrElse(size()))
47                      .withLimit(getBounds().getLimit().orElse(some(size()))), reader));
48          }
49          return none();
50      }
51  
52      @Override
53      public Option<PageReference<T>> getNext()
54      {
55          for (URI n: nextUri)
56          {
57              return some(new PageReference<T>(n,
58                  QueryBounds.offset(getBounds().getOffset() + getBounds().getLimit().getOrElse(size()))
59                      .withLimit(getBounds().getLimit().orElse(some(size()))), reader));
60          }
61          return none();
62      }
63      
64      private QueryBounds getBounds()
65      {
66          return reference.getBounds();
67      }
68  }