Use Q type parameter in MongodbQuery for subclass

This commit is contained in:
Timo Westkämper 2015-01-27 22:25:51 +02:00
parent cfa11f5095
commit eaa57555a4
6 changed files with 35 additions and 33 deletions

View File

@ -28,19 +28,19 @@ import com.querydsl.core.types.PredicateOperation;
*
* @param <K>
*/
public class AnyEmbeddedBuilder<K> {
public class AnyEmbeddedBuilder<Q extends MongodbQuery<Q,K>, K> {
private final QueryMixin<MongodbQuery<K>> queryMixin;
private final QueryMixin<Q> queryMixin;
private final Path<? extends Collection<?>> collection;
public AnyEmbeddedBuilder(QueryMixin<MongodbQuery<K>> queryMixin,
public AnyEmbeddedBuilder(QueryMixin<Q> queryMixin,
Path<? extends Collection<?>> collection) {
this.queryMixin = queryMixin;
this.collection = collection;
}
public MongodbQuery<K> on(Predicate... conditions) {
public Q on(Predicate... conditions) {
return queryMixin.where(PredicateOperation.create(
MongodbOps.ELEM_MATCH, collection, ExpressionUtils.allOf(conditions)));
}

View File

@ -24,24 +24,25 @@ import com.querydsl.core.types.Predicate;
*
* @author tiwe
*
* @param <Q>
* @param <K>
* @param <T>
*/
public class JoinBuilder<K, T> {
public class JoinBuilder<Q extends MongodbQuery<Q,K>, K, T> {
private final QueryMixin<MongodbQuery<K>> queryMixin;
private final QueryMixin<Q> queryMixin;
private final Path<?> ref;
private final Path<T> target;
public JoinBuilder(QueryMixin<MongodbQuery<K>> queryMixin, Path<?> ref, Path<T> target) {
public JoinBuilder(QueryMixin<Q> queryMixin, Path<?> ref, Path<T> target) {
this.queryMixin = queryMixin;
this.ref = ref;
this.target = target;
}
public MongodbQuery<K> on(Predicate... conditions) {
public Q on(Predicate... conditions) {
queryMixin.addJoin(JoinType.JOIN, ExpressionUtils.as((Path)ref, target));
queryMixin.on(conditions);
return queryMixin.getSelf();

View File

@ -35,16 +35,17 @@ import com.querydsl.core.types.path.CollectionPathBase;
*
* @author laimw
*
* @param <Q>
* @param <K>
*/
public abstract class MongodbQuery<K> implements SimpleQuery<MongodbQuery<K>>, SimpleProjectable<K> {
public abstract class MongodbQuery<Q extends MongodbQuery<Q,K>, K> implements SimpleQuery<Q>, SimpleProjectable<K> {
@SuppressWarnings("serial")
private static class NoResults extends RuntimeException {}
private final MongodbSerializer serializer;
private final QueryMixin<MongodbQuery<K>> queryMixin;
private final QueryMixin<Q> queryMixin;
private final DBCollection collection;
@ -60,7 +61,7 @@ public abstract class MongodbQuery<K> implements SimpleQuery<MongodbQuery<K>>, S
* @param serializer
*/
public MongodbQuery(DBCollection collection, Function<DBObject, K> transformer, MongodbSerializer serializer) {
this.queryMixin = new QueryMixin<MongodbQuery<K>>(this, new DefaultQueryMetadata().noValidate(), false);
this.queryMixin = new QueryMixin<Q>((Q)this, new DefaultQueryMetadata().noValidate(), false);
this.transformer = transformer;
this.collection = collection;
this.serializer = serializer;
@ -73,8 +74,8 @@ public abstract class MongodbQuery<K> implements SimpleQuery<MongodbQuery<K>>, S
* @param target
* @return
*/
public <T> JoinBuilder<K,T> join(Path<T> ref, Path<T> target) {
return new JoinBuilder<K,T>(queryMixin, ref, target);
public <T> JoinBuilder<Q, K,T> join(Path<T> ref, Path<T> target) {
return new JoinBuilder<Q, K,T>(queryMixin, ref, target);
}
/**
@ -84,8 +85,8 @@ public abstract class MongodbQuery<K> implements SimpleQuery<MongodbQuery<K>>, S
* @param target
* @return
*/
public <T> JoinBuilder<K,T> join(CollectionPathBase<?,T,?> ref, Path<T> target) {
return new JoinBuilder<K,T>(queryMixin, ref, target);
public <T> JoinBuilder<Q, K,T> join(CollectionPathBase<?,T,?> ref, Path<T> target) {
return new JoinBuilder<Q, K,T>(queryMixin, ref, target);
}
/**
@ -95,8 +96,8 @@ public abstract class MongodbQuery<K> implements SimpleQuery<MongodbQuery<K>>, S
* @param target
* @return
*/
public <T> AnyEmbeddedBuilder<K> anyEmbedded(Path<? extends Collection<T>> collection, Path<T> target) {
return new AnyEmbeddedBuilder<K>(queryMixin, collection);
public <T> AnyEmbeddedBuilder<Q, K> anyEmbedded(Path<? extends Collection<T>> collection, Path<T> target) {
return new AnyEmbeddedBuilder<Q, K>(queryMixin, collection);
}
protected abstract DBCollection getCollection(Class<?> type);
@ -170,45 +171,45 @@ public abstract class MongodbQuery<K> implements SimpleQuery<MongodbQuery<K>>, S
}
@Override
public MongodbQuery<K> distinct() {
public Q distinct() {
return queryMixin.distinct();
}
public MongodbQuery<K> where(Predicate e) {
public Q where(Predicate e) {
return queryMixin.where(e);
}
@Override
public MongodbQuery<K> where(Predicate... e) {
public Q where(Predicate... e) {
return queryMixin.where(e);
}
@Override
public MongodbQuery<K> limit(long limit) {
public Q limit(long limit) {
return queryMixin.limit(limit);
}
@Override
public MongodbQuery<K> offset(long offset) {
public Q offset(long offset) {
return queryMixin.offset(offset);
}
@Override
public MongodbQuery<K> restrict(QueryModifiers modifiers) {
public Q restrict(QueryModifiers modifiers) {
return queryMixin.restrict(modifiers);
}
public MongodbQuery<K> orderBy(OrderSpecifier<?> o) {
public Q orderBy(OrderSpecifier<?> o) {
return queryMixin.orderBy(o);
}
@Override
public MongodbQuery<K> orderBy(OrderSpecifier<?>... o) {
public Q orderBy(OrderSpecifier<?>... o) {
return queryMixin.orderBy(o);
}
@Override
public <T> MongodbQuery<K> set(ParamExpression<T> param, T value) {
public <T> Q set(ParamExpression<T> param, T value) {
return queryMixin.set(param, value);
}

View File

@ -32,7 +32,7 @@ import com.querydsl.core.types.EntityPath;
* @author tiwe
*
*/
public class MorphiaQuery<K> extends MongodbQuery<K> {
public class MorphiaQuery<K> extends MongodbQuery<MorphiaQuery<K>, K> {
private final EntityCache cache;

View File

@ -145,11 +145,11 @@ public class JoinTest {
.singleResult().getFirstName());
}
private MongodbQuery<User> query() {
private MorphiaQuery<User> query() {
return new MorphiaQuery<User>(morphia, ds, user);
}
private MongodbQuery<User> where(Predicate ... e) {
private MorphiaQuery<User> where(Predicate ... e) {
return query().where(e);
}
}

View File

@ -381,7 +381,7 @@ public class MongodbQueryTest {
@Test
public void UniqueResultAndLimitAndOffset() {
MongodbQuery<User> q = query().where(user.firstName.startsWith("Ja")).orderBy(user.age.asc());
MorphiaQuery<User> q = query().where(user.firstName.startsWith("Ja")).orderBy(user.age.asc());
assertEquals(4, q.list().size());
assertEquals(u1, q.list().get(0));
}
@ -485,11 +485,11 @@ public class MongodbQueryTest {
assertQuery(where(e).orderBy(orderBy), expected);
}
private <T> MongodbQuery<T> where(EntityPath<T> entity, Predicate... e) {
private <T> MorphiaQuery<T> where(EntityPath<T> entity, Predicate... e) {
return new MorphiaQuery<T>(morphia, ds, entity).where(e);
}
private MongodbQuery<User> where(Predicate ... e) {
private MorphiaQuery<User> where(Predicate ... e) {
return query().where(e);
}
@ -501,7 +501,7 @@ public class MongodbQueryTest {
return new MorphiaQuery<T>(morphia, ds, path);
}
private void assertQuery(MongodbQuery<User> query, User ... expected ) {
private void assertQuery(MorphiaQuery<User> query, User ... expected ) {
//System.out.println(query.toString());
List<User> results = query.list();