Example usage for com.google.common.base Preconditions checkArgument

List of usage examples for com.google.common.base Preconditions checkArgument

Introduction

In this page you can find the example usage for com.google.common.base Preconditions checkArgument.

Prototype

public static void checkArgument(boolean expression, @Nullable String errorMessageTemplate,
        @Nullable Object... errorMessageArgs) 

Source Link

Document

Ensures the truth of an expression involving one or more parameters to the calling method.

Usage

From source file:org.apache.curator.test.DirectoryUtils.java

public static void deleteDirectoryContents(File directory) throws IOException {
    Preconditions.checkArgument(directory.isDirectory(), "Not a directory: %s", directory);
    File[] files = directory.listFiles();
    if (files == null) {
        throw new IOException("Error listing files for " + directory);
    }/*  ww  w. j  a  v  a  2 s  . co  m*/
    for (File file : files) {
        deleteRecursively(file);
    }
}

From source file:org.opendaylight.protocol.pcep.impl.tls.SslKeyStore.java

/**
 * InputStream instance of key - key location is on classpath or specific path
 *
 * @param filename/*from  w ww  .  j av a  2 s  .  c  o m*/
 *          keystore location
 * @param pathType
 *          keystore location type - "classpath" or "path"
 *
 * @return key as InputStream
 */
public static InputStream asInputStream(final String filename, final PathType pathType) {
    InputStream in;
    switch (pathType) {
    case CLASSPATH:
        in = SslKeyStore.class.getResourceAsStream(filename);
        Preconditions.checkArgument(in != null, "KeyStore file not found: %s", filename);
        break;
    case PATH:
        LOG.debug("Current dir using System: {}", System.getProperty("user.dir"));
        final File keystorefile = new File(filename);
        try {
            in = new FileInputStream(keystorefile);
        } catch (final FileNotFoundException e) {
            throw new IllegalStateException("KeyStore file not found: " + filename, e);
        }
        break;
    default:
        throw new IllegalArgumentException("Unknown path type: " + pathType);
    }
    return in;
}

From source file:net.myrrix.common.LangUtils.java

/**
 * Parses a {@code float} from a {@link String} as if by {@link Float#valueOf(String)}, but disallows special
 * values like {@link Float#NaN}, {@link Float#POSITIVE_INFINITY} and {@link Float#NEGATIVE_INFINITY}.
 *
 * @param s {@link String} to parse//from ww  w  .  j a v  a2  s. c  om
 * @return floating-point value in the {@link String}
 * @throws NumberFormatException if input does not parse as a floating-point value
 * @throws IllegalArgumentException if input is infinite or {@link Float#NaN}
 * @see #parseDouble(String)
 */
public static float parseFloat(String s) {
    float value = Float.parseFloat(s);
    Preconditions.checkArgument(isFinite(value), "Bad value: %s", value);
    return value;
}

From source file:com.cloudera.oryx.common.LangUtils.java

/**
 * Parses a {@code float} from a {@link String} as if by {@link Float#valueOf(String)}, but disallows special
 * values like {@link Float#NaN}, {@link Float#POSITIVE_INFINITY} and {@link Float#NEGATIVE_INFINITY}.
 *
 * @param s {@link String} to parse/*from  www  .jav a 2s .  com*/
 * @return floating-point value in the {@link String}
 * @throws NumberFormatException if input does not parse as a floating-point value
 * @throws IllegalArgumentException if input is infinite or {@link Float#NaN}
 * @see #parseDouble(String)
 */
public static float parseFloat(String s) {
    float value = Float.parseFloat(s);
    Preconditions.checkArgument(Floats.isFinite(value), "Bad value: %s", value);
    return value;
}

From source file:org.asoem.greyfish.utils.math.ApproximationMath.java

/**
 * Gaussian function using {@link #exp}/*w  ww .ja va  2s .  co  m*/
 *
 * @param x x
 * @param norm the normalization factor
 * @param mean the mean
 * @param sigma the standard deviation
 * @return norm * e ^ -((x - mean)^2 / 2 * sigma^2)
 */
public static double gaussian(final double x, final double norm, final double mean, final double sigma) {
    Preconditions.checkArgument(sigma > 0, "Sigma must be strictly positive, but was %s", sigma);
    return gaussianHelper(x - mean, norm, i2s2(sigma));
}

From source file:org.gradle.caching.internal.CompositeBuildCacheService.java

public static BuildCacheService create(@Nullable BuildCacheService pushToCache,
        List<BuildCacheService> pullFromCaches) {
    if (pushToCache != null) {
        Preconditions.checkArgument(pullFromCaches.contains(pushToCache),
                "pushToCache %s must be contained in pullFromCaches", pushToCache.getDescription());
    }//  w w w .  j a  v a 2  s. c o m
    if (pullFromCaches.size() == 1) {
        return CollectionUtils.single(pullFromCaches);
    }
    return new CompositeBuildCacheService(pushToCache, pullFromCaches);
}

From source file:org.opendaylight.controller.cluster.datastore.node.utils.stream.PathArgumentTypes.java

public static byte getSerializablePathArgumentType(YangInstanceIdentifier.PathArgument pathArgument) {
    final Byte type = CLASS_TO_ENUM_MAP.get(pathArgument.getClass());
    Preconditions.checkArgument(type != null, "Unknown type of PathArgument = %s", pathArgument);
    return type;//from  ww w . j  av  a2  s .  co  m
}

From source file:com.atoito.please.core.util.Environment.java

public static void initializeWithHome(File home) {
    // M.info("=========  initializeWithHome '%s'", home.getAbsolutePath());
    if (current != null) {
        throw new PleaseException("environment already initialized");
    }//from   www. j a v a 2  s.c  o m
    File cleanHome = Preconditions.checkNotNull(home, "home directory cannot be null");
    Preconditions.checkArgument(cleanHome.exists(), "home '%s' not found", cleanHome.getAbsolutePath());
    Preconditions.checkArgument(cleanHome.isDirectory(), "home '%s' is not a directory",
            cleanHome.getAbsolutePath());
    current = new Environment(cleanHome);
}