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 Object errorMessage) 

Source Link

Document

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

Usage

From source file:com.aerofs.baseline.http.HttpUtils.java

public static String readResponseEntityToString(HttpResponse response) throws IOException {
    Preconditions.checkArgument(response.getEntity().getContentLength() > 0,
            "entity must have non-zero content length");
    return readStreamToString(response.getEntity().getContent());
}

From source file:org.haiku.haikudepotserver.dataobjects.PkgScreenshot.java

public static Optional<PkgScreenshot> tryGetByCode(ObjectContext context, String code) {
    Preconditions.checkArgument(null != context, "the context must be supplied");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(code), "the code must be supplied");
    return Optional.ofNullable(ObjectSelect.query(PkgScreenshot.class).where(CODE.eq(code)).selectOne(context));
}

From source file:com.metamx.common.RetryUtils.java

public static <T> T retry(final Callable<T> f, Predicate<Throwable> shouldRetry, final int maxTries)
        throws Exception {
    Preconditions.checkArgument(maxTries > 0, "maxTries > 0");
    int nTry = 0;
    while (true) {
        try {//  w  w  w. j a  v a2 s  .  c  o  m
            nTry++;
            return f.call();
        } catch (Throwable e) {
            if (nTry < maxTries && shouldRetry.apply(e)) {
                awaitNextRetry(e, nTry);
            } else {
                Throwables.propagateIfInstanceOf(e, Exception.class);
                throw Throwables.propagate(e);
            }
        }
    }
}

From source file:org.apache.mahout.classifier.df.ErrorEstimate.java

public static double errorRate(double[] labels, double[] predictions) {
    Preconditions.checkArgument(labels.length == predictions.length, "labels.length != predictions.length");
    double nberrors = 0; // number of instance that got bad predictions
    double datasize = 0; // number of classified instances

    for (int index = 0; index < labels.length; index++) {
        if (predictions[index] == -1) {
            continue; // instance not classified
        }/*from  ww w. j a  va 2s . co  m*/

        if (predictions[index] != labels[index]) {
            nberrors++;
        }

        datasize++;
    }

    return nberrors / datasize;
}

From source file:com.android.builder.internal.packaging.zip.AlignmentRules.java

/**
 * A rule that defines a constant alignment for all files.
 *
 * @param alignment the alignment/*from   ww w  . ja  va  2  s .co  m*/
 * @return the rule
 */
public static AlignmentRule constant(int alignment) {
    Preconditions.checkArgument(alignment > 0, "alignment <= 0");

    return (String path) -> alignment;
}

From source file:com.digitalpetri.opcua.stack.core.util.ArrayUtil.java

/**
 * Flatten a multi-dimensional array into a one-dimensional array.
 *
 * @param array the array to flatten.//  w  ww . j  a v  a2 s  .co  m
 * @return a 1-dimensional array.
 */
public static Object flatten(Object array) {
    Preconditions.checkArgument(array.getClass().isArray(), "array");

    Class<?> type = getType(array);
    int[] dimensions = getDimensions(array);
    int length = length(dimensions);

    Object flattened = Array.newInstance(type, length);

    flatten(array, flattened, dimensions, 0);

    return flattened;
}

From source file:com.palantir.lock.LockCollections.java

public static <T extends Comparable<T>> SortedLockCollection<T> of(SortedMap<T, LockMode> locks) {
    Preconditions.checkArgument(locks.comparator() == null || locks.comparator() == Ordering.natural(),
            "sorted lock collections must use naturally comparable keys");
    return new SortedLockCollection<T>(locks.entrySet());
}

From source file:it.ItEnvironment.java

public static Map<String, Object> getConfiguration() {
    final ObjectMapper mapper = new ObjectMapper();
    final Map<String, Object> configuration;
    final File src = new File("it.json");

    Preconditions.checkArgument(src.exists(), "it.json doesn't exist in the working directory!");
    try {//from  w  w  w .java  2 s .c  o m
        configuration = mapper.readValue(src, new TypeReference<Map<String, Object>>() {
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return configuration;
}

From source file:org.apache.mahout.df.ErrorEstimate.java

public static double errorRate(int[] labels, int[] predictions) {
    Preconditions.checkArgument(labels.length == predictions.length, "labels.length != predictions.length");
    double nberrors = 0; // number of instance that got bad predictions
    double datasize = 0; // number of classified instances

    for (int index = 0; index < labels.length; index++) {
        if (predictions[index] == -1) {
            continue; // instance not classified
        }/* www .ja  va2  s  .  c  o  m*/

        if (predictions[index] != labels[index]) {
            nberrors++;
        }

        datasize++;
    }

    return nberrors / datasize;
}

From source file:rus.cpuinfo.Util.ConverterUtil.java

public static int convertFromKHzToMHz(final int KHz) {

    Preconditions.checkArgument(KHz > 0, "KHz must be greater than zero");
    return KHz / 1000;
}