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:jcomposition.processor.utils.AnnotationUtils.java

@SuppressWarnings("unchecked")
public static IMergeConflictPolicy getCompositionMergeConflictPolicy(TypeElement element,
        ProcessingEnvironment env) {
    Optional<AnnotationValue> value = getParameterFrom(element, Composition.class, "onConflict", env);

    if (value.isPresent()) {
        TypeElement typeElement = MoreTypes.asTypeElement((Type) value.get().getValue());
        try {//from w w  w  . jav a2s .c o  m
            return (IMergeConflictPolicy) Class.forName(typeElement.getQualifiedName().toString())
                    .newInstance();
        } catch (Exception ignore) {
        }
    }

    throw new IncompleteAnnotationException(Composition.class, "onConflict");
}

From source file:org.eclipse.buildship.ui.view.task.TaskNodeSelectionUtils.java

private static String getProjectDirectoryExpression(ProjectNode projectNode) {
    // return the directory as an expression if the project is part of the workspace, otherwise
    // return the absolute path of the project directory available on the Eclipse project model
    Optional<IProject> workspaceProject = projectNode.getWorkspaceProject();
    if (workspaceProject.isPresent()) {
        return ExpressionUtils.encodeWorkspaceLocation(workspaceProject.get());
    } else {//from   w  ww  .  ja v  a 2s.  com
        return projectNode.getEclipseProject().getProjectDirectory().getAbsolutePath();
    }
}

From source file:org.eclipse.viatra.addon.querybasedfeatures.runtime.QueryBasedFeatureSetup.java

private static Set<EStructuralFeature> initializeFeatures(Notifier rootNotifier,
        QueryBasedFeatureSettingDelegateFactory qbfFactory, Set<EStructuralFeature> qbfFeatures) {
    Set<EStructuralFeature> initializedQBFFeatures = Sets.newHashSet();
    for (EStructuralFeature eStructuralFeature : qbfFeatures) {
        Optional<QueryBasedFeatureSettingDelegate> optional = qbfFactory.getSettingDelegate(eStructuralFeature);
        if (optional.isPresent()) {
            QueryBasedFeatureSettingDelegate delegate = optional.get();
            delegate.initializeSettingDelegate(rootNotifier);
            initializedQBFFeatures.add(eStructuralFeature);
        }//from w  w w .  j  a  v a2s . c o m
    }
    return initializedQBFFeatures;
}

From source file:org.opendaylight.lacp.Utils.LacpPortProperties.java

public static NodeConnector getNodeConnector(DataBroker dataService, InstanceIdentifier<NodeConnector> ncId) {
    NodeConnector nc = null;/*  w ww .j a va2 s .  com*/
    LOG.debug("getNodeConnector - Entry");

    ReadOnlyTransaction readTransaction = dataService.newReadOnlyTransaction();

    try {
        Optional<NodeConnector> dataObjectOptional = readTransaction
                .read(LogicalDatastoreType.OPERATIONAL, ncId).get();
        if (dataObjectOptional.isPresent()) {
            nc = (NodeConnector) dataObjectOptional.get();
        }
    } catch (Exception e) {
        LOG.error("Error reading node connector {}", ncId);
        readTransaction.close();
        throw new RuntimeException("Error reading from operational store, node connector : " + ncId, e);
    }
    readTransaction.close();
    if (nc != null) {
        LOG.debug("getNodeConnector - nodeconnector ref obtained sucessfully");
    } else {
        LOG.error("getNodeConnector - nodeconnector ref cannot be obtained");
    }
    LOG.debug("getNodeConnector - Exit");
    return nc;

}

From source file:org.locationtech.geogig.test.integration.remoting.RemotesIndexTestSupport.java

public static List<Ref> getBranches(Repository repo, Optional<String> branch) {
    List<Ref> branches;
    if (branch.isPresent()) {
        Optional<Ref> r = repo.command(RefParse.class).setName(branch.get()).call();
        if (r.isPresent()) {
            branches = Collections.singletonList(r.get());
        } else {//from  w  w w  .ja v a  2 s.  co  m
            return Collections.emptyList();
        }
    } else {
        branches = repo.command(BranchListOp.class).call();
    }
    return branches;
}

From source file:org.opendaylight.netvirt.vpnmanager.utilities.InterfaceUtils.java

public static Optional<String> getMacAddressForInterface(DataBroker dataBroker, String interfaceName) {
    Optional<String> macAddressOptional = Optional.absent();
    InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId = buildStateInterfaceId(
            interfaceName);//from   w  w  w . jav  a 2s  .c  om
    Optional<Interface> ifStateOptional = VpnUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, ifStateId);
    if (ifStateOptional.isPresent()) {
        PhysAddress macAddress = ifStateOptional.get().getPhysAddress();
        if (macAddress != null) {
            macAddressOptional = Optional.of(macAddress.getValue());
        }
    }
    return macAddressOptional;
}

From source file:com.facebook.buck.util.environment.MacWifiSsidFinder.java

private static Optional<String> getSsidFromInterface(Optional<Proxy> defaultInterface) {
    if (!defaultInterface.isPresent()) {
        LOG.debug("No Wi-Fi interface found.");
        return Optional.absent();
    }//from w ww.j av a 2  s  .com
    LOG.debug("Getting SSID from Wi-Fi interface: %s", defaultInterface.get());

    // NSString *ssid = [defaultInterface ssid];
    Object ssid = defaultInterface.get().send("ssid");
    if (ssid == null) {
        LOG.debug("No SSID found for interface %s.", defaultInterface.get());
        return Optional.absent();
    }
    if (!(ssid instanceof String)) {
        LOG.error("Fetched SSID, but got unexpected non-string type (got: %s).", ssid);
        return Optional.absent();
    }

    String ssidString = (String) ssid;
    LOG.debug("Found SSID: %s", ssidString);
    return Optional.of(ssidString);
}

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 va 2s .com*/
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:io.mesosphere.mesos.frameworks.cassandra.ZooKeeperSeedProvider.java

private static Optional<Integer> parseInt(Optional<String> stringOption, String key) {

    if (stringOption.isPresent()) {

        String string = stringOption.get();

        try {/*from w  w w  .j a  v a 2s .c om*/
            return Optional.of(Integer.parseInt(string));

        } catch (NumberFormatException nfe) {

            LOGGER.error(String.format("%s must be set to a valid integer %s provided", key, string), nfe);

            return Optional.absent();
        }
    } else {

        return Optional.absent();
    }
}

From source file:org.sonar.server.computation.task.projectanalysis.component.ComponentRootBuilder.java

private static String createProjectVersion(ScannerReport.Component component, String projectUuid,
        Function<String, Optional<SnapshotDto>> analysisSupplier) {
    String version = trimToNull(component.getVersion());
    if (version != null) {
        return version;
    }/*from   ww  w  .ja  v  a2 s  . c  om*/
    Optional<SnapshotDto> snapshotDto = analysisSupplier.apply(projectUuid);
    if (snapshotDto.isPresent()) {
        return MoreObjects.firstNonNull(snapshotDto.get().getVersion(), DEFAULT_PROJECT_VERSION);
    }
    return DEFAULT_PROJECT_VERSION;
}