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