View Javadoc

1   package com.atlassian.marketplace.client.api;
2   
3   import static com.atlassian.marketplace.client.api.QueryProperties.describeOptBoolean;
4   import static com.atlassian.marketplace.client.api.QueryProperties.describeParams;
5   import static com.google.common.base.Preconditions.checkNotNull;
6   
7   /**
8    * Encapsulates search parameters that can be passed to {@link Vendors} to determine what
9    * subset of vendors you are interested in.  Besides the usual {@link QueryBounds} parameters,
10   * you can specify whether to query all vendors (the default) or only the vendors that are
11   * associated with the current authenticated user.
12   * @since 2.0.0
13   */
14  public final class VendorQuery implements QueryProperties.Bounds
15  {
16      private static final VendorQuery DEFAULT_QUERY = builder().build();
17      
18      private final QueryBounds bounds;
19      private final boolean forThisUserOnly;
20      
21      /**
22       * Returns a new {@link Builder} for constructing a VendorQuery.
23       */
24      public static Builder builder()
25      {
26          return new Builder();
27      }
28  
29      /**
30       * Returns a VendorQuery with default criteria.
31       */
32      public static VendorQuery any()
33      {
34          return DEFAULT_QUERY;
35      }
36      
37      /**
38       * Returns a new {@link Builder} for constructing a VendorQuery based on an existing VendorQuery.
39       */
40      public static Builder builder(VendorQuery query)
41      {
42          return builder()
43              .bounds(query.getBounds())
44              .forThisUserOnly(query.isForThisUserOnly());
45      }
46  
47      private VendorQuery(Builder builder)
48      {
49          bounds = builder.bounds;
50          forThisUserOnly = builder.forThisUserOnly;
51      }
52      
53      @Override
54      public QueryBounds getBounds()
55      {
56          return bounds;
57      }
58      
59      /**
60       * True if the client is querying only the list of vendors that are associated with the
61       * current authenticated user.
62       * @see Builder#forThisUserOnly(boolean)
63       */
64      public boolean isForThisUserOnly()
65      {
66          return forThisUserOnly;
67      }
68  
69      @SuppressWarnings("unchecked")
70      @Override
71      public String toString()
72      {
73          return describeParams("VendorQuery",
74              describeOptBoolean("forThisUserOnly", forThisUserOnly),
75              bounds.describe()
76          );
77      }
78      
79      @Override
80      public boolean equals(Object other)
81      {
82          return (other instanceof VendorQuery) ? toString().equals(other.toString()) : false;
83      }
84  
85      @Override
86      public int hashCode()
87      {
88          return toString().hashCode();
89      }
90  
91      /**
92       * Builder class for {@link VendorQuery}.  Use {@link VendorQuery#builder()} to create an instance. 
93       */
94      public static class Builder implements QueryBuilderProperties.Bounds<Builder>
95      {
96          private QueryBounds bounds = QueryBounds.defaultBounds();
97          private boolean forThisUserOnly;
98          
99          /**
100          * Returns an immutable {@link VendorQuery} based on the current builder properties.
101          */
102         public VendorQuery build()
103         {
104             return new VendorQuery(this);
105         }
106         
107         @Override
108         public Builder bounds(QueryBounds bounds)
109         {
110             this.bounds = checkNotNull(bounds);
111             return this;
112         }
113         
114         /**
115          * Specifies whether to query only the vendor(s) associated with the current authenticated user
116          * or all vendors.
117          * @param forThisUserOnly  true to query only the vendor(s) associated with the current
118          *   authenticated user; false (the default) to query all vendors.
119          * @return  the same query builder
120          * @see VendorQuery#isForThisUserOnly()
121          */
122         public Builder forThisUserOnly(boolean forThisUserOnly)
123         {
124             this.forThisUserOnly = forThisUserOnly;
125             return this;
126         }
127     }
128 }