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 not equal to. 085 * 086 * @param value the equal to bind value 087 * @return the root query bean instance 088 */ 089 public final R notEqualTo(T value) { 090 expr().ne(_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 ne(T value) { 101 expr().ne(_name, value); 102 return _root; 103 } 104 105 /** 106 * Is in a list of values. 107 * 108 * @param values the list of values for the predicate 109 * @return the root query bean instance 110 */ 111 @SafeVarargs 112 public final R in(T... values) { 113 expr().in(_name, (Object[]) values); 114 return _root; 115 } 116 117 /** 118 * In where null or empty values means that no predicate is added to the query. 119 * <p> 120 * That is, only add the IN predicate if the values are not null or empty. 121 * <p> 122 * Without this we typically need to code an <code>if</code> block to only add 123 * the IN predicate if the collection is not empty like: 124 * </p> 125 * 126 * <h3>Without inOrEmpty()</h3> 127 * <pre>{@code 128 * 129 * List<String> names = Arrays.asList("foo", "bar"); 130 * 131 * QCustomer query = new QCustomer() 132 * .registered.before(LocalDate.now()) 133 * 134 * // conditionally add the IN expression to the query 135 * if (names != null && !names.isEmpty()) { 136 * query.name.in(names) 137 * } 138 * 139 * query.findList(); 140 * 141 * }</pre> 142 * 143 * <h3>Using inOrEmpty()</h3> 144 * <pre>{@code 145 * 146 * List<String> names = Arrays.asList("foo", "bar"); 147 * 148 * new QCustomer() 149 * .registered.before(LocalDate.now()) 150 * .name.inOrEmpty(names) 151 * .findList(); 152 * 153 * }</pre> 154 */ 155 public final R inOrEmpty(Collection<T> values) { 156 expr().inOrEmpty(_name, values); 157 return _root; 158 } 159 160 /** 161 * Is NOT in a list of values. 162 * 163 * @param values the list of values for the predicate 164 * @return the root query bean instance 165 */ 166 @SafeVarargs 167 public final R notIn(T... values) { 168 expr().notIn(_name, (Object[]) values); 169 return _root; 170 } 171 172 /** 173 * Is in a list of values. Synonym for in(). 174 * 175 * @param values the list of values for the predicate 176 * @return the root query bean instance 177 */ 178 @SafeVarargs 179 public final R isIn(T... values) { 180 expr().in(_name, (Object[]) values); 181 return _root; 182 } 183 184 /** 185 * Is in a list of values. 186 * 187 * @param values the list of values for the predicate 188 * @return the root query bean instance 189 */ 190 public final R in(Collection<T> values) { 191 expr().in(_name, values); 192 return _root; 193 } 194 195 /** 196 * Is NOT 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 notIn(Collection<T> values) { 202 expr().notIn(_name, values); 203 return _root; 204 } 205 206 /** 207 * Is in a list of values. Synonym for in(). 208 * 209 * @param values the list of values for the predicate 210 * @return the root query bean instance 211 */ 212 public final R isIn(Collection<T> values) { 213 expr().in(_name, values); 214 return _root; 215 } 216}