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.eclipse.recommenders.utils.rcp.ast.BindingUtils.java

public static List<IPackageName> toPackageNames(final ITypeBinding[] types) {
    final List<IPackageName> res = Lists.newLinkedList();
    for (final ITypeBinding b : types) {
        final Optional<IPackageName> opt = toPackageName(b);
        if (opt.isPresent()) {
            res.add(opt.get());
        }/*from  www  .  j  a v  a 2  s .c o m*/
    }
    return res;
}

From source file:org.opendaylight.netconf.util.messages.NetconfMessageUtil.java

public static Collection<String> extractCapabilitiesFromHello(Document doc) throws NetconfDocumentedException {
    XmlElement responseElement = XmlElement.fromDomDocument(doc);
    // Extract child element <capabilities> from <hello> with or without(fallback) the same namespace
    Optional<XmlElement> capabilitiesElement = responseElement
            .getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES)
            .or(responseElement.getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES));

    List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
    return Collections2.transform(caps, new Function<XmlElement, String>() {

        @Override//from www . j a  v  a  2  s.c o  m
        public String apply(@Nonnull XmlElement input) {
            // Trim possible leading/tailing whitespace
            try {
                return input.getTextContent().trim();
            } catch (DocumentedException e) {
                LOG.trace("Error fetching input text content", e);
                return null;
            }
        }
    });

}

From source file:org.eclipse.mylyn.internal.wikitext.commonmark.inlines.InlineParser.java

static List<Inline> secondPass(List<Inline> inlines) {
    List<Inline> processedInlines = ImmutableList.copyOf(inlines);
    Optional<InlinesSubstitution> substitution = Optional.absent();
    do {//from www.j  a v  a 2s  .c  o m
        for (Inline inline : processedInlines) {
            substitution = inline.secondPass(processedInlines);
            if (substitution.isPresent()) {
                processedInlines = substitution.get().apply(processedInlines);
                break;
            }
        }
    } while (substitution.isPresent());
    return processedInlines;
}

From source file:com.facebook.buck.step.StepFailedException.java

static StepFailedException createForFailingStepWithExitCode(Step step, ExecutionContext context, int exitCode,
        Optional<BuildTarget> buildTarget) {
    String nameOrDescription = context.getVerbosity().shouldPrintCommand() ? step.getDescription(context)
            : step.getShortName();//  ww w.j a va  2s.c  om
    String message;
    if (buildTarget.isPresent()) {
        message = String.format("%s failed with exit code %d:\n%s", buildTarget.get().getFullyQualifiedName(),
                exitCode, nameOrDescription);
    } else {
        message = String.format("Failed with exit code %d:\n%s", exitCode, nameOrDescription);
    }
    return new StepFailedException(message, step, exitCode);
}

From source file:org.opendaylight.controller.md.sal.dom.store.impl.replica.ResolveDataChangeState.java

private static void addChildNodes(final List<Node> result, final Collection<Node> parentNodes,
        final PathArgument childIdentifier) {
    for (Node node : parentNodes) {
        Optional<Node> child = node.getChild(childIdentifier);
        if (child.isPresent()) {
            result.add(child.get());
        }//from www  .  ja va2  s .  c o  m
    }
}

From source file:org.apache.brooklyn.util.core.flags.MethodCoercions.java

/**
 * Tries to find a single-parameter method with a parameter compatible with (can be coerced to) the argument, and
 * invokes it./* ww  w .  j  a va2 s. c om*/
 *
 * @param instance the object to invoke the method on
 * @param methodName the name of the method to invoke
 * @param argument the argument to the method's parameter.
 * @return the result of the method call, or {@link org.apache.brooklyn.util.guava.Maybe#absent()} if method could not be matched.
 */
public static Maybe<?> tryFindAndInvokeSingleParameterMethod(final Object instance, final String methodName,
        final Object argument) {
    Class<?> clazz = instance.getClass();
    Iterable<Method> methods = Arrays.asList(clazz.getMethods());
    Optional<Method> matchingMethod = Iterables.tryFind(methods,
            matchSingleParameterMethod(methodName, argument));
    if (matchingMethod.isPresent()) {
        Method method = matchingMethod.get();
        try {
            Type paramType = method.getGenericParameterTypes()[0];
            Object coercedArgument = TypeCoercions.coerce(argument, TypeToken.of(paramType));
            return Maybe.of(method.invoke(instance, coercedArgument));
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw Exceptions.propagate(e);
        }
    } else {
        return Maybe.absent();
    }
}

From source file:org.opendaylight.protocol.bgp.flowspec.ipv4.FlowspecIpv4NlriParserHelper.java

private static final List<ProtocolIps> createProtocolsIps(final UnkeyedListNode protocolIpsData) {
    final List<ProtocolIps> protocolIps = new ArrayList<>();

    for (final UnkeyedListEntryNode node : protocolIpsData.getValue()) {
        final ProtocolIpsBuilder ipsBuilder = new ProtocolIpsBuilder();
        final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node
                .getChild(AbstractFlowspecNlriParser.OP_NID);
        if (opValue.isPresent()) {
            ipsBuilder//  w w  w.  j a  va2s. c  o  m
                    .setOp(NumericOneByteOperandParser.INSTANCE.create((Set<String>) opValue.get().getValue()));
        }
        final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node
                .getChild(AbstractFlowspecNlriParser.VALUE_NID);
        if (valueNode.isPresent()) {
            ipsBuilder.setValue((Short) valueNode.get().getValue());
        }
        protocolIps.add(ipsBuilder.build());
    }

    return protocolIps;
}

From source file:org.spongepowered.common.service.scheduler.SpongeScheduler.java

/**
 * Check the object is a plugin instance.
 *
 * @param plugin The plugin to check/*from ww w.  j a  v  a  2 s  .c o m*/
 * @return The plugin container of the plugin instance
 * @throws NullPointerException If the passed in plugin instance is null
 * @throws IllegalArgumentException If the object is not a plugin instance
 */
static PluginContainer checkPluginInstance(Object plugin) {
    Optional<PluginContainer> optPlugin = Sponge.getGame().getPluginManager()
            .fromInstance(checkNotNull(plugin, "plugin"));
    checkArgument(optPlugin.isPresent(), "Provided object is not a plugin instance");
    return optPlugin.get();
}

From source file:org.killbill.billing.plugin.adyen.api.mapping.UserDataMappingService.java

public static UserData toUserData(@Nullable final Account account, final Iterable<PluginProperty> properties) {
    final UserData userData = new UserData();

    // determine the customer id
    final String customerIdProperty = PluginProperties
            .findPluginPropertyValue(AdyenPaymentPluginApi.PROPERTY_CUSTOMER_ID, properties);
    final Optional<String> optionalCustomerId = toCustomerId(customerIdProperty, account);
    final String customerId = optionalCustomerId.isPresent() ? optionalCustomerId.get() : null;
    userData.setShopperReference(customerId);

    // determine the customer locale
    final String propertyLocaleString = PluginProperties
            .findPluginPropertyValue(AdyenPaymentPluginApi.PROPERTY_CUSTOMER_LOCALE, properties);
    final Optional<Locale> customerLocaleOptional = toCustomerLocale(propertyLocaleString, account);
    final Locale customerLocale = customerLocaleOptional.isPresent() ? customerLocaleOptional.get() : null;
    userData.setShopperLocale(customerLocale);

    // determine the email
    final String propertyEmail = PluginProperties.findPluginPropertyValue(AdyenPaymentPluginApi.PROPERTY_EMAIL,
            properties);//from  w ww  . ja  va 2  s  . co m
    final Optional<String> optionalEmail = toCustomerEmail(propertyEmail, account);
    final String email = optionalEmail.isPresent() ? optionalEmail.get() : null;
    userData.setShopperEmail(email);

    // determine first Name
    final String propertyFirstName = PluginProperties
            .findPluginPropertyValue(AdyenPaymentPluginApi.PROPERTY_FIRST_NAME, properties);
    final Optional<String> optionalFirstName = toFirstName(propertyFirstName, account);
    final String firstName = optionalFirstName.isPresent() ? optionalFirstName.get() : null;
    userData.setFirstName(firstName);

    // determine last Name
    final String propertyLastName = PluginProperties
            .findPluginPropertyValue(AdyenPaymentPluginApi.PROPERTY_LAST_NAME, properties);
    final Optional<String> optionalLastName = toLastName(propertyLastName, account);
    final String lastName = optionalLastName.isPresent() ? optionalLastName.get() : null;
    userData.setLastName(lastName);

    // set ip
    userData.setShopperIP(
            PluginProperties.findPluginPropertyValue(AdyenPaymentPluginApi.PROPERTY_IP, properties));

    return userData;
}

From source file:gobblin.util.ExecutorsUtils.java

private static ThreadFactory newThreadFactory(ThreadFactoryBuilder builder, Optional<Logger> logger,
        Optional<String> nameFormat) {
    if (nameFormat.isPresent()) {
        builder.setNameFormat(nameFormat.get());
    }//from ww  w .jav a2  s  . c om
    return builder.setUncaughtExceptionHandler(new LoggingUncaughtExceptionHandler(logger)).build();
}