View Javadoc

1   package com.atlassian.marketplace.client.api;
2   
3   import com.atlassian.fugue.Either;
4   import com.atlassian.fugue.Option;
5   
6   import static com.atlassian.fugue.Option.none;
7   import static com.atlassian.fugue.Option.some;
8   
9   /**
10   * Encapsulates parameters that can be passed to {@link Products#getVersion}.
11   * @since 2.0.0
12   */
13  public final class ProductVersionSpecifier
14  {
15      private final Option<Either<Integer, String>> nameOrBuild;
16      
17      private ProductVersionSpecifier(Option<Either<Integer, String>> nameOrBuild)
18      {
19          this.nameOrBuild = nameOrBuild;
20      }
21      
22      /**
23       * Searches for a product version by build number.
24       */
25      public static ProductVersionSpecifier buildNumber(int buildNumber)
26      {
27          return new ProductVersionSpecifier(some(Either.<Integer, String>left(buildNumber)));
28      }
29  
30      /**
31       * Searches for a product version by name (version string).
32       */
33      public static ProductVersionSpecifier name(String name)
34      {
35          return new ProductVersionSpecifier(some(Either.<Integer, String>right(name)));
36      }
37  
38      /**
39       * Searches for the latest version.
40       */
41      public static ProductVersionSpecifier latest()
42      {
43          return new ProductVersionSpecifier(Option.<Either<Integer, String>>none());
44      }
45      
46      public Option<Integer> getBuildNumber()
47      {
48          for (Either<Integer, String> nb: nameOrBuild)
49          {
50              return nb.left().toOption();
51          }
52          return none();
53      }
54  
55      public Option<String> getName()
56      {
57          for (Either<Integer, String> nb: nameOrBuild)
58          {
59              return nb.right().toOption();
60          }
61          return none();
62      }
63  
64      @Override
65      public String toString()
66      {
67          for (Integer b: getBuildNumber())
68          {
69              return "buildNumber(" + b + ")";            
70          }
71          for (String n: getName())
72          {
73              return "name(" + n + ")";
74          }
75          return "latest";
76      }
77      
78      @Override
79      public boolean equals(Object other)
80      {
81          return (other instanceof ProductVersionSpecifier) && ((ProductVersionSpecifier) other).nameOrBuild.equals(this.nameOrBuild);
82      }
83      
84      @Override
85      public int hashCode()
86      {
87          return nameOrBuild.hashCode();
88      }
89  }