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) 

Source Link

Document

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

Usage

From source file:com.google.template.soy.jssrc.dsl.Ternary.java

static Ternary create(CodeChunk.WithValue predicate, CodeChunk.WithValue consequent,
        CodeChunk.WithValue alternate) {
    Preconditions.checkArgument(predicate.initialStatements().containsAll(consequent.initialStatements()));
    Preconditions.checkArgument(predicate.initialStatements().containsAll(alternate.initialStatements()));
    return new AutoValue_Ternary(
            ImmutableSet.<CodeChunk>builder().addAll(predicate.initialStatements())
                    .addAll(consequent.initialStatements()).addAll(alternate.initialStatements()).build(),
            predicate, consequent, alternate);
}

From source file:com.google.api.server.spi.config.validation.InconsistentApiConfigurationException.java

private static String getErrorMessage(ApiConfig config, ApiConfig otherConfig,
        Iterable<ApiConfigInconsistency<Object>> inconsistencies) {
    Preconditions.checkArgument(!Iterables.isEmpty(inconsistencies));
    ApiConfigInconsistency<?> firstInconsistency = Iterables.getFirst(inconsistencies, null);
    return String.format(
            "API-wide configuration does not match between the classes %s and %s. All "
                    + "API classes with the same API name and version must have the exact same API-wide "
                    + "configuration. Differing property: %s (%s vs %s).",
            config.getApiClassConfig().getApiClassJavaName(),
            otherConfig.getApiClassConfig().getApiClassJavaName(), firstInconsistency.getPropertyName(),
            firstInconsistency.getValue1(), firstInconsistency.getValue2());
}

From source file:org.sonar.flex.checks.utils.Function.java

public static boolean isAccessor(AstNode functionDef) {
    Preconditions.checkArgument(functionDef.is(FlexGrammar.FUNCTION_DEF));
    return functionDef.getFirstChild(FlexGrammar.FUNCTION_NAME).getFirstChild(FlexKeyword.GET,
            FlexKeyword.SET) != null;//from   w  w  w.j  a  v a 2 s .com
}

From source file:com.google.cloud.tools.appengine.cloudsdk.internal.FileUtil.java

/**
 * Implementation of recursive directory copy, does NOT overwrite
 *
 * @param source an existing source directory to copy from.
 * @param destination an existing destination directory to copy to.
 *///  w w  w .ja v a 2 s  . c om
public static void copyDirectory(final Path source, final Path destination) throws IOException {
    Preconditions.checkNotNull(source);
    Preconditions.checkNotNull(destination);
    Preconditions.checkArgument(Files.isDirectory(source));
    Preconditions.checkArgument(Files.isDirectory(destination));
    Preconditions.checkArgument(!source.equals(destination));
    Preconditions.checkArgument(!destination.startsWith(source), "destination is child of source");

    Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
        final CopyOption[] copyOptions = new CopyOption[] { StandardCopyOption.COPY_ATTRIBUTES };

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

            if (dir.equals(source)) {
                return FileVisitResult.CONTINUE;
            }

            Files.copy(dir, destination.resolve(source.relativize(dir)), copyOptions);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

            Files.copy(file, destination.resolve(source.relativize(file)), copyOptions);
            return FileVisitResult.CONTINUE;
        }
    });

}

From source file:org.inferred.freebuilder.processor.util.TypeReference.java

/**
 * Returns a {@link TypeReference} for a type in {@code packageName}. If {@code nestedTypes} is
 * empty, it is a top level type called {@code topLevelType}; otherwise, it is nested in that
 * type./*  w  w w.j a  va2  s.co  m*/
 */
public static TypeReference to(String packageName, String topLevelType, String... nestedTypes) {
    Preconditions.checkArgument(!packageName.isEmpty());
    Preconditions.checkArgument(!topLevelType.isEmpty());
    StringBuilder nestedSuffix = new StringBuilder();
    for (String nestedType : nestedTypes) {
        Preconditions.checkArgument(!nestedType.isEmpty());
        nestedSuffix.append(".").append(nestedType);
    }
    return new TypeReference(packageName, topLevelType, nestedSuffix.toString());
}

From source file:org.sonar.flex.checks.utils.Clazz.java

public static List<AstNode> getFields(AstNode classDefNode) {
    Preconditions.checkArgument(classDefNode.is(FlexGrammar.CLASS_DEF));
    List<AstNode> fields = Lists.newArrayList();

    for (AstNode directive : classDefNode.getFirstChild(FlexGrammar.BLOCK).getFirstChild(FlexGrammar.DIRECTIVES)
            .getChildren()) {/*from ww w .  j  ava 2s  .  c  om*/
        AstNode fieldDef = getFieldDefinition(directive);
        if (fieldDef != null) {
            fields.add(fieldDef);
        }
    }
    return fields;
}

From source file:com.cloudera.oryx.als.computation.ALSJobStepConfig.java

public static ALSJobStepConfig fromArgsArray(String... args) {
    Preconditions.checkNotNull(args);//from  ww w.ja  v  a  2 s. c om
    Preconditions.checkArgument(args.length >= 5);
    return new ALSJobStepConfig(args[0], Integer.parseInt(args[1]), Integer.parseInt(args[2]),
            Integer.parseInt(args[3]), Boolean.parseBoolean(args[4]));
}

From source file:net.sourceforge.cilib.functions.continuous.moo.wfg.Transitions.java

public static Vector WFG1_t1(Vector y, int k) {
    int n = y.size();

    Preconditions.checkArgument(Misc.vector_in_01(y));
    Preconditions.checkArgument(k >= 1);
    Preconditions.checkArgument(k < n);

    Vector.Builder t = Vector.newBuilder();

    for (int i = 0; i < k; i++) {
        t.add(y.doubleValueOf(i));/*w  ww .  ja  va2 s.  c  o m*/
    }

    for (int i = k; i < n; i++) {
        t.add(TransFunctions.s_linear(y.doubleValueOf(i), 0.35));
    }

    return t.build();
}

From source file:org.janusgraph.testutil.JanusGraphAssert.java

public static int size(Object obj) {
    Preconditions.checkArgument(obj != null);
    if (obj instanceof Traversal)
        return size(((Traversal) obj).toList());
    else if (obj instanceof Collection)
        return ((Collection) obj).size();
    else if (obj instanceof Iterable)
        return Iterables.size((Iterable) obj);
    else if (obj instanceof Iterator)
        return Iterators.size((Iterator) obj);
    else if (obj.getClass().isArray())
        return Array.getLength(obj);
    throw new IllegalArgumentException("Cannot determine size of: " + obj);
}

From source file:org.apache.james.mailbox.store.mail.model.AttachmentId.java

public static AttachmentId forPayload(byte[] payload) {
    Preconditions.checkArgument(payload != null);
    return new AttachmentId(DigestUtils.sha1Hex(payload));
}