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:li.klass.fhem.behavior.dim.DimmableBehavior.java

public static Optional<DimmableBehavior> behaviorFor(FhemDevice fhemDevice) {
    SetList setList = fhemDevice.getSetList();

    Optional<DiscreteDimmableBehavior> discrete = DiscreteDimmableBehavior.behaviorFor(setList);
    if (discrete.isPresent()) {
        return Optional.of(new DimmableBehavior(fhemDevice, discrete.get()));
    }//from w w  w  .  j ava 2  s .c  o m

    Optional<ContinuousDimmableBehavior> continuous = ContinuousDimmableBehavior.behaviorFor(setList);
    if (continuous.isPresent()) {
        DimmableTypeBehavior behavior = continuous.get();
        return Optional.of(new DimmableBehavior(fhemDevice, behavior));
    }

    return Optional.absent();
}

From source file:org.n52.sos.util.I18NHelper.java

/**
 * Add offering description to {@link SosOffering} for the specific language
 * or the configured default language//from  w w w.j  ava 2s  .c o  m
 *
 * @param offering
 *            {@link SosOffering} to add description
 * @param locale
 *            the specific language
 */
public static void addOfferingDescription(SosOffering offering, Locale locale) {
    MultilingualString descriptions = getCache().getI18nDescriptionsForOffering(offering.getIdentifier());
    if (descriptions != null) {
        Optional<LocalizedString> description = descriptions.getLocalizationOrDefault(locale);
        if (description.isPresent()) {
            offering.setDescription(description.get().getText());
        }
    }
}

From source file:com.eucalyptus.util.async.AsyncRequests.java

private static <A extends BaseMessage, B extends BaseMessage> B sendSync(final ServiceConfiguration config,
        final Optional<CallerContext> callerContext, final A msg) throws Exception {
    if (callerContext.isPresent()) {
        callerContext.get().apply(msg);
    }/*from  ww  w . j av a  2s .  c  o  m*/

    if (config.isVmLocal()) {
        return ServiceContext.send(config.getComponentId(), msg);
    } else {
        try {
            Request<A, B> req = newRequest(new MessageCallback<A, B>() {
                {
                    this.setRequest(msg);
                }

                @Override
                public void fire(B msg) {
                    Logs.extreme().debug(msg.toSimpleString());
                }
            });
            return req.sendSync(config);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw ex;
        }
    }
}

From source file:google.registry.flows.host.HostFlowUtils.java

/** Checks that a host name is valid. */
static InternetDomainName validateHostName(String name) throws EppException {
    checkArgumentNotNull(name, "Must specify host name to validate");
    if (name.length() > 253) {
        throw new HostNameTooLongException();
    }// w  ww  .j av a 2s  . c o  m
    String hostNameLowerCase = Ascii.toLowerCase(name);
    if (!name.equals(hostNameLowerCase)) {
        throw new HostNameNotLowerCaseException(hostNameLowerCase);
    }
    try {
        String hostNamePunyCoded = Idn.toASCII(name);
        if (!name.equals(hostNamePunyCoded)) {
            throw new HostNameNotPunyCodedException(hostNamePunyCoded);
        }
        InternetDomainName hostName = InternetDomainName.from(name);
        if (!name.equals(hostName.toString())) {
            throw new HostNameNotNormalizedException(hostName.toString());
        }
        // Checks whether a hostname is deep enough. Technically a host can be just one under a
        // public suffix (e.g. example.com) but we require by policy that it has to be at least one
        // part beyond that (e.g. ns1.example.com). The public suffix list includes all current
        // ccTlds, so this check requires 4+ parts if it's a ccTld that doesn't delegate second
        // level domains, such as .co.uk. But the list does not include new tlds, so in that case
        // we just ensure 3+ parts. In the particular case where our own tld has a '.' in it, we know
        // that there need to be 4 parts as well.
        if (hostName.isUnderPublicSuffix()) {
            if (hostName.parent().isUnderPublicSuffix()) {
                return hostName;
            }
        } else {
            // We need to know how many parts the hostname has beyond the public suffix, but we don't
            // know what the public suffix is. If the host is in bailiwick and we are hosting a
            // multipart "tld" like .co.uk the publix suffix might be 2 parts. Otherwise it's an
            // unrecognized tld that's not on the public suffix list, so assume the tld alone is the
            // public suffix.
            Optional<InternetDomainName> tldParsed = findTldForName(hostName);
            int suffixSize = tldParsed.isPresent() ? tldParsed.get().parts().size() : 1;
            if (hostName.parts().size() >= suffixSize + 2) {
                return hostName;
            }
        }
        throw new HostNameTooShallowException();
    } catch (IllegalArgumentException e) {
        throw new InvalidHostNameException();
    }
}

From source file:org.blockartistry.mod.ThermalRecycling.data.RecipeHelper.java

/**
 * Adds the given recipe to the tracking tables. It assumes control over
 * output.//from  w  ww .ja  va2s.c o m
 */
public static int put(final ItemStack input, List<ItemStack> output) {
    assert input != null;
    assert output != null;

    int retCode = DUPLICATE;

    // See if we have an existing mapping
    final RecipeData result = ItemRegistry.getRecipe(input);

    // Use the incoming recipe if:
    // * It doesn't exist
    // * Existing entry is wildcard and the new one isn't
    // * The new entry has a quantity greater than the existing one
    if (result == RecipeData.EPHEMERAL || (result.isGeneric() && !OreDictionaryHelper.isGeneric(input))
            || (input.stackSize > result.getMinimumInputQuantityRequired())) {

        final ItemStack stack = input.copy();

        // An immutable list has already been processed by
        // something like RecipeDecomposition
        if (!(output instanceof ImmutableList)) {
            // Traverse the list replacing WILDCARD stacks with concrete
            // ones. The logic prefers Thermal Foundation equivalents
            // if found.
            for (int i = 0; i < output.size(); i++) {

                ItemStack working = output.get(i);

                if (OreDictionaryHelper.isGeneric(working)) {
                    final String oreName = OreDictionaryHelper.getOreName(working);

                    if (oreName != null) {
                        final Optional<ItemStack> t = ItemStackHelper.getItemStack(oreName, working.stackSize);
                        if (t.isPresent())
                            output.set(i, t.get());
                    }
                }
            }

            output = ImmutableList.copyOf(InventoryHelper.coelece(output));
        }

        ItemRegistry.setRecipe(stack, new RecipeData(stack, output));

        retCode = SUCCESS;
    }

    return retCode;
}

From source file:org.eclipse.buildship.core.workspace.internal.ProjectNameUpdater.java

private static void ensureProjectNameIsFree(String normalizedProjectName, Set<OmniEclipseProject> allProjects,
        IProgressMonitor monitor) {// w  ww. jav  a2 s  .  c om
    Optional<IProject> possibleDuplicate = CorePlugin.workspaceOperations()
            .findProjectByName(normalizedProjectName);
    if (possibleDuplicate.isPresent()) {
        IProject duplicate = possibleDuplicate.get();
        if (isScheduledForRenaming(duplicate, allProjects)) {
            renameTemporarily(duplicate, monitor);
        } else {
            String message = String.format("A project with the name %s already exists.", normalizedProjectName);
            throw new GradlePluginsRuntimeException(message);
        }
    }
}

From source file:org.opendaylight.openflowplugin.applications.topology.manager.TopologyManagerUtil.java

static void removeAffectedLinks(final NodeId id, Optional<Topology> topologyOptional,
        ReadWriteTransaction transaction, final InstanceIdentifier<Topology> topology) {
    if (!topologyOptional.isPresent()) {
        return;/*from  w  w  w  .  j a  v a  2s .  c  o m*/
    }

    List<Link> linkList = topologyOptional.get().getLink() != null ? topologyOptional.get().getLink()
            : Collections.<Link>emptyList();
    for (Link link : linkList) {
        if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
            transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
        }
    }
}

From source file:org.opendaylight.openflowplugin.applications.topology.manager.TopologyManagerUtil.java

static void removeAffectedLinks(final TpId id, Optional<Topology> topologyOptional,
        ReadWriteTransaction transaction, final InstanceIdentifier<Topology> topology) {
    if (!topologyOptional.isPresent()) {
        return;//from  ww w .j av  a2  s. c o  m
    }

    List<Link> linkList = topologyOptional.get().getLink() != null ? topologyOptional.get().getLink()
            : Collections.<Link>emptyList();
    for (Link link : linkList) {
        if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
            transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link, topology));
        }
    }
}

From source file:springfox.documentation.spring.web.readers.operation.ResponseMessagesReader.java

public static String message(OperationContext context) {
    Optional<ResponseStatus> responseStatus = context.findAnnotation(ResponseStatus.class);
    String reasonPhrase = HttpStatus.OK.getReasonPhrase();
    if (responseStatus.isPresent()) {
        reasonPhrase = responseStatus.get().reason();
        if (reasonPhrase.isEmpty()) {
            reasonPhrase = responseStatus.get().value().getReasonPhrase();
        }//from   w  w w. j  a  v  a2 s . c o  m
    }
    return reasonPhrase;
}

From source file:ninja.Results.java

public static Result created(Optional<String> url) {
    Result result = status(Result.SC_201_CREATED);
    if (url.isPresent()) {
        result.addHeader(Result.LOCATION, url.get());
    }//from  www  . j a  v  a  2 s . c o m

    return result;
}