Search in sources :

Example 1 with VisibleForTesting

use of com.google_voltpatches.common.annotations.VisibleForTesting in project voltdb by VoltDB.

the class TypeToken method toGenericType.

/**
   * Returns the type token representing the generic type declaration of {@code cls}. For example:
   * {@code TypeToken.getGenericType(Iterable.class)} returns {@code Iterable<T>}.
   *
   * <p>If {@code cls} isn't parameterized and isn't a generic array, the type token of the class is
   * returned.
   */
@VisibleForTesting
static <T> TypeToken<? extends T> toGenericType(Class<T> cls) {
    if (cls.isArray()) {
        Type arrayOfGenericType = Types.newArrayType(// If we are passed with int[].class, don't turn it to GenericArrayType
        toGenericType(cls.getComponentType()).runtimeType);
        // array is covariant
        @SuppressWarnings("unchecked") TypeToken<? extends T> result = (TypeToken<? extends T>) of(arrayOfGenericType);
        return result;
    }
    TypeVariable<Class<T>>[] typeParams = cls.getTypeParameters();
    Type ownerType = cls.isMemberClass() && !Modifier.isStatic(cls.getModifiers()) ? toGenericType(cls.getEnclosingClass()).runtimeType : null;
    if ((typeParams.length > 0) || ((ownerType != null) && ownerType != cls.getEnclosingClass())) {
        // Like, it's Iterable<T> for Iterable.class
        @SuppressWarnings("unchecked") TypeToken<? extends T> type = (TypeToken<? extends T>) of(Types.newParameterizedTypeWithOwner(ownerType, cls, typeParams));
        return type;
    } else {
        return of(cls);
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) VisibleForTesting(com.google_voltpatches.common.annotations.VisibleForTesting)

Example 2 with VisibleForTesting

use of com.google_voltpatches.common.annotations.VisibleForTesting in project voltdb by VoltDB.

the class BloomFilter method create.

@VisibleForTesting
static <T> BloomFilter<T> create(Funnel<? super T> funnel, long expectedInsertions, double fpp, Strategy strategy) {
    checkNotNull(funnel);
    checkArgument(expectedInsertions >= 0, "Expected insertions (%s) must be >= 0", expectedInsertions);
    checkArgument(fpp > 0.0, "False positive probability (%s) must be > 0.0", fpp);
    checkArgument(fpp < 1.0, "False positive probability (%s) must be < 1.0", fpp);
    checkNotNull(strategy);
    if (expectedInsertions == 0) {
        expectedInsertions = 1;
    }
    /*
     * TODO(user): Put a warning in the javadoc about tiny fpp values, since the resulting size
     * is proportional to -log(p), but there is not much of a point after all, e.g.
     * optimalM(1000, 0.0000000000000001) = 76680 which is less than 10kb. Who cares!
     */
    long numBits = optimalNumOfBits(expectedInsertions, fpp);
    int numHashFunctions = optimalNumOfHashFunctions(expectedInsertions, numBits);
    try {
        return new BloomFilter<T>(new BitArray(numBits), numHashFunctions, funnel, strategy);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("Could not create BloomFilter of " + numBits + " bits", e);
    }
}
Also used : BitArray(com.google_voltpatches.common.hash.BloomFilterStrategies.BitArray) VisibleForTesting(com.google_voltpatches.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google_voltpatches.common.annotations.VisibleForTesting)2 BitArray (com.google_voltpatches.common.hash.BloomFilterStrategies.BitArray)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 WildcardType (java.lang.reflect.WildcardType)1