Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions src/main/java/com/rethinkdb/ast/ReqlAst.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.rethinkdb.model.OptArgs;
import com.rethinkdb.net.Connection;
import com.rethinkdb.net.Result;
import com.rethinkdb.utils.Types;

import java.lang.reflect.Type;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -95,7 +95,7 @@ public Result<Object> run(Connection conn, Result.FetchMode fetchMode) {
* @return The result of this query
*/
public <T> Result<T> run(Connection conn, Class<T> typeRef) {
return conn.run(this, new OptArgs(), null, new ClassReference<>(typeRef));
return conn.run(this, new OptArgs(), null, Types.of(typeRef));
}

/**
Expand Down Expand Up @@ -135,7 +135,7 @@ public Result<Object> run(Connection conn, OptArgs runOpts, Result.FetchMode fet
* @return The result of this query
*/
public <T> Result<T> run(Connection conn, OptArgs runOpts, Class<T> typeRef) {
return conn.run(this, runOpts, null, new ClassReference<>(typeRef));
return conn.run(this, runOpts, null, Types.of(typeRef));
}

/**
Expand Down Expand Up @@ -163,7 +163,7 @@ public <T> Result<T> run(Connection conn, OptArgs runOpts, TypeReference<T> type
* @return The result of this query
*/
public <T> Result<T> run(Connection conn, Result.FetchMode fetchMode, Class<T> typeRef) {
return conn.run(this, new OptArgs(), fetchMode, new ClassReference<>(typeRef));
return conn.run(this, new OptArgs(), fetchMode, Types.of(typeRef));
}

/**
Expand Down Expand Up @@ -192,7 +192,7 @@ public <T> Result<T> run(Connection conn, Result.FetchMode fetchMode, TypeRefere
* @return The result of this query
*/
public <T> Result<T> run(Connection conn, OptArgs runOpts, Result.FetchMode fetchMode, Class<T> typeRef) {
return conn.run(this, runOpts, fetchMode, new ClassReference<>(typeRef));
return conn.run(this, runOpts, fetchMode, Types.of(typeRef));
}

/**
Expand Down Expand Up @@ -254,7 +254,7 @@ public CompletableFuture<Result<Object>> runAsync(Connection conn, Result.FetchM
* @return The result of this query
*/
public <T> CompletableFuture<Result<T>> runAsync(Connection conn, Class<T> typeRef) {
return conn.runAsync(this, new OptArgs(), null, new ClassReference<>(typeRef));
return conn.runAsync(this, new OptArgs(), null, Types.of(typeRef));
}

/**
Expand Down Expand Up @@ -294,7 +294,7 @@ public CompletableFuture<Result<Object>> runAsync(Connection conn, OptArgs runOp
* @return The result of this query
*/
public <T> CompletableFuture<Result<T>> runAsync(Connection conn, OptArgs runOpts, Class<T> typeRef) {
return conn.runAsync(this, runOpts, null, new ClassReference<>(typeRef));
return conn.runAsync(this, runOpts, null, Types.of(typeRef));
}

/**
Expand Down Expand Up @@ -322,7 +322,7 @@ public <T> CompletableFuture<Result<T>> runAsync(Connection conn, OptArgs runOpt
* @return The result of this query
*/
public <T> CompletableFuture<Result<T>> runAsync(Connection conn, Result.FetchMode fetchMode, Class<T> typeRef) {
return conn.runAsync(this, new OptArgs(), fetchMode, new ClassReference<>(typeRef));
return conn.runAsync(this, new OptArgs(), fetchMode, Types.of(typeRef));
}

/**
Expand Down Expand Up @@ -351,7 +351,7 @@ public <T> CompletableFuture<Result<T>> runAsync(Connection conn, Result.FetchMo
* @return The result of this query
*/
public <T> CompletableFuture<Result<T>> runAsync(Connection conn, OptArgs runOpts, Result.FetchMode fetchMode, Class<T> typeRef) {
return conn.runAsync(this, runOpts, fetchMode, new ClassReference<>(typeRef));
return conn.runAsync(this, runOpts, fetchMode, Types.of(typeRef));
}

/**
Expand Down Expand Up @@ -438,22 +438,4 @@ private void astToString(StringBuilder builder, String name, String indent, bool
}
}
}

/**
* A TypeReference that accepts an class instead of compiler type information.
*
* @param <T> the type referred to.
*/
private static class ClassReference<T> extends TypeReference<T> {
private Class<T> c;

ClassReference(Class<T> c) {
this.c = c;
}

@Override
public Type getType() {
return c;
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/rethinkdb/net/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import com.fasterxml.jackson.databind.JavaType;
import com.rethinkdb.RethinkDB;
import com.rethinkdb.gen.exc.ReqlDriverError;
import com.rethinkdb.utils.Types;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.time.OffsetDateTime;
import java.util.*;

public class Util {
private static final TypeReference<Map<String, Object>> mapTypeRef = new TypeReference<Map<String, Object>>() {};
private static final TypeReference<Map<String, Object>> mapTypeRef = Types.mapOf(String.class, Object.class);

private Util() {}

Expand Down
131 changes: 131 additions & 0 deletions src/main/java/com/rethinkdb/utils/Types.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.rethinkdb.utils;

import com.fasterxml.jackson.core.type.TypeReference;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.stream.Collectors;

/**
* An utility class that can create simple generic type references without having to grab information from the compiler.
*/
public class Types {
private Types() {
}

public static <T> TypeReference<T> of(Class<T> type) {
return new BuiltTypeRef<>(Objects.requireNonNull(type, "type"));
}

public static <T> TypeReference<List<T>> listOf(Class<T> type) {
return new BuiltTypeRef<>(
new BuiltParametrizedType(List.class, Objects.requireNonNull(type, "type"))
);
}

public static <T> TypeReference<List<T>> listOf(TypeReference<T> type) {
return new BuiltTypeRef<>(
new BuiltParametrizedType(List.class, Objects.requireNonNull(type, "type").getType())
);
}

public static <T> TypeReference<Set<T>> setOf(Class<T> type) {
return new BuiltTypeRef<>(
new BuiltParametrizedType(Set.class, Objects.requireNonNull(type, "type"))
);
}

public static <T> TypeReference<Set<T>> setOf(TypeReference<T> type) {
return new BuiltTypeRef<>(
new BuiltParametrizedType(Set.class, Objects.requireNonNull(type, "type").getType())
);
}

public static <K, V> TypeReference<Map<K, V>> mapOf(Class<K> keyType, Class<V> valueType) {
return new BuiltTypeRef<>(
new BuiltParametrizedType(Map.class, Objects.requireNonNull(keyType, "keyType"), Objects.requireNonNull(valueType, "valueType"))
);
}

public static <K, V> TypeReference<Map<K, V>> mapOf(TypeReference<K> keyType, Class<V> valueType) {
return new BuiltTypeRef<>(
new BuiltParametrizedType(Map.class, Objects.requireNonNull(keyType, "keyType").getType(), Objects.requireNonNull(valueType, "valueType"))
);
}

public static <K, V> TypeReference<Map<K, V>> mapOf(Class<K> keyType, TypeReference<V> valueType) {
return new BuiltTypeRef<>(
new BuiltParametrizedType(Map.class, Objects.requireNonNull(keyType, "keyType"), Objects.requireNonNull(valueType, "valueType").getType())
);
}

public static <K, V> TypeReference<Map<K, V>> mapOf(TypeReference<K> keyType, TypeReference<V> valueType) {
return new BuiltTypeRef<>(
new BuiltParametrizedType(Map.class, Objects.requireNonNull(keyType, "keyType").getType(), Objects.requireNonNull(valueType, "valueType").getType())
);
}

private static class BuiltTypeRef<T> extends TypeReference<T> {
private final Type type;

private BuiltTypeRef(Type type) {
this.type = type;
}

@Override
public Type getType() {
return type;
}
}

private static class BuiltParametrizedType implements ParameterizedType {
private final Class<?> type;
private final Type[] params;

public BuiltParametrizedType(Class<?> type, Type... params) {
if (params.length != type.getTypeParameters().length) {
throw new MalformedParameterizedTypeException();
}
this.type = type;
this.params = params;
}

@Override
public Type[] getActualTypeArguments() {
return params.clone();
}

@Override
public Type getRawType() {
return type;
}

@Override
public Type getOwnerType() {
return type.getDeclaringClass();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ParameterizedType)) return false;
ParameterizedType that = (ParameterizedType) o;
return Objects.equals(type, that.getRawType()) &&
Objects.equals(type.getDeclaringClass(), that.getOwnerType()) &&
Arrays.equals(params, that.getActualTypeArguments());
}

@Override
public int hashCode() {
return Arrays.hashCode(params) ^ Objects.hashCode(type) ^ Objects.hashCode(type.getDeclaringClass());
}

@Override
public String toString() {
return type.getName() + Arrays.stream(params).map(Type::getTypeName)
.collect(Collectors.joining(", ", "<", ">"));
}
}
}
Loading