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:uk.ac.ed.inf.setmac.databases.XmlImporter.java

public void importXml(File file, String site, long randomSeed) throws Exception {
    Preconditions.checkNotNull(file);// w w  w. j av a  2s  . c  om
    Preconditions.checkState(file.exists(), "XML file does not exist: {0}", file);
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    InputStream inputStream = new RemoveBadCharactersInputStream(
            new BufferedInputStream(new FileInputStream(file)));
    Database database = engine.getEnvironment().getDatabase();
    int siteId = DB.getSiteId(database, site);
    XmlHandler handler = new XmlHandler(engine, siteId, randomSeed);

    try {
        parser.parse(inputStream, handler);
    } catch (SAXException saxException) {
        if (saxException instanceof ParseStoppedException) {
            throw new StoppingException();
        } else {
            Throwable temp = saxException.getException();

            while (true) {
                if (temp == null) {
                    break;
                }

                if (temp instanceof ParseStoppedException) {
                    throw new StoppingException();
                }

                temp = temp.getCause();
            }
        }

        throw saxException;
    }
}

From source file:com.facebook.buck.jvm.java.abi.FilesystemStubJarWriter.java

public FilesystemStubJarWriter(ProjectFilesystem filesystem, Path outputPath) throws IOException {
    Preconditions.checkState(!filesystem.exists(outputPath), "Output file already exists: %s)", outputPath);

    if (outputPath.getParent() != null && !filesystem.exists(outputPath.getParent())) {
        filesystem.createParentDirs(outputPath);
    }/*from  w w w.j  a  va2 s  . c o m*/

    this.outputPath = filesystem.resolve(outputPath);
    jarBuilder = new JarBuilder().setShouldHashEntries(true).setShouldMergeManifests(true);
}

From source file:com.facebook.buck.jvm.java.abi.StubJar.java

public void writeTo(ProjectFilesystem filesystem, Path path) throws IOException {
    Preconditions.checkState(!filesystem.exists(path), "Output file already exists: %s)", path);

    if (path.getParent() != null && !filesystem.exists(path.getParent())) {
        filesystem.createParentDirs(path);
    }/*from   w  w  w  .ja v a  2 s  .c  o  m*/

    Walker walker = Walkers.getWalkerFor(toMirror);
    try (HashingDeterministicJarWriter jar = new HashingDeterministicJarWriter(
            new JarOutputStream(filesystem.newFileOutputStream(path)))) {
        final CreateStubAction createStubAction = new CreateStubAction(jar);
        walker.walk(createStubAction);
    }
}

From source file:org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.CompositeAttributeReadingStrategy.java

@Override
AttributeConfigElement readElementHook(List<XmlElement> configNodes) throws DocumentedException {

    Preconditions.checkState(configNodes.size() == 1, "This element should be present only once %s",
            configNodes);//from   w w w . ja v a  2s.  co m

    XmlElement complexElement = configNodes.get(0);

    Map<String, Object> innerMap = Maps.newHashMap();

    List<XmlElement> recognisedChildren = Lists.newArrayList();
    for (Entry<String, AttributeReadingStrategy> innerAttrEntry : innerStrategies.entrySet()) {
        List<XmlElement> childItem = complexElement.getChildElementsWithSameNamespace(innerAttrEntry.getKey());
        recognisedChildren.addAll(childItem);

        AttributeConfigElement resolvedInner = innerAttrEntry.getValue().readElement(childItem);

        Object value = resolvedInner.getValue();
        if (value == null) {
            value = resolvedInner.getDefaultValue();
        }

        innerMap.put(innerAttrEntry.getKey(), value);
    }

    complexElement.checkUnrecognisedElements(recognisedChildren);

    String perInstanceEditStrategy = complexElement.getAttribute(XmlMappingConstants.OPERATION_ATTR_KEY,
            XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);

    return Strings.isNullOrEmpty(perInstanceEditStrategy)
            ? AttributeConfigElement.create(getNullableDefault(), innerMap)
            : AttributeConfigElement.create(getNullableDefault(), innerMap,
                    EditStrategyType.valueOf(perInstanceEditStrategy));
}

From source file:alluxio.heartbeat.HeartbeatScheduler.java

/**
 * @param timer a timer to add to the scheduler
 *//*from  w  w w. java2s .co m*/
public static void addTimer(ScheduledTimer timer) {
    Preconditions.checkNotNull(timer, "timer");
    try (LockResource r = new LockResource(sLock)) {
        Preconditions.checkState(!sTimers.containsKey(timer.getThreadName()),
                "The timer for thread %s is already waiting to be scheduled", timer.getThreadName());
        sTimers.put(timer.getThreadName(), timer);
        sCondition.signalAll();
    }
}

From source file:fathom.realm.pam.PamRealm.java

@Override
public void setup(Config config) {
    super.setup(config);

    String os = System.getProperty("os.name").toLowerCase();
    Preconditions.checkState(!os.startsWith("windows"), "PAM authentication is not supported on '{}'", os);

    // Try to identify the passwd database
    String[] files = { "/etc/shadow", "/etc/master.passwd" };
    File passwdFile = null;// w ww.  ja  v a2 s  .  co m
    for (String name : files) {
        File f = new File(name);
        if (f.exists()) {
            passwdFile = f;
            break;
        }
    }
    if (passwdFile == null) {
        log.warn("Could not find a passwd database!");
    } else if (!passwdFile.canRead()) {
        log.warn("Can not read passwd database {}! PAM authentications may fail!", passwdFile);
    }

    serviceName = "system-auth";
    if (config.hasPath("serviceName")) {
        serviceName = config.getString("serviceName");
    }
}

From source file:de.cosmocode.palava.scope.AbstractUnitOfWorkScope.java

/**
 * Checks that this scope is currently <strong>not</strong> in progress.
 *
 * @since 2.0/* w w  w.  j av  a2s. c  o  m*/
 * @throws IllegalStateException if this scope is active
 */
protected final void checkNotActive() {
    Preconditions.checkState(isNotActive(), "%s already in progress", this);
}