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:org.apache.storm.nimbus.WorkerHeartbeatsRecoveryStrategyFactory.java

/**
 * Get instance of {@link IWorkerHeartbeatsRecoveryStrategy} with conf.
 * @param conf strategy config/*from   w w  w .j a v a 2s  .com*/
 * @return an instance of {@link IWorkerHeartbeatsRecoveryStrategy}
 */
public static IWorkerHeartbeatsRecoveryStrategy getStrategy(Map<String, Object> conf) {
    IWorkerHeartbeatsRecoveryStrategy strategy;
    if (conf.get(DaemonConfig.NIMBUS_WORKER_HEARTBEATS_RECOVERY_STRATEGY_CLASS) != null) {
        Object targetObj = ReflectionUtils
                .newInstance((String) conf.get(DaemonConfig.NIMBUS_WORKER_HEARTBEATS_RECOVERY_STRATEGY_CLASS));
        Preconditions.checkState(targetObj instanceof IWorkerHeartbeatsRecoveryStrategy,
                "{} must implements IWorkerHeartbeatsRecoveryStrategy",
                DaemonConfig.NIMBUS_WORKER_HEARTBEATS_RECOVERY_STRATEGY_CLASS);
        strategy = ((IWorkerHeartbeatsRecoveryStrategy) targetObj);
    } else {
        strategy = new TimeOutWorkerHeartbeatsRecoveryStrategy();
    }

    strategy.prepare(conf);
    return strategy;
}

From source file:org.opendaylight.controller.config.facade.xml.rpc.Rpcs.java

public ModuleRpcs getRpcMapping(RuntimeRpcElementResolved id) {
    Map<String, ModuleRpcs> modules = mappedRpcs.get(id.getNamespace());
    Preconditions.checkState(modules != null, "No modules found for namespace %s", id.getNamespace());
    String moduleName = id.getModuleName();
    ModuleRpcs rpcMapping = modules.get(moduleName);
    Preconditions.checkState(rpcMapping != null, "No module %s found for namespace %s", moduleName,
            id.getNamespace());/*  www  .  j  a  v  a 2  s.c om*/

    return rpcMapping;
}

From source file:org.opendaylight.yangtools.yang.model.repo.util.RefcountedRegistration.java

public boolean decRef() {
    Preconditions.checkState(refcount > 0, "Refcount underflow: %s", refcount);

    if (0 == --refcount) {
        reg.close();/* w w w .j a v a  2  s.c om*/
        return true;
    } else {
        return false;
    }
}

From source file:org.opendaylight.yangtools.yang.model.util.type.DecimalTypeBuilder.java

public DecimalTypeBuilder setFractionDigits(final int fractionDigits) {
    Preconditions.checkState(this.fractionDigits == null, "Fraction digits already defined to %s",
            this.fractionDigits);
    this.fractionDigits = fractionDigits;
    return this;
}

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

@Nullable
public static String getGraphiteUrl(@Nullable Instance graphite) throws IllegalStateException {
    if (graphite == null) {
        return null;
    }/* w ww  . ja  va2s .c o m*/
    Preconditions.checkState(graphite.getPublicDnsName() != null && !graphite.getPublicDnsName().isEmpty(),
            "Given graphite is not yet initialized, it publicDnsName is null: %s", graphite);
    return "http://" + graphite.getPublicDnsName() + "/";
}

From source file:org.opendaylight.controller.cluster.common.actor.FileAkkaConfigurationReader.java

@Override
public Config read() {
    File customConfigFile = new File(CUSTOM_AKKA_CONF_PATH);
    Preconditions.checkState(customConfigFile.exists(), "%s is missing", customConfigFile);

    File factoryConfigFile = new File(FACTORY_AKKA_CONF_PATH);
    if (factoryConfigFile.exists()) {
        return ConfigFactory.parseFile(customConfigFile)
                .withFallback(ConfigFactory.parseFile(factoryConfigFile));
    }/*w w  w . ja va 2  s . c  om*/

    return ConfigFactory.parseFile(customConfigFile);
}

From source file:co.cask.cdap.internal.app.runtime.spark.SparkClassLoader.java

/**
 * Finds the SparkClassLoader from the context ClassLoader hierarchy.
 *
 * @return the SparkClassLoader found//from w  w w. ja  va  2s  .c  o m
 * @throws IllegalStateException if no SparkClassLoader was found
 */
public static SparkClassLoader findFromContext() {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    SparkClassLoader sparkClassLoader = ClassLoaders.find(contextClassLoader, SparkClassLoader.class);
    // Should found the Spark ClassLoader
    Preconditions.checkState(sparkClassLoader != null,
            "Cannot find SparkClassLoader from context ClassLoader %s", contextClassLoader);
    return sparkClassLoader;
}

From source file:minium.cucumber.rest.BackendRegistry.java

public BackendRegistry register(String name, Backend backend) {
    Preconditions.checkState(!backends.containsKey(name), "Backend with name %s already registered", name);
    backends.put(name, backend);/*from w  w  w  . ja v  a  2  s .  c o m*/
    return this;
}

From source file:com.google.errorprone.bugpatterns.testdata.PreconditionsExpensiveStringNegativeCase1.java

public void error() {
    int foo = 42;
    Preconditions.checkState(true, "The foo %s foo  is not a good foo", foo);

    // This call should not be converted because of the %d, which does some locale specific
    // behaviour. If it were an %s, it would be fair game.
    Preconditions.checkState(true, String.format("The foo %d foo is not a good foo", foo));
}

From source file:org.opendaylight.yangtools.yang.model.util.type.LeafrefTypeBuilder.java

public LeafrefTypeBuilder setPathStatement(@Nonnull final RevisionAwareXPath pathStatement) {
    Preconditions.checkState(this.pathStatement == null, "Path statement already set to %s",
            this.pathStatement);
    this.pathStatement = Preconditions.checkNotNull(pathStatement);
    return this;
}