Search in sources :

Example 1 with Nullable

use of javax.annotation_voltpatches.Nullable in project voltdb by VoltDB.

the class LocalCache method loadAll.

/**
   * Returns the result of calling {@link CacheLoader#loadAll}, or null if {@code loader} doesn't
   * implement {@code loadAll}.
   */
@Nullable
Map<K, V> loadAll(Set<? extends K> keys, CacheLoader<? super K, V> loader) throws ExecutionException {
    checkNotNull(loader);
    checkNotNull(keys);
    Stopwatch stopwatch = Stopwatch.createStarted();
    Map<K, V> result;
    boolean success = false;
    try {
        // safe since all keys extend K
        @SuppressWarnings("unchecked") Map<K, V> map = (Map<K, V>) loader.loadAll(keys);
        result = map;
        success = true;
    } catch (UnsupportedLoadingOperationException e) {
        success = true;
        throw e;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new ExecutionException(e);
    } catch (RuntimeException e) {
        throw new UncheckedExecutionException(e);
    } catch (Exception e) {
        throw new ExecutionException(e);
    } catch (Error e) {
        throw new ExecutionError(e);
    } finally {
        if (!success) {
            globalStatsCounter.recordLoadException(stopwatch.elapsed(NANOSECONDS));
        }
    }
    if (result == null) {
        globalStatsCounter.recordLoadException(stopwatch.elapsed(NANOSECONDS));
        throw new InvalidCacheLoadException(loader + " returned null map from loadAll");
    }
    stopwatch.stop();
    // TODO(fry): batch by segment
    boolean nullsPresent = false;
    for (Map.Entry<K, V> entry : result.entrySet()) {
        K key = entry.getKey();
        V value = entry.getValue();
        if (key == null || value == null) {
            // delay failure until non-null entries are stored
            nullsPresent = true;
        } else {
            put(key, value);
        }
    }
    if (nullsPresent) {
        globalStatsCounter.recordLoadException(stopwatch.elapsed(NANOSECONDS));
        throw new InvalidCacheLoadException(loader + " returned null keys or values from loadAll");
    }
    // TODO(fry): record count of loaded entries
    globalStatsCounter.recordLoadSuccess(stopwatch.elapsed(NANOSECONDS));
    return result;
}
Also used : ExecutionError(com.google_voltpatches.common.util.concurrent.ExecutionError) UncheckedExecutionException(com.google_voltpatches.common.util.concurrent.UncheckedExecutionException) Stopwatch(com.google_voltpatches.common.base.Stopwatch) ExecutionError(com.google_voltpatches.common.util.concurrent.ExecutionError) UncheckedExecutionException(com.google_voltpatches.common.util.concurrent.UncheckedExecutionException) UnsupportedLoadingOperationException(com.google_voltpatches.common.cache.CacheLoader.UnsupportedLoadingOperationException) InvalidCacheLoadException(com.google_voltpatches.common.cache.CacheLoader.InvalidCacheLoadException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) InvalidCacheLoadException(com.google_voltpatches.common.cache.CacheLoader.InvalidCacheLoadException) UncheckedExecutionException(com.google_voltpatches.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) ImmutableMap(com.google_voltpatches.common.collect.ImmutableMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) AbstractMap(java.util.AbstractMap) UnsupportedLoadingOperationException(com.google_voltpatches.common.cache.CacheLoader.UnsupportedLoadingOperationException) Nullable(javax.annotation_voltpatches.Nullable)

Example 2 with Nullable

use of javax.annotation_voltpatches.Nullable in project voltdb by VoltDB.

the class TypeToken method getGenericSuperclass.

/**
   * Returns the generic superclass of this type or {@code null} if the type represents
   * {@link Object} or an interface. This method is similar but different from
   * {@link Class#getGenericSuperclass}. For example, {@code new TypeToken<StringArrayList>()
   * {}.getGenericSuperclass()} will return {@code new TypeToken<ArrayList<String>>() {}}; while
   * {@code StringArrayList.class.getGenericSuperclass()} will return {@code ArrayList<E>}, where
   * {@code E} is the type variable declared by class {@code ArrayList}.
   *
   * <p>If this type is a type variable or wildcard, its first upper bound is examined and returned
   * if the bound is a class or extends from a class. This means that the returned type could be a
   * type variable too.
   */
@Nullable
final TypeToken<? super T> getGenericSuperclass() {
    if (runtimeType instanceof TypeVariable) {
        // First bound is always the super class, if one exists.
        return boundAsSuperclass(((TypeVariable<?>) runtimeType).getBounds()[0]);
    }
    if (runtimeType instanceof WildcardType) {
        // wildcard has one and only one upper bound.
        return boundAsSuperclass(((WildcardType) runtimeType).getUpperBounds()[0]);
    }
    Type superclass = getRawType().getGenericSuperclass();
    if (superclass == null) {
        return null;
    }
    // super class of T
    @SuppressWarnings("unchecked") TypeToken<? super T> superToken = (TypeToken<? super T>) resolveSupertype(superclass);
    return superToken;
}
Also used : WildcardType(java.lang.reflect.WildcardType) 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) Nullable(javax.annotation_voltpatches.Nullable)

Example 3 with Nullable

use of javax.annotation_voltpatches.Nullable in project voltdb by VoltDB.

the class Throwables method getJLA.

/**
   * Returns the JavaLangAccess class that is present in all Sun JDKs. It is not whitelisted for
   * AppEngine, and not present in non-Sun JDKs.
   */
// java.lang.reflect
@GwtIncompatible
@Nullable
private static Object getJLA() {
    try {
        /*
       * We load sun.misc.* classes using reflection since Android doesn't support these classes and
       * would result in compilation failure if we directly refer to these classes.
       */
        Class<?> sharedSecrets = Class.forName(SHARED_SECRETS_CLASSNAME, false, null);
        Method langAccess = sharedSecrets.getMethod("getJavaLangAccess");
        return langAccess.invoke(null);
    } catch (ThreadDeath death) {
        throw death;
    } catch (Throwable t) {
        /*
       * This is not one of AppEngine's whitelisted classes, so even in Sun JDKs, this can fail with
       * a NoClassDefFoundError. Other apps might deny access to sun.misc packages.
       */
        return null;
    }
}
Also used : Method(java.lang.reflect.Method) GwtIncompatible(com.google_voltpatches.common.annotations.GwtIncompatible) Nullable(javax.annotation_voltpatches.Nullable)

Example 4 with Nullable

use of javax.annotation_voltpatches.Nullable in project voltdb by VoltDB.

the class Finalizer method getInheritableThreadLocalsField.

@Nullable
public static Field getInheritableThreadLocalsField() {
    try {
        Field inheritableThreadLocals = Thread.class.getDeclaredField("inheritableThreadLocals");
        inheritableThreadLocals.setAccessible(true);
        return inheritableThreadLocals;
    } catch (Throwable t) {
        logger.log(Level.INFO, "Couldn't access Thread.inheritableThreadLocals. Reference finalizer threads will " + "inherit thread local values.");
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) Nullable(javax.annotation_voltpatches.Nullable)

Example 5 with Nullable

use of javax.annotation_voltpatches.Nullable in project voltdb by VoltDB.

the class InetAddresses method textToNumericFormatV6.

@Nullable
private static byte[] textToNumericFormatV6(String ipString) {
    // An address can have [2..8] colons, and N colons make N+1 parts.
    String[] parts = ipString.split(":", IPV6_PART_COUNT + 2);
    if (parts.length < 3 || parts.length > IPV6_PART_COUNT + 1) {
        return null;
    }
    // Disregarding the endpoints, find "::" with nothing in between.
    // This indicates that a run of zeroes has been skipped.
    int skipIndex = -1;
    for (int i = 1; i < parts.length - 1; i++) {
        if (parts[i].length() == 0) {
            if (skipIndex >= 0) {
                // Can't have more than one ::
                return null;
            }
            skipIndex = i;
        }
    }
    // Number of parts to copy from above/before the "::"
    int partsHi;
    // Number of parts to copy from below/after the "::"
    int partsLo;
    if (skipIndex >= 0) {
        // If we found a "::", then check if it also covers the endpoints.
        partsHi = skipIndex;
        partsLo = parts.length - skipIndex - 1;
        if (parts[0].length() == 0 && --partsHi != 0) {
            // ^: requires ^::
            return null;
        }
        if (parts[parts.length - 1].length() == 0 && --partsLo != 0) {
            // :$ requires ::$
            return null;
        }
    } else {
        // Otherwise, allocate the entire address to partsHi. The endpoints
        // could still be empty, but parseHextet() will check for that.
        partsHi = parts.length;
        partsLo = 0;
    }
    // If we found a ::, then we must have skipped at least one part.
    // Otherwise, we must have exactly the right number of parts.
    int partsSkipped = IPV6_PART_COUNT - (partsHi + partsLo);
    if (!(skipIndex >= 0 ? partsSkipped >= 1 : partsSkipped == 0)) {
        return null;
    }
    // Now parse the hextets into a byte array.
    ByteBuffer rawBytes = ByteBuffer.allocate(2 * IPV6_PART_COUNT);
    try {
        for (int i = 0; i < partsHi; i++) {
            rawBytes.putShort(parseHextet(parts[i]));
        }
        for (int i = 0; i < partsSkipped; i++) {
            rawBytes.putShort((short) 0);
        }
        for (int i = partsLo; i > 0; i--) {
            rawBytes.putShort(parseHextet(parts[parts.length - i]));
        }
    } catch (NumberFormatException ex) {
        return null;
    }
    return rawBytes.array();
}
Also used : ByteBuffer(java.nio.ByteBuffer) Nullable(javax.annotation_voltpatches.Nullable)

Aggregations

Nullable (javax.annotation_voltpatches.Nullable)5 GwtIncompatible (com.google_voltpatches.common.annotations.GwtIncompatible)1 Stopwatch (com.google_voltpatches.common.base.Stopwatch)1 InvalidCacheLoadException (com.google_voltpatches.common.cache.CacheLoader.InvalidCacheLoadException)1 UnsupportedLoadingOperationException (com.google_voltpatches.common.cache.CacheLoader.UnsupportedLoadingOperationException)1 ImmutableMap (com.google_voltpatches.common.collect.ImmutableMap)1 ExecutionError (com.google_voltpatches.common.util.concurrent.ExecutionError)1 UncheckedExecutionException (com.google_voltpatches.common.util.concurrent.UncheckedExecutionException)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 Method (java.lang.reflect.Method)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 ByteBuffer (java.nio.ByteBuffer)1 AbstractMap (java.util.AbstractMap)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1