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 checkUser(User user) {
    Preconditions.checkState(user instanceof UserDelegate,
            "User instance cannot be handled by this implementation.");
}

From source file:eu.lp0.cursus.db.DatabaseSession.java

public static EntityManager getEntityManager() {
    EntityManager em = THREADS.get();/*from  www . j av a 2s .c  om*/
    Preconditions.checkState(em != null, "Session not open"); //$NON-NLS-1$
    return em;
}

From source file:io.grpc.alts.HandshakerServiceChannel.java

public static synchronized void setHandshakerAddressForTesting(String handshakerAddress) {
    Preconditions.checkState(
            channel == null || HandshakerServiceChannel.handshakerAddress.equals(handshakerAddress),
            "HandshakerServiceChannel already created with a different handshakerAddress");
    HandshakerServiceChannel.handshakerAddress = handshakerAddress;
    if (channel == null) {
        channel = createChannel();// ww  w .  j  a  v a 2  s.  c  o m
    }
}

From source file:org.apache.kylin.engine.mr.common.JobInfoConverter.java

public static JobInstance parseToJobInstance(AbstractExecutable job, Map<String, Output> outputs) {
    if (job == null) {
        return null;
    }//from www  .j a  va 2  s. co m
    Preconditions.checkState(job instanceof CubingJob, "illegal job type, id:" + job.getId());
    CubingJob cubeJob = (CubingJob) job;
    Output output = outputs.get(job.getId());
    final JobInstance result = new JobInstance();
    result.setName(job.getName());
    result.setRelatedCube(CubingExecutableUtil.getCubeName(cubeJob.getParams()));
    result.setRelatedSegment(CubingExecutableUtil.getSegmentId(cubeJob.getParams()));
    result.setLastModified(output.getLastModified());
    result.setSubmitter(cubeJob.getSubmitter());
    result.setUuid(cubeJob.getId());
    result.setType(CubeBuildTypeEnum.BUILD);
    result.setStatus(parseToJobStatus(output.getState()));
    result.setMrWaiting(
            AbstractExecutable.getExtraInfoAsLong(output, CubingJob.MAP_REDUCE_WAIT_TIME, 0L) / 1000);
    result.setExecStartTime(AbstractExecutable.getStartTime(output));
    result.setExecEndTime(AbstractExecutable.getEndTime(output));
    result.setExecInterruptTime(AbstractExecutable.getInterruptTime(output));
    result.setDuration(AbstractExecutable.getDuration(result.getExecStartTime(), result.getExecEndTime(),
            result.getExecInterruptTime()) / 1000);
    for (int i = 0; i < cubeJob.getTasks().size(); ++i) {
        AbstractExecutable task = cubeJob.getTasks().get(i);
        result.addStep(parseToJobStep(task, i, outputs.get(task.getId())));
    }
    return result;
}

From source file:uk.ac.ed.inf.ace.Config.java

public static Engine<?, ?> load(String configPath, String environmentName, String extensionSchemaPath,
        String extensionContextPath) throws Exception {
    configPath = Utilities.ifNull(configPath, DEFAULT_PATH);
    File file = new File(configPath);
    Preconditions.checkState(file.exists(), "Config file missing");
    Schema schema;/*from  w  w  w  . j  ava  2s.co m*/

    try (InputStream inputStream = uk.ac.ed.inf.ace.config.v1.Engine.class
            .getResourceAsStream(CONFIG_XSD_RESOURCE_NAME)) {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        StreamSource[] sources;

        if (extensionSchemaPath != null) {
            sources = new StreamSource[] { new StreamSource(inputStream),
                    new StreamSource(new File(extensionSchemaPath)) };
        } else {
            sources = new StreamSource[] { new StreamSource(inputStream) };
        }

        schema = schemaFactory.newSchema(sources);
    }

    ValidatorHandler validatorHandler = schema.newValidatorHandler();
    JAXBContext jaxbContext = JAXBContext
            .newInstance(Utilities.ifNull(extensionContextPath, "") + ":uk.ac.ed.inf.ace.config.v1");
    UnmarshallerHandler unmarshallerHandler = jaxbContext.createUnmarshaller().getUnmarshallerHandler();
    validatorHandler.setContentHandler(unmarshallerHandler);
    TransformerFactory.newInstance().newTransformer().transform(new StreamSource(file),
            new SAXResult(validatorHandler));
    uk.ac.ed.inf.ace.config.v1.EngineBase engineBase = (uk.ac.ed.inf.ace.config.v1.EngineBase) unmarshallerHandler
            .getResult();
    @SuppressWarnings("unchecked")
    Class<? extends Engine<?, ?>> type = (Class<? extends Engine<?, ?>>) Class.forName(engineBase.getType());
    return Utilities.construct(type, new Class[] { engineBase.getClass(), String.class },
            new Object[] { engineBase, environmentName });
}

From source file:com.twitter.common.util.ParsingUtil.java

/**
 * Parses a string as a range between one integer and another.  The integers must be separated by
 * a hypen character (space padding is acceptable).  Additionally, the first integer
 * (left-hand side) must be less than or equal to the second (right-hand side).
 *
 * @param rangeString The string to parse as an integer range.
 * @return A pair of the parsed integers.
 *///ww  w.ja  va2s  .  c om
public static Pair<Integer, Integer> parseRange(String rangeString) {
    if (rangeString == null)
        return null;

    String[] startEnd = rangeString.split("-");
    Preconditions.checkState(startEnd.length == 2, "Shard range format: start-end (e.g. 1-4)");
    int start;
    int end;
    try {
        start = Integer.parseInt(startEnd[0].trim());
        end = Integer.parseInt(startEnd[1].trim());
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("Failed to parse shard range.", e);
    }

    Preconditions.checkState(start <= end,
            "The left-hand side of a shard range must be <= the right-hand side.");
    return Pair.of(start, end);
}

From source file:me.lucko.luckperms.common.api.internal.Utils.java

public static void checkGroup(Group group) {
    Preconditions.checkState(group instanceof GroupLink,
            "Group instance cannot be handled by this implementation.");
}

From source file:me.lucko.luckperms.common.api.delegates.model.ApiUser.java

public static me.lucko.luckperms.common.model.User cast(User u) {
    Preconditions.checkState(u instanceof ApiUser,
            "Illegal instance " + u.getClass() + " cannot be handled by this implementation.");
    return ((ApiUser) u).getHandle();
}

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

@Override
public Config read() {
    File defaultConfigFile = new File(AKKA_CONF_PATH);
    Preconditions.checkState(defaultConfigFile.exists(), "akka.conf is missing");
    return ConfigFactory.parseFile(defaultConfigFile);

}

From source file:io.ssc.trackthetrackers.extraction.hadoop.util.DistributedCacheHelper.java

public static Path[] getCachedFiles(Configuration conf) throws IOException {
    LocalFileSystem localFs = FileSystem.getLocal(conf);
    Path[] cacheFiles = DistributedCache.getLocalCacheFiles(conf);
    URI[] fallbackFiles = DistributedCache.getCacheFiles(conf);
    // fallback for local execution
    if (cacheFiles == null) {
        Preconditions.checkState(fallbackFiles != null, "Unable to find cached files!");
        cacheFiles = new Path[fallbackFiles.length];
        for (int n = 0; n < fallbackFiles.length; n++) {
            cacheFiles[n] = new Path(fallbackFiles[n].getPath());
        }/*from w w w.  j  a v a  2s . co  m*/
    } else {
        for (int n = 0; n < cacheFiles.length; n++) {
            cacheFiles[n] = localFs.makeQualified(cacheFiles[n]);
            // fallback for local execution
            if (!localFs.exists(cacheFiles[n])) {
                cacheFiles[n] = new Path(fallbackFiles[n].getPath());
            }
        }
    }
    Preconditions.checkState(cacheFiles.length > 0, "Unable to find cached files!");
    return cacheFiles;
}