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

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

Introduction

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

Prototype

public abstract T get();

Source Link

Document

Returns the contained instance, which must be present.

Usage

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 . ja  v a  2s. c  om
    Optional<String> propertyName = spy.getVersionPropertyName(version);
    if (propertyName.isPresent()) {
        writer.setVersionProperty(propertyName.get(), dependency.getVersion());
    } else {
        writer.setDependencyVersion(dependency);
    }
}

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

/**
 * Tries to identify the FLAC file contained in the given stream.
 *
 * @param inputStream/*from ww  w  .  ja  v a  2  s. co 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:de.azapps.tools.OptionalUtils.java

@Nullable
public static <F, V> V withOptional(@NonNull final Optional<F> optional, @NonNull final Function<F, V> function,
        final V alternative) {
    if (optional.isPresent()) {
        return function.apply(optional.get());
    } else {/*from  www  . ja v  a 2s . c o m*/
        return alternative;
    }
}

From source file:google.registry.flows.FlowReporter.java

/**
 * Returns the set of unique results of {@link #extractTld} applied to each given domain name,
 * excluding any absent results (i.e. cases where no TLD was detected).
 *///w  ww .  j a v a  2 s. c  o m
private static final ImmutableSet<String> extractTlds(Iterable<String> domainNames) {
    ImmutableSet.Builder<String> set = new ImmutableSet.Builder<>();
    for (String domainName : domainNames) {
        Optional<String> extractedTld = extractTld(domainName);
        if (extractedTld.isPresent()) {
            set.add(extractedTld.get());
        }
    }
    return set.build();
}

From source file:org.opendaylight.netvirt.vpnmanager.api.VpnExtraRouteHelper.java

public static List<Routes> getVpnExtraroutes(DataBroker broker, String vpnName, String vpnRd) {
    InstanceIdentifier<ExtraRoutes> vpnExtraRoutesId = getVpnToExtrarouteIdentifier(vpnName, vpnRd);
    Optional<ExtraRoutes> vpnOpc = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, vpnExtraRoutesId);
    return vpnOpc.isPresent() ? vpnOpc.get().getRoutes() : new ArrayList<Routes>();
}

From source file:gobblin.commit.DeliverySemantics.java

/**
 * Get the devliery semantics type from {@link ConfigurationKeys#DELIVERY_SEMANTICS}.
 * The default value is {@link Type#AT_LEAST_ONCE}.
 *///  w  ww. ja  v  a  2 s  . c  om
public static DeliverySemantics parse(State state) {
    String value = state.getProp(ConfigurationKeys.GOBBLIN_RUNTIME_DELIVERY_SEMANTICS, AT_LEAST_ONCE.toString())
            .toUpperCase();
    Optional<DeliverySemantics> semantics = Enums.getIfPresent(DeliverySemantics.class, value);
    Preconditions.checkState(semantics.isPresent(), value + " is not a valid delivery semantics");
    return semantics.get();
}

From source file:org.pau.assetmanager.business.UserClientAssociationBusiness.java

/**
 * @param client of the relationship//from w w  w .  j  a va  2  s.co m
 * @param user of the relationship
 * @return the relationships between 'client' and 'user'
 */
public static UserClientAssociation getUserClientAssociationFromClientAndUser(Client client, User user) {
    String query = "select userClientAssociation from UserClientAssociation userClientAssociation where userClientAssociation.user = :user and userClientAssociation.client = :client";
    Optional<UserClientAssociation> userClientAssociationOptional = DaoFunction
            .<UserClientAssociation>querySimpleUniqueFunction("client", client, "user", user).apply(query);
    return userClientAssociationOptional.get();
}

From source file:org.sonar.server.computation.issue.commonrule.CommentDensityRule.java

private static CommonRuleIssue generateIssue(Optional<Measure> commentDensityMeasure,
        Optional<Measure> commentLinesMeasure, Optional<Measure> nclocMeasure, double minCommentDensity) {
    int commentLines = commentLinesMeasure.isPresent() ? commentLinesMeasure.get().getIntValue() : 0;
    int ncloc = nclocMeasure.get().getIntValue();
    int minExpectedCommentLines = (int) Math.ceil(minCommentDensity * ncloc / (100 - minCommentDensity));
    int missingCommentLines = minExpectedCommentLines - commentLines;
    if (missingCommentLines <= 0) {
        throw new IllegalStateException(format(
                "Bug in measures of comment lines - density=%s, comment_lines= %d, ncloc=%d, threshold=%s%%",
                commentDensityMeasure.get().getDoubleValue(), commentLines, nclocMeasure.get().getIntValue(),
                minCommentDensity));/*  w w  w  . j a v a  2  s  .c  o  m*/
    }

    // TODO declare min threshold as int but not float ?
    String message = format(
            "%d more comment lines need to be written to reach the minimum threshold of %s%% comment density.",
            missingCommentLines, minCommentDensity);
    return new CommonRuleIssue(missingCommentLines, message);
}

From source file:net.caseif.ttt.util.helper.gamemode.BanHelper.java

/**
 * Bans the player by the specified UUID from using TTT for a set amount of time.
 *
 * @param player  the UUID of the player to ban
 * @param minutes the length of time to ban the player for
 *///ww w .j  a va2  s  .  com
public static void ban(UUID player, int minutes) throws InvalidConfigurationException, IOException {
    File f = new File(TTTCore.getPlugin().getDataFolder(), "bans.yml");
    YamlConfiguration y = new YamlConfiguration();
    Player p = Bukkit.getPlayer(player);
    y.load(f);
    long unbanTime = minutes < 0 ? -1 : System.currentTimeMillis() / 1000L + (minutes * 60);
    y.set(player.toString(), unbanTime);
    y.save(f);
    if (p != null) {
        Optional<Challenger> ch = TTTCore.mg.getChallenger(p.getUniqueId());
        if (ch.isPresent()) {
            ch.get().removeFromRound();
        }
    }
}

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

public static Matcher<Optional<?>> anAbsentOptional() {
    return new TypeSafeDiagnosingMatcher<Optional<?>>() {
        @Override/*from  ww  w .ja v  a2 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");
        }
    };
}