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

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:me.lucko.luckperms.common.api.ApiUtils.java

public static void checkTrack(Track track) {
    Preconditions.checkState(track instanceof TrackDelegate,
            "Track instance cannot be handled by this implementation.");
}

From source file:com.lyndir.omicron.api.Security.java

static <R> R godRun(final Job<R> job) {
    if (isGod())//from w w  w .  j  a v a 2  s. co  m
        // Already god.
        return job.execute();

    try {
        // Become god.
        godJobTL.get().push(true);
        return job.execute();
    } finally {
        // Become mortal.
        Preconditions.checkState(godJobTL.get().pop(), "Expected to be god.");
    }
}

From source file:org.apache.usergrid.chop.example.DigitalWatch.java

@Inject
public void addPowerSource(PowerSource powerSource) {
    Preconditions.checkState(powerSource.hasPower(), "Don't install a dead battery");
    Preconditions.checkState(powerSource instanceof Battery);

    //noinspection ConstantConditions
    this.battery = (Battery) powerSource;
}

From source file:org.ros.internal.message.definition.MessageDefinitionTupleParser.java

/**
 * Splits the message definition tuple into a {@link List} of message
 * definitions. Split message definitions may be empty (e.g. std_srvs/Empty).
 *
 * @param definition/* w w w.j  a va 2  s . co m*/
 *          the message definition tuple
 * @param size
 *          the expected tuple size, or -1 to ignore this requirement
 * @return a {@link List} of the specified size
 */
public static List<String> parse(String definition, int size) {
    Preconditions.checkNotNull(definition);
    List<String> definitions = Lists.newArrayList();
    StringBuilder current = new StringBuilder();
    for (String line : definition.split("\n")) {
        if (line.startsWith(SEPARATOR)) {
            definitions.add(current.toString());
            current = new StringBuilder();
            continue;
        }
        current.append(line);
        current.append("\n");
    }
    if (current.length() > 0) {
        current.deleteCharAt(current.length() - 1);
    }
    definitions.add(current.toString());
    Preconditions.checkState(size == -1 || definitions.size() <= size,
            String.format("Message tuple exceeds expected size: %d > %d", definitions.size(), size));
    while (definitions.size() < size) {
        definitions.add("");
    }
    return definitions;
}

From source file:org.haiku.pkg.HpkrFileExtractor.java

public HpkrFileExtractor(File file) throws IOException, HpkException {

    super();// w  w  w  . j av  a 2s  .co m
    Preconditions.checkNotNull(file);
    Preconditions.checkState(file.isFile() && file.exists(), "the file does not exist or is not a file");

    this.file = file;
    this.header = readHeader();

    try {
        heapReader = new HpkHeapReader(file, header.getHeapCompression(), header.getHeaderSize(),
                header.getHeapChunkSize(), // uncompressed size
                header.getHeapSizeCompressed(), // including the compressed chunk lengths.
                header.getHeapSizeUncompressed() // excludes the compressed chunk lengths.
        );

        attributesStringTable = new HpkStringTable(heapReader, header.getInfoLength(),
                header.getPackagesStringsLength(), header.getPackagesStringsCount());

    } catch (Exception e) {
        close();
        throw new HpkException("unable to setup the hpkr file extractor", e);
    } catch (Throwable th) {
        close();
        throw new RuntimeException("unable to setup the hpkr file extractor", th);
    }
}

From source file:org.locationtech.geogig.api.plumbing.ResolveBranchId.java

@Override
protected Optional<Ref> _call() {
    Preconditions.checkState(id != null, "id has not been set.");
    Predicate<Ref> filter = new Predicate<Ref>() {
        @Override//from  ww  w  .ja  va  2 s . c  o m
        public boolean apply(@Nullable Ref ref) {
            return ref.getObjectId().equals(id);
        }
    };
    ImmutableSet<Ref> refs = command(ForEachRef.class).setFilter(filter).call();
    if (refs.isEmpty()) {
        return Optional.absent();
    } else {
        return Optional.of(refs.iterator().next());
    }
}

From source file:org.apache.usergrid.chop.example.MechanicalWatch.java

@Inject
public void addPowerSource(PowerSource powerSource) {
    Preconditions.checkState(powerSource.hasPower(), "Make sure the spring is wound before starting.");
    this.spring = (Mainspring) powerSource;
}

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

public void checkState(boolean expression, String message) throws T {
    try {/*from   ww  w  . j  a v  a 2 s.  c o m*/
        Preconditions.checkState(expression, message);
    } catch (Exception e) {
        throw ExceptionUtils.wrap(e, clazz);
    }
}

From source file:garmintools.sections.AirspaceTable.java

public IndexForeignKey lookup(String airspace) {
    int index = AIRSPACE.indexOf(airspace);
    Preconditions.checkState(index != -1, "Undefined airspace: " + airspace);
    return new IndexForeignKey(index);
}

From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraExpiringKeyValueService.java

public static CassandraExpiringKeyValueService create(CassandraKeyValueServiceConfigManager configManager) {
    Preconditions.checkState(!configManager.getConfig().servers().isEmpty(), "address list was empty");

    Optional<CassandraJmxCompactionManager> compactionManager = CassandraJmxCompaction
            .createJmxCompactionManager(configManager);
    CassandraExpiringKeyValueService kvs = new CassandraExpiringKeyValueService(configManager,
            compactionManager);/*from w  w w. ja v  a2s.c om*/
    kvs.init();
    return kvs;
}