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

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

Introduction

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

Prototype

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

Source Link

Document

Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

Usage

From source file:com.linkedin.pinot.core.segment.index.converter.SegmentV1V2ToV3FormatConverter.java

public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        System.err.println("Usage: $0 <table directory with segments>");
        System.exit(1);//from ww w .j  ava2s. com
    }
    File tableDirectory = new File(args[0]);
    Preconditions.checkState(tableDirectory.exists(), "Directory: {} does not exist", tableDirectory);
    Preconditions.checkState(tableDirectory.isDirectory(), "Path: {} is not a directory", tableDirectory);
    File[] files = tableDirectory.listFiles();
    SegmentFormatConverter converter = new SegmentV1V2ToV3FormatConverter();

    for (File file : files) {
        if (!file.isDirectory()) {
            System.out.println("Path: " + file + " is not a directory. Skipping...");
            continue;
        }
        long startTimeNano = System.nanoTime();
        converter.convert(file);
        long endTimeNano = System.nanoTime();
        long latency = (endTimeNano - startTimeNano) / (1000 * 1000);
        System.out.println("Converting segment: " + file + " took " + latency + " milliseconds");
    }
}

From source file:com.facebook.nifty.core.ConnectionContexts.java

public static ConnectionContext getContext(Channel channel) {
    ConnectionContext context = (ConnectionContext) channel.getPipeline()
            .getContext(ConnectionContextHandler.class).getAttachment();
    Preconditions.checkState(context != null, "Context not yet set on channel %s", channel);
    return context;
}

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

static StatementList of(ImmutableList<? extends CodeChunk> statements) {
    Preconditions.checkState(statements.size() > 1, "list of size %s makes no sense", statements.size());
    return new AutoValue_StatementList(statements);
}

From source file:com.facebook.buck.cxx.toolchain.PathShortener.java

static PathShortener byRelativizingToWorkingDir(Path workingDir) {
    return (absolutePath) -> {
        Preconditions.checkState(absolutePath.isAbsolute(), "Expected preprocessor suffix to be absolute: %s",
                absolutePath);/*from   w  w  w  .j  a v a  2 s  . co m*/
        Path relativePath = MorePaths.relativize(workingDir, absolutePath);
        return absolutePath.toString().length() > relativePath.toString().length() ? relativePath
                : absolutePath;
    };
}

From source file:com.palantir.docker.compose.connection.State.java

public static State parseFromDockerComposePs(String psOutput) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(psOutput), "No container found");
    Matcher matcher = STATE_PATTERN.matcher(psOutput);
    Preconditions.checkState(matcher.find(), "Could not parse status: %s", psOutput);
    String matchedStatus = matcher.group(STATE_INDEX);
    return valueOf(matchedStatus);
}

From source file:fr.xebia.workshop.monitoring.TeamInfrastructure.java

@Nullable
public static String getNagiosUrl(@Nullable Instance nagios) throws IllegalStateException {
    if (nagios == null) {
        return null;
    }//from www .j a va2 s  . co m
    Preconditions.checkState(nagios.getPublicDnsName() != null && !nagios.getPublicDnsName().isEmpty(),
            "Given nagios is not yet initialized, it publicDnsName is null: %s", nagios);
    return "http://" + nagios.getPublicDnsName() + "/nagios";
}

From source file:org.apache.storm.assignments.LocalAssignmentsBackendFactory.java

public static ILocalAssignmentsBackend getBackend(Map<String, Object> conf) {
    if (conf.get(Config.NIMBUS_LOCAL_ASSIGNMENTS_BACKEND_CLASS) != null) {
        Object targetObj = ReflectionUtils
                .newInstance((String) conf.get(Config.NIMBUS_LOCAL_ASSIGNMENTS_BACKEND_CLASS));
        Preconditions.checkState(targetObj instanceof ILocalAssignmentsBackend,
                "{} must implements ILocalAssignmentsBackend", Config.NIMBUS_LOCAL_ASSIGNMENTS_BACKEND_CLASS);
        ((ILocalAssignmentsBackend) targetObj).prepare(conf);
        return (ILocalAssignmentsBackend) targetObj;
    }//from   w w  w. j av  a2  s.  c  o  m

    return getDefault();
}

From source file:com.facebook.util.Validations.java

public static <T extends Exception> void checkState(boolean expression, Class<T> clazz, String message,
        Object... errorMessageArgs) throws T {
    try {//ww w.  j a v  a2  s .co  m
        Preconditions.checkState(expression, message, errorMessageArgs);
    } catch (Exception e) {
        throw ExceptionUtils.wrap(e, clazz);
    }
}

From source file:com.aerofs.baseline.Injection.java

/**
 * Get the {@link ServiceHandle} to an instance of type {@code T}.
 *
 * @param locator {@code ServiceLocator} instance used to locate the singleton instance of type {@code T}
 * @param implementationClass type of the service to be located
 * @param <T> type-parameter identifying the type of the service to be located
 * @return valid {@code ServiceHandle} that points to a <strong>active</strong> instance of type {@code T}
 * @throws IllegalStateException if no instance satisfying {@code T} could be found
 *///from   w w  w  .  j a v  a2 s .c om
public static <T> ServiceHandle<T> getServiceHandle(ServiceLocator locator, Class<T> implementationClass) {
    ServiceHandle<T> handle = locator.getServiceHandle(implementationClass);

    Preconditions.checkState(handle != null, "fail locate type %s", implementationClass.getSimpleName());
    Preconditions.checkState(handle.getService() != null, "fail create instance of %s",
            implementationClass.getSimpleName());
    Preconditions.checkState(handle.isActive(), "%s is no longer active", implementationClass.getSimpleName());

    return handle;
}

From source file:com.google.devtools.build.android.desugar.ByteCodeTypePrinter.java

public static void printClassesWithTypes(Path inputJarFile, PrintWriter printWriter) throws IOException {
    Preconditions.checkState(Files.exists(inputJarFile), "The input jar file %s does not exist.", inputJarFile);
    try (ZipFile jarFile = new ZipFile(inputJarFile.toFile())) {
        for (ZipEntry entry : getSortedClassEntriess(jarFile)) {
            try (InputStream classStream = jarFile.getInputStream(entry)) {
                printWriter.println("\nClass: " + entry.getName());
                ClassReader classReader = new ClassReader(classStream);
                ClassVisitor visitor = new ClassWithTypeDumper(printWriter);
                classReader.accept(visitor, 0);
            }/*w  w  w  .  j  a  v a 2  s  .com*/
            printWriter.println("\n");
        }
    }
}