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:org.bozzo.ipplan.tools.IpAddress.java

/**
 * Return the format format of an IP address
 * @param ip the IP address as {@link Long} value
 * @return the IP address in format x.x.x.x
 *//* ww  w  .j  a  v  a  2s .c  o  m*/
public static String toString(Long ip) {
    Preconditions.checkArgument(ip != null, "IP address shouldn't be null");

    return ((ip >> 24) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + (ip & 0xFF);
}

From source file:org.apache.eagle.metadata.utils.StreamIdConversions.java

public static String parseStreamTypeId(String siteId, String generatedUniqueStreamId) {
    String subffix = String.format("_%s", siteId).toUpperCase();
    if (generatedUniqueStreamId.endsWith(subffix)) {
        int streamTypeIdLength = generatedUniqueStreamId.length() - subffix.length();
        Preconditions.checkArgument(streamTypeIdLength > 0,
                "Invalid streamId: " + generatedUniqueStreamId + ", streamTypeId is empty");
        return generatedUniqueStreamId.substring(0, streamTypeIdLength).toUpperCase();
    } else {/*from   ww w .  j a  va  2 s . c  o m*/
        throw new IllegalArgumentException(
                "Invalid streamId: " + generatedUniqueStreamId + ", not end with \"" + subffix + "\"");
    }
}

From source file:org.opf_labs.fmts.corpora.Corpora.java

/**
 * @param count the number of items in the courpus
 * @param size the size of the corpus in bytes
 * @return the new details builder//w  ww. j av a  2 s  . co  m
 */
public static final Details details(final int count, final long size) {
    Preconditions.checkArgument(count >= 0, "count < 0");
    Preconditions.checkArgument(size >= 0, "size < 0");
    return new Details(count, size);
}

From source file:com.olacabs.fabric.compute.builder.impl.ArtifactoryJarPathResolver.java

public static String resolve(final String artifactoryUrl, final String groupId, final String artifactId,
        final String version) throws Exception {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(artifactoryUrl), "Artifactory URL cannot be null");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(groupId), "Group Id cannot be null");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(artifactId), "Artifact Id cannot be null");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(version), "Artifact version cannot be null");
    boolean isSnapshot = version.contains("SNAPSHOT");
    LOGGER.info("Artifact is snapshot: {}", isSnapshot);
    final String repoName = isSnapshot ? "libs-snapshot-local" : "libs-release-local";

    Artifactory client = ArtifactoryClient.create(artifactoryUrl);
    LOGGER.info("Aritifactory client created successfully with uri {}", client.getUri());
    FileAttribute<Set<PosixFilePermission>> perms = PosixFilePermissions
            .asFileAttribute(PosixFilePermissions.fromString("rwxr-xr-x"));
    java.nio.file.Path tempFilePath = Files.createTempFile(Long.toString(System.currentTimeMillis()), "xml",
            perms);/*from  w w w  .j av a2 s . c  om*/
    String metadataStr = null;
    if (isSnapshot) {
        metadataStr = String.format("%s/%s/%s/maven-metadata.xml", groupId.replaceAll("\\.", "/"), artifactId,
                version);
    } else {
        metadataStr = String.format("%s/%s/maven-metadata.xml", groupId.replaceAll("\\.", "/"), artifactId);
    }

    LOGGER.info("Repo-name - {}, metadataStr - {}", repoName, metadataStr);
    InputStream response = client.repository(repoName).download(metadataStr).doDownload();
    LOGGER.info("download complete");
    Files.copy(response, tempFilePath, StandardCopyOption.REPLACE_EXISTING);
    LOGGER.info("Metadata file downloaded to: {}", tempFilePath.toAbsolutePath().toString());

    final String url = String.format("%s/%s/%s/%s/%s/%s-%s.jar", artifactoryUrl, repoName,
            groupId.replaceAll("\\.", "/"), artifactId, version, artifactId, version);
    LOGGER.info("Jar will be downloaded from: " + url);
    return url;
}

From source file:org.apache.usergrid.persistence.index.utils.IndexValidationUtils.java

/**
 * Validate the search edge is correct//from   www.j  a v a2s.  c  om
 * @param searchEdge
 */
public static void validateSearchEdge(final SearchEdge searchEdge) {
    Preconditions.checkNotNull(searchEdge, "searchEdge is required");
    org.apache.usergrid.persistence.core.util.ValidationUtils.verifyIdentity(searchEdge.getNodeId());
    Preconditions.checkArgument(searchEdge.getEdgeName() != null && searchEdge.getEdgeName().length() > 0,
            "search edge name is required");

}

From source file:org.arbeitspferde.groningen.common.Statistics.java

/**
 * Compute the input percentile of the input double {@link List}. Returns
 * Double.NaN if the list of values is empty.
 *
 * Keep in mind that the values are sorted in place, thus modifying the values
 * {@link List} you've passed in.//  ww  w  .  j a  v  a2 s  .  c om
 *
 * @param {@link Double} {@link List}
 * @param double percentile
 */
public static double computePercentile(List<Double> values, double percentile) {
    Preconditions.checkArgument(percentile >= 0, "percentile must be greater or equal than 0");
    Preconditions.checkArgument(percentile <= 100, "percentile must be smaller or equal than 100");

    int sz = values.size();
    switch (sz) {
    case 0:
        log.warning("The list passed to computePercentile is empty. NaN returned.");
        return Double.NaN;
    case 1:
        return values.get(0);
    default:
        // Sort {@link List} in place and in ascending order
        Collections.sort(values);
        // the 100th percentile is defined to be the largest value
        if (percentile == 100) {
            return values.get(sz - 1);
        }
        return values.get((int) Math.floor(percentile * (double) sz / 100.0));
    }
}

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

/**
 * A rule that defines constant alignment for all files with a certain suffix, placing no
 * restrictions on other files.//from  w  ww .j  a  v  a 2  s .  co  m
 *
 * @param suffix the suffix
 * @param alignment the alignment for paths that match the provided suffix
 * @return the rule
 */
public static AlignmentRule constantForSuffix(@NonNull String suffix, int alignment) {
    Preconditions.checkArgument(!suffix.isEmpty(), "suffix.isEmpty()");
    Preconditions.checkArgument(alignment > 0, "alignment <= 0");

    return (String path) -> path.endsWith(suffix) ? alignment : AlignmentRule.NO_ALIGNMENT;
}