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:de.flapdoodle.logparser.matcher.stacktrace.AbstractStackElement.java

protected static <T> Optional<T> match(CharSequence input, Pattern pattern, IStackElementFactory<T> factory) {
    Optional<Map<String, String>> m = Patterns.match(pattern.matcher(input));
    if (m.isPresent()) {
        return Optional.of(factory.newInstance(input.toString(), m.get()));
    }/*from w  ww .  j  a  v a2  s .com*/
    return Optional.absent();
}

From source file:net.pterodactylus.sonitus.io.FlacIdentifier.java

/**
 * Tries to identify the FLAC file contained in the given stream.
 *
 * @param inputStream/*from   www.  j  a v a  2s.c  o m*/
 *       The input stream
 * @return The identified metadata, or {@link Optional#absent()} if the
 *         metadata can not be identified
 * @throws IOException
 *       if an I/O error occurs
 */
public static Optional<Metadata> identify(InputStream inputStream) throws IOException {
    Optional<Stream> stream = Stream.parse(inputStream);
    if (!stream.isPresent()) {
        return Optional.absent();
    }

    List<MetadataBlock> streamInfos = stream.get().metadataBlocks(STREAMINFO);
    if (streamInfos.isEmpty()) {
        /* FLAC file without STREAMINFO is invalid. */
        return Optional.absent();
    }

    MetadataBlock streamInfoBlock = streamInfos.get(0);
    StreamInfo streamInfo = (StreamInfo) streamInfoBlock.data();

    return Optional
            .of(new Metadata(new FormatMetadata(streamInfo.numberOfChannels(), streamInfo.sampleRate(), "FLAC"),
                    new ContentMetadata()));
}

From source file:com.complexible.common.base.Optionals.java

public static <T> Predicate<Optional<T>> present() {
    return new Predicate<Optional<T>>() {
        @Override/*from  ww w . j a va  2s.  com*/
        public boolean apply(final Optional<T> input) {
            return input.isPresent();
        }
    };
}

From source file:org.opendaylight.groupbasedpolicy.util.DataStoreHelper.java

/**
 * If an element on the path exists in datastore the element is removed and returned as a result.
 * {@link Optional#isPresent()} is {@code false} in case that element on path does not exist.
 * @return removed element in {@link Optional#get()}; otherwise {@link Optional#absent()}
 *///from   ww w .j  a  v  a 2s . com
public static <T extends DataObject> Optional<T> removeIfExists(LogicalDatastoreType store,
        InstanceIdentifier<T> path, ReadWriteTransaction rwTx) {
    Optional<T> potentialResult = readFromDs(store, path, rwTx);
    if (potentialResult.isPresent()) {
        rwTx.delete(store, path);
    }
    return potentialResult;
}

From source file:info.gehrels.voting.OptionalMatchers.java

public static Matcher<Optional<?>> anAbsentOptional() {
    return new TypeSafeDiagnosingMatcher<Optional<?>>() {
        @Override//from  www  . j  a  v a 2 s.c om
        protected boolean matchesSafely(Optional<?> item, Description mismatchDescription) {
            if (item.isPresent()) {
                mismatchDescription.appendText("an Optional whose value was ").appendValue(item.get());
                return false;
            }

            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("an absent Optional");
        }
    };
}

From source file:io.crate.planner.Limits.java

public static Optional<Symbol> mergeMin(Optional<Symbol> limit1, Optional<Symbol> limit2) {
    if (limit1.isPresent()) {
        if (limit2.isPresent()) {
            return Optional
                    .of(new Function(LeastFunction.TWO_LONG_INFO, Arrays.asList(limit1.get(), limit2.get())));
        }/*from  w  w  w  . j av  a2  s .c  o  m*/
        return limit1;
    }
    return limit2;
}

From source file:com.facebook.buck.cxx.CxxCompilableEnhancer.java

/**
 * Resolve the map of names to SourcePaths to a map of names to CxxSource objects.
 *///w w  w  .  j a v a  2s. c  o  m
public static ImmutableMap<String, CxxSource> resolveCxxSources(ImmutableMap<String, SourceWithFlags> sources) {

    ImmutableMap.Builder<String, CxxSource> cxxSources = ImmutableMap.builder();

    // For each entry in the input C/C++ source, build a CxxSource object to wrap
    // it's name, input path, and output object file path.
    for (ImmutableMap.Entry<String, SourceWithFlags> ent : sources.entrySet()) {
        String extension = Files.getFileExtension(ent.getKey());
        Optional<CxxSource.Type> type = CxxSource.Type.fromExtension(extension);
        if (!type.isPresent()) {
            throw new HumanReadableException("invalid extension \"%s\": %s", extension, ent.getKey());
        }
        cxxSources.put(ent.getKey(),
                CxxSource.of(type.get(), ent.getValue().getSourcePath(), ent.getValue().getFlags()));
    }

    return cxxSources.build();
}

From source file:org.opendaylight.faas.fabric.utils.InterfaceManager.java

public static LportAttribute getLogicalPortAttr(DataBroker broker, InstanceIdentifier<TerminationPoint> iid) {
    ReadTransaction rt = broker.newReadOnlyTransaction();
    Optional<TerminationPoint> opt = MdSalUtils.syncReadOper(rt, iid);
    if (opt.isPresent()) {
        TerminationPoint tp = opt.get();
        return tp.getAugmentation(LogicalPortAugment.class).getLportAttribute();
    }/* w  w w . j  av a2s .com*/
    return null;
}

From source file:org.brooklyncentral.catalog.scrape.CatalogItemScraper.java

@SuppressWarnings("unchecked")
private static List<String> parseDirectoryYaml(String repoUrl) {
    Optional<String> directoryYamlString = getGithubRawText(repoUrl, "directory.yaml");

    if (!directoryYamlString.isPresent()) {
        throw new IllegalStateException("Failed to read catalog directory.");
    }/*ww  w. j ava 2 s .c  o m*/

    return (List<String>) Yamls.parseAll(directoryYamlString.get()).iterator().next();
}

From source file:com.complexible.common.base.Optionals.java

public static <T> List<T> all(final Collection<Optional<T>> theCollection) {
    List<T> aList = Lists.newArrayList();
    for (Optional<T> aOpt : theCollection) {
        if (aOpt.isPresent()) {
            aList.add(aOpt.get());// w  w  w  . jav a2s.  co  m
        }
    }

    return aList;
}