001package io.ebean.typequery;
002
003import java.util.Collection;
004
005/**
006 * Base property for types that primarily have equal to.
007 *
008 * @param <R> the root query bean type
009 * @param <T> the number type
010 */
011public abstract class PBaseValueEqual<R, T> extends TQPropertyBase<R> {
012
013  /**
014   * Construct with a property name and root instance.
015   *
016   * @param name property name
017   * @param root the root query bean instance
018   */
019  public PBaseValueEqual(String name, R root) {
020    super(name, root);
021  }
022
023  /**
024   * Construct with additional path prefix.
025   */
026  public PBaseValueEqual(String name, R root, String prefix) {
027    super(name, root, prefix);
028  }
029
030  /**
031   * Set the property as the map key for a <code>findMap</code> query.
032   *
033   * <pre>{@code
034   *
035   *   Map<String, Customer> map =
036   *     new QCustomer()
037   *       .organisation.id.equalTo(42)
038   *       .email.asMapKey() // email property as map key
039   *       .findMap();
040   *
041   * }</pre>
042   *
043   * @return the root query bean instance
044   */
045  public final R asMapKey() {
046    expr().setMapKey(_name);
047    return _root;
048  }
049
050  /**
051   * Is equal to or Null.
052   *
053   * @param value the equal to bind value
054   * @return the root query bean instance
055   */
056  public final R equalToOrNull(T value) {
057    expr().eqOrNull(_name, value);
058    return _root;
059  }
060
061  /**
062   * Is equal to.
063   *
064   * @param value the equal to bind value
065   * @return the root query bean instance
066   */
067  public final R equalTo(T value) {
068    expr().eq(_name, value);
069    return _root;
070  }
071
072  /**
073   * Is equal to.
074   *
075   * @param value the equal to bind value
076   * @return the root query bean instance
077   */
078  public final R eq(T value) {
079    expr().eq(_name, value);
080    return _root;
081  }
082
083  /**
084   * Is equal to or Null.
085   *
086   * @param value the equal to bind value
087   * @return the root query bean instance
088   */
089  public final R eqOrNull(T value) {
090    expr().eqOrNull(_name, value);
091    return _root;
092  }
093
094  /**
095   * Is not equal to.
096   *
097   * @param value the equal to bind value
098   * @return the root query bean instance
099   */
100  public final R notEqualTo(T value) {
101    expr().ne(_name, value);
102    return _root;
103  }
104
105  /**
106   * Is not equal to.
107   *
108   * @param value the equal to bind value
109   * @return the root query bean instance
110   */
111  public final R ne(T value) {
112    expr().ne(_name, value);
113    return _root;
114  }
115
116  /**
117   * Is in a list of values.
118   *
119   * @param values the list of values for the predicate
120   * @return the root query bean instance
121   */
122  @SafeVarargs
123  public final R in(T... values) {
124    expr().in(_name, (Object[]) values);
125    return _root;
126  }
127
128  /**
129   * In where null or empty values means that no predicate is added to the query.
130   * <p>
131   * That is, only add the IN predicate if the values are not null or empty.
132   * <p>
133   * Without this we typically need to code an <code>if</code> block to only add
134   * the IN predicate if the collection is not empty like:
135   * </p>
136   *
137   * <h3>Without inOrEmpty()</h3>
138   * <pre>{@code
139   *
140   *   List<String> names = Arrays.asList("foo", "bar");
141   *
142   *   QCustomer query = new QCustomer()
143   *       .registered.before(LocalDate.now())
144   *
145   *   // conditionally add the IN expression to the query
146   *   if (names != null && !names.isEmpty()) {
147   *       query.name.in(names)
148   *   }
149   *
150   *   query.findList();
151   *
152   * }</pre>
153   *
154   * <h3>Using inOrEmpty()</h3>
155   * <pre>{@code
156   *
157   *   List<String> names = Arrays.asList("foo", "bar");
158   *
159   *   new QCustomer()
160   *       .registered.before(LocalDate.now())
161   *       .name.inOrEmpty(names)
162   *       .findList();
163   *
164   * }</pre>
165   */
166  public final R inOrEmpty(Collection<T> values) {
167    expr().inOrEmpty(_name, values);
168    return _root;
169  }
170
171  /**
172   * Is NOT in a list of values.
173   *
174   * @param values the list of values for the predicate
175   * @return the root query bean instance
176   */
177  @SafeVarargs
178  public final R notIn(T... values) {
179    expr().notIn(_name, (Object[]) values);
180    return _root;
181  }
182
183  /**
184   * Is in a list of values. Synonym for in().
185   *
186   * @param values the list of values for the predicate
187   * @return the root query bean instance
188   */
189  @SafeVarargs
190  public final R isIn(T... values) {
191    expr().in(_name, (Object[]) values);
192    return _root;
193  }
194
195  /**
196   * Is in a list of values.
197   *
198   * @param values the list of values for the predicate
199   * @return the root query bean instance
200   */
201  public final R in(Collection<T> values) {
202    expr().in(_name, values);
203    return _root;
204  }
205
206  /**
207   * Is NOT in a list of values.
208   *
209   * @param values the list of values for the predicate
210   * @return the root query bean instance
211   */
212  public final R notIn(Collection<T> values) {
213    expr().notIn(_name, values);
214    return _root;
215  }
216
217  /**
218   * Is in a list of values. Synonym for in().
219   *
220   * @param values the list of values for the predicate
221   * @return the root query bean instance
222   */
223  public final R isIn(Collection<T> values) {
224    expr().in(_name, values);
225    return _root;
226  }
227}