View Javadoc

1   package com.atlassian.marketplace.client.impl;
2   
3   import com.atlassian.fugue.Iterables;
4   import com.atlassian.fugue.Option;
5   import com.atlassian.marketplace.client.MpacException;
6   import com.atlassian.marketplace.client.api.LicenseTypes;
7   import com.atlassian.marketplace.client.model.LicenseType;
8   import com.atlassian.marketplace.client.util.UriBuilder;
9   
10  import com.google.common.base.Predicate;
11  import com.google.common.collect.ImmutableList;
12  
13  final class LicenseTypesImpl implements LicenseTypes
14  {
15      private final ApiHelper apiHelper;
16      private final InternalModel.MinimalLinks root;
17  
18      LicenseTypesImpl(ApiHelper apiHelper, InternalModel.MinimalLinks root)
19      {
20          this.apiHelper = apiHelper;
21          this.root = root;
22      }
23  
24      @Override
25      public Iterable<LicenseType> getAllLicenseTypes() throws MpacException
26      {
27          UriBuilder uri = licenseTypesBaseUri();
28          InternalModel.LicenseTypes collectionRep =
29              apiHelper.getEntity(uri.build(), InternalModel.LicenseTypes.class);
30          return ImmutableList.copyOf(collectionRep.getItems());
31      }
32      
33      @Override
34      public Option<LicenseType> getByKey(final String licenseTypeKey) throws MpacException
35      {
36          return Iterables.findFirst(getAllLicenseTypes(), new Predicate<LicenseType>()
37              {
38                  public boolean apply(LicenseType l)
39                  {
40                      return l.getKey().equals(licenseTypeKey);
41                  }
42              });
43      }
44      
45      private UriBuilder licenseTypesBaseUri() throws MpacException
46      {
47          return UriBuilder.fromUri(apiHelper.requireLinkUri(root.getLinks(), "licenseTypes", root.getClass()));
48      }
49  }