Example usage for com.google.common.base Optional isPresent

List of usage examples for com.google.common.base Optional isPresent

Introduction

In this page you can find the example usage for com.google.common.base Optional isPresent.

Prototype

public abstract boolean isPresent();

Source Link

Document

Returns true if this holder contains a (non-null) instance.

Usage

From source file:com.facebook.buck.java.JavaLibraryRules.java

static JavaLibrary.Data initializeFromDisk(BuildTarget buildTarget, OnDiskBuildInfo onDiskBuildInfo) {
    Optional<Sha1HashCode> abiKeyHash = onDiskBuildInfo.getHash(AbiRule.ABI_KEY_ON_DISK_METADATA);
    if (!abiKeyHash.isPresent()) {
        throw new IllegalStateException(String
                .format("Should not be initializing %s from disk if the ABI key is not written.", buildTarget));
    }/*from   www  .j  av a2 s.  c o  m*/

    List<String> lines;
    try {
        lines = onDiskBuildInfo.getOutputFileContentsByLine(getPathToClassHashes(buildTarget));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    ImmutableSortedMap<String, HashCode> classHashes = AccumulateClassNamesStep.parseClassHashes(lines);

    return new JavaLibrary.Data(abiKeyHash.get(), classHashes);
}

From source file:edu.sdsc.solr.lemmatization.DefinitionParser.java

static Optional<String> getSynonym(String definition, LemmatizationSpec spec) {
    if (spec.isIncludeNouns()) {
        Optional<String> plural = checkPattern(pluralPattern, definition);
        if (plural.isPresent()) {
            return plural;
        }//from   ww  w .j a v  a  2 s .c om
        Optional<String> adjective = checkPattern(adjectivePattern, definition);
        if (adjective.isPresent()) {
            return adjective;
        }
    }
    if (spec.isIncludeVerbs()) {
        Optional<String> verb = checkPattern(verbPattern, definition);
        if (verb.isPresent()) {
            return verb;
        }
        Optional<String> adverb = checkPattern(adverbPattern, definition);
        if (adverb.isPresent()) {
            return adverb;
        }
    }
    if (spec.isIncludeVariants()) {
        Optional<String> alternative = checkPattern(alternativePattern, definition);
        if (alternative.isPresent()) {
            return alternative;
        }
    }

    return Optional.absent();
}

From source file:org.opendaylight.yangtools.yang.data.operations.LeafSetNodeModification.java

private static boolean contains(Optional<LeafSetNode<?>> actual, LeafSetEntryNode<?> leafListModification) {
    boolean contains = actual.isPresent();
    contains &= actual.get().getChild(leafListModification.getIdentifier()).isPresent();

    return contains;
}

From source file:org.automagic.deps.doctor.editor.PomEditor.java

private static void updateVersion(PomSpy spy, PomWriter writer, TransitiveDependency dependency,
        String version) {/*from   w  ww  .  j  a va 2s  .  c o  m*/
    Optional<String> propertyName = spy.getVersionPropertyName(version);
    if (propertyName.isPresent()) {
        writer.setVersionProperty(propertyName.get(), dependency.getVersion());
    } else {
        writer.setDependencyVersion(dependency);
    }
}

From source file:org.anhonesteffort.flock.AccountAndKeyRequiredActivity.java

protected static MasterCipher handleGetMasterCipherOrFail(Activity activity) {
    try {//from w  w w .  j  a  va 2  s . c o  m

        Optional<MasterCipher> cipher = KeyHelper.getMasterCipher(activity);
        if (cipher.isPresent())
            return cipher.get();
        else {
            Log.e(TAG, "master cipher is missing, fuck");
            throw new IOException("Where did master chipher GO!?!?");
        }

    } catch (IOException e) {
        // TODO: import account from scratch...
        activity.finish();
        return null;
    }
}

From source file:org.apache.gobblin.metadata.GlobalMetadata.java

/**
 * Builder that takes in an input {@GlobalMetadata} to use as a base.
 * @param inputMetadata input metadata/*w ww .  j a  va 2  s  . c o m*/
 * @param outputSchema output schema to set in the builder
 * @param <SI> input schema type
 * @param <SO> output schema type
 * @return builder
 */
public static <SI, SO> GlobalMetadataBuilder<SO> builderWithInput(GlobalMetadata<SI> inputMetadata,
        Optional<SO> outputSchema) {
    GlobalMetadataBuilder<SO> builder = (GlobalMetadataBuilder<SO>) builder();

    if (outputSchema.isPresent()) {
        builder.schema(outputSchema.get());
    }

    return builder;
}

From source file:springfox.documentation.schema.property.bean.Accessors.java

public static String propertyName(Method method) {
    Optional<JsonGetter> jsonGetterAnnotation = getterAnnotation(method);
    if (jsonGetterAnnotation.isPresent() && !isNullOrEmpty(jsonGetterAnnotation.get().value())) {
        return jsonGetterAnnotation.get().value();
    }/* w  ww. j  a v a 2s .co m*/
    Optional<JsonSetter> jsonSetterAnnotation = setterAnnotation(method);
    if (jsonSetterAnnotation.isPresent() && !isNullOrEmpty(jsonSetterAnnotation.get().value())) {
        return jsonSetterAnnotation.get().value();
    }
    Matcher matcher = getter.matcher(method.getName());
    if (matcher.find()) {
        return toCamelCase(matcher.group(1));
    }
    matcher = isGetter.matcher(method.getName());
    if (matcher.find()) {
        return toCamelCase(matcher.group(1));
    }
    matcher = setter.matcher(method.getName());
    if (matcher.find()) {
        return toCamelCase(matcher.group(1));
    }
    return "";
}

From source file:gobblin.util.CLIPasswordEncryptor.java

private static String getMasterPassword(CommandLine cl) {
    if (cl.hasOption(MASTER_PWD_OPTION)) {
        if (cl.hasOption(MASTER_PWD_FILE_OPTION)) {
            System.out.println(String.format("both -%s and -%s are provided. Using -%s", MASTER_PWD_OPTION,
                    MASTER_PWD_FILE_OPTION, MASTER_PWD_OPTION));
        }// www.  j a va 2  s . c o  m
        return cl.getOptionValue(MASTER_PWD_OPTION);
    }
    Path masterPwdLoc = new Path(cl.getOptionValue(MASTER_PWD_FILE_OPTION));
    Optional<String> masterPwd = PasswordManager.getMasterPassword(masterPwdLoc);
    if (masterPwd.isPresent()) {
        return masterPwd.get();
    }
    throw new RuntimeException("Failed to get master password from " + masterPwdLoc);
}

From source file:org.opendaylight.ovsdb.southbound.SouthboundUtil.java

public static Optional<OvsdbNodeAugmentation> getManagingNode(DataBroker db, OvsdbBridgeAttributes mn) {
    Preconditions.checkNotNull(mn);//from www  .j a  v  a  2s . c  om
    try {
        OvsdbNodeRef ref = mn.getManagedBy();
        if (ref != null && ref.getValue() != null) {
            ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
            @SuppressWarnings("unchecked") // Note: erasure makes this safe in combination with the typecheck below
            InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();

            CheckedFuture<Optional<Node>, ReadFailedException> nf = transaction
                    .read(LogicalDatastoreType.OPERATIONAL, path);
            transaction.close();
            Optional<Node> optional = nf.get();
            if (optional != null && optional.isPresent()) {
                OvsdbNodeAugmentation ovsdbNode = null;
                if (optional.get() instanceof Node) {
                    ovsdbNode = optional.get().getAugmentation(OvsdbNodeAugmentation.class);
                } else if (optional.get() instanceof OvsdbNodeAugmentation) {
                    ovsdbNode = (OvsdbNodeAugmentation) optional.get();
                }
                if (ovsdbNode != null) {
                    return Optional.of(ovsdbNode);
                } else {
                    LOG.warn("OvsdbManagedNode {} claims to be managed by {} but "
                            + "that OvsdbNode does not exist", mn, ref.getValue());
                    return Optional.absent();
                }
            } else {
                LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
                return Optional.absent();
            }
        } else {
            LOG.warn("Cannot find client for OvsdbManagedNode without a specified ManagedBy {}", mn);
            return Optional.absent();
        }
    } catch (Exception e) {
        LOG.warn("Failed to get OvsdbNode that manages OvsdbManagedNode {}", mn, e);
        return Optional.absent();
    }
}

From source file:info.gehrels.voting.singleTransferableVote.VoteState.java

public static <CANDIDATE_TYPE extends Candidate> Optional<VoteState<CANDIDATE_TYPE>> forBallotAndElection(
        Ballot<CANDIDATE_TYPE> ballot, Election<CANDIDATE_TYPE> election) {
    validateThat(ballot, is(notNullValue()));
    validateThat(election, is(notNullValue()));

    Optional<Vote<CANDIDATE_TYPE>> vote = ballot.getVote(election);
    if (!vote.isPresent()) {
        return Optional.absent();
    }/*from w  w w .jav a  2s . c  o  m*/

    return Optional.of(new VoteState<>(ballot.id, vote.get()));
}