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:com.sk89q.worldedit.world.biome.Biomes.java

/**
 * Find a biome that matches the given input name.
 *
 * @param biomes a list of biomes// w  w w  . j  av a 2s . c om
 * @param name the name to test
 * @param registry a biome registry
 * @return a biome or null
 */
@Nullable
public static BaseBiome findBiomeByName(Collection<BaseBiome> biomes, String name, BiomeRegistry registry) {
    checkNotNull(biomes);
    checkNotNull(name);
    checkNotNull(registry);

    Function<String, ? extends Number> compare = new LevenshteinDistance(name, false,
            LevenshteinDistance.STANDARD_CHARS);
    WeightedChoice<BaseBiome> chooser = new WeightedChoice<BaseBiome>(
            Functions.compose(compare, new BiomeName(registry)), 0);
    for (BaseBiome biome : biomes) {
        chooser.consider(biome);
    }
    Optional<Choice<BaseBiome>> choice = chooser.getChoice();
    if (choice.isPresent() && choice.get().getScore() <= 1) {
        return choice.get().getValue();
    } else {
        return null;
    }
}

From source file:org.spongepowered.api.util.text.OptBool.java

/**
 * Coerces the given {@code Optional&lt;Boolean&gt;} into one of the three stored states.
 *
 * @param bool The boolean//w  ww. j  a va 2 s . co m
 * @return The constructed Optional, or {@link Optional#absent()}
 */
public static Optional<Boolean> of(Optional<Boolean> bool) {
    if (bool.isPresent()) {
        return of(bool.get().booleanValue());
    } else {
        return ABSENT;
    }
}

From source file:rickbw.incubator.choice.Choices.java

public static <F, T> Optional<T> map(final Optional<F> from, final Function<? super F, ? extends T> func) {
    if (from.isPresent()) {
        final T result = func.apply(from.get());
        return Optional.of(result);
    } else {//  w w w . j  a va  2  s.c  o m
        return Optional.absent();
    }
}

From source file:com.treasuredata.client.TDClientException.java

private static final String formatErrorMessage(ErrorType errorType, String message, Optional<Exception> cause) {
    String rootCauseErrorMessage = cause.isPresent() ? " The root cause: " + cause.get().getMessage() : "";
    return String.format("[%s] %s%s", errorType.name(), message != null ? message : "", rootCauseErrorMessage);
}

From source file:org.raml.jaxrs.model.HttpVerb.java

/**
 * Same as {@link #fromString(String)}, but throws if there is no corresponding verb.
 *
 * @param httpMethod the method to pair//from  w ww . j av a  2s  .c o m
 * @return the corresponding http verb
 * @throws NoSuchElementException if there is no corresponding verb
 */
public static HttpVerb fromStringUnchecked(String httpMethod) {
    Optional<HttpVerb> verb = fromString(httpMethod);

    if (verb.isPresent())
        return verb.get();

    throw new NoSuchElementException(String.format("invalid http method: %s", httpMethod));
}

From source file:org.opendaylight.netconf.util.osgi.NetconfConfigUtil.java

private static InetSocketAddress parseAddress(final Optional<String> address, final Optional<String> port) {
    final int portNumber = Integer.valueOf(port.get());
    return new InetSocketAddress(address.get(), portNumber);
}

From source file:com.eucalyptus.util.FUtils.java

/**
 * Flatten a nested optional./*from  w  w w . j  ava  2s. co  m*/
 *
 * @param option The optional to flatten
 * @param <T> The resulting optional type
 * @return The optional
 */
@Nonnull
public static <T> Optional<T> flatten(@Nullable final Optional<? extends Optional<T>> option) {
    if (option != null && option.isPresent()) {
        return option.get();
    }
    return Optional.absent();
}

From source file:org.pau.assetmanager.utils.UserUtil.java

/**
 * @return the current user that is logged in in the application
 * WATCH OUT: To call the method the thread should be running in a servlet container in a ZK environment.
 *///from w  ww . j a v  a2s .com
public static User getExecutionUser() {
    String username = Executions.getCurrent().getUserPrincipal().getName();
    Optional<User> optionalUser = UsersBusiness.getUserByUsername(username);
    if (optionalUser.isPresent()) {
        return optionalUser.get();
    } else {
        String message = "The execution user with name '" + username + "' does not exist in the database";
        logger.error(message);
        throw new AssetManagerRuntimeException(message);
    }
}

From source file:org.opendaylight.alto.spce.network.util.DataHelper.java

/**
 * @param dataBroker/*from w w w  . j  a v a  2 s. c om*/
 * @param iid
 * @param type
 * @param <T>
 * @return The data of iid
 * @throws ReadDataFailedException
 */
public static <T extends DataObject> T readFromDataStore(DataBroker dataBroker, InstanceIdentifier<T> iid,
        LogicalDatastoreType type) throws ReadDataFailedException {
    ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction();
    Future<Optional<T>> future = tx.read(type, iid);
    try {
        if (future != null) {
            Optional<T> optional = future.get();
            if (optional.isPresent()) {
                return optional.get();
            }
        }
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    } finally {
        tx.close();
    }
    throw new ReadDataFailedException();
}

From source file:org.sonar.server.permission.PermissionPrivilegeChecker.java

public static void checkProjectAdminUserByComponentDto(UserSession userSession,
        Optional<ComponentDto> project) {
    if (project.isPresent()) {
        checkProjectAdminUserByComponentUuid(userSession, project.get().uuid());
    } else {//from w ww .  j a  v a  2  s  .  c  om
        checkGlobalAdminUser(userSession);
    }
}