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

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

Introduction

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

Prototype

public abstract boolean isPresent();

Source Link

Document

Returns true if this holder contains a (non-null) instance.

Usage

From source file:org.dswarm.graph.gdm.utils.NodeTypeUtils.java

public static Optional<NodeType> getNodeTypeByGDMNodeType(
        final Optional<org.dswarm.graph.json.NodeType> optionalNodeType,
        final Optional<Boolean> optionalIsType) {

    if (!optionalNodeType.isPresent()) {

        return Optional.absent();
    }/*from   ww  w . ja  v  a2 s.  co  m*/

    final NodeType nodeType;

    switch (optionalNodeType.get()) {

    case Literal:

        nodeType = NodeType.Literal;

        break;
    case Resource:

        if (optionalIsType.isPresent()) {

            if (Boolean.FALSE.equals(optionalIsType.get())) {

                nodeType = NodeType.Resource;
            } else {

                nodeType = NodeType.TypeResource;
            }
        } else {

            nodeType = NodeType.Resource;
        }

        break;
    case BNode:

        if (optionalIsType.isPresent()) {

            if (Boolean.FALSE.equals(optionalIsType.get())) {

                nodeType = NodeType.BNode;
            } else {

                nodeType = NodeType.TypeBNode;
            }
        } else {

            nodeType = NodeType.BNode;
        }

        break;
    default:

        nodeType = null;
    }

    return Optional.fromNullable(nodeType);
}

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

private static NodeConnector readNodeConnector(NodeConnectorRef ncRef) {
    NodeConnector nc = null;// w  ww .ja  v  a  2s.c  o m
    ReadOnlyTransaction readTx = LacpUtil.getDataBrokerService().newReadOnlyTransaction();

    try {
        Optional<NodeConnector> dataObject = readTx
                .read(LogicalDatastoreType.OPERATIONAL, (InstanceIdentifier<NodeConnector>) ncRef.getValue())
                .get();
        if (dataObject.isPresent()) {
            nc = (NodeConnector) dataObject.get();
        }
    } catch (Exception e) {
        readTx.close();
    }
    readTx.close();
    return nc;
}

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

public static NodeConnector getNodeConnector(DataBroker dataService, InstanceIdentifier<NodeConnector> ncId) {
    NodeConnector nc = null;/*from ww w .  jav a  2 s.  c o m*/
    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.sonar.server.computation.task.projectanalysis.qualitymodel.NewMaintainabilityMeasuresVisitor.java

private static long getLongValue(Optional<Measure> measure) {
    if (!measure.isPresent()) {
        return 0L;
    }//from www. j  av  a 2 s . c  om
    return getLongValue(measure.get());
}

From source file:com.palantir.atlasdb.keyvalue.cassandra.jmx.CassandraJmxCompaction.java

public static Optional<CassandraJmxCompactionManager> createJmxCompactionManager(
        CassandraKeyValueServiceConfigManager configManager) {
    Preconditions.checkNotNull(configManager);
    CassandraKeyValueServiceConfig config = configManager.getConfig();
    CassandraJmxCompaction jmxCompaction = new CassandraJmxCompaction(config);

    Optional<CassandraJmxCompactionConfig> jmxConfig = config.jmx();
    // need to set the property before creating the JMX compaction client
    if (!jmxConfig.isPresent()) {
        log.info("Jmx compaction is not enabled.");
        return Optional.absent();
    }/*from ww  w.j  av  a  2s  .  c  o  m*/

    jmxCompaction.setJmxSslProperty(jmxConfig.get());
    ImmutableSet<CassandraJmxCompactionClient> clients = jmxCompaction.createCompactionClients(jmxConfig.get());
    ExecutorService exec = Executors.newFixedThreadPool(clients.size(),
            new ThreadFactoryBuilder().setNameFormat("Cassandra-Jmx-Compaction-ThreadPool-%d").build());

    return Optional.of(CassandraJmxCompactionManager.create(clients, exec));
}

From source file:org.opendaylight.protocol.bgp.labeled.unicast.AbstractLabeledUnicastRIBSupport.java

public static List<LabelStack> extractLabel(final DataContainerNode<? extends PathArgument> route,
        final NodeIdentifier labelStackNid, final NodeIdentifier labelValueNid) {
    final List<LabelStack> labels = new ArrayList<>();
    final Optional<DataContainerChild<? extends PathArgument, ?>> labelStacks = route.getChild(labelStackNid);
    if (labelStacks.isPresent()) {
        for (final UnkeyedListEntryNode label : ((UnkeyedListNode) labelStacks.get()).getValue()) {
            final Optional<DataContainerChild<? extends PathArgument, ?>> labelStack = label
                    .getChild(labelValueNid);
            if (labelStack.isPresent()) {
                final LabelStackBuilder labelStackbuilder = new LabelStackBuilder();
                labelStackbuilder.setLabelValue(new MplsLabel((Long) labelStack.get().getValue()));
                labels.add(labelStackbuilder.build());
            }/*from   www . j  ava2s .  c  o m*/
        }
    }
    return labels;
}

From source file:org.fabrician.enabler.util.DockerfileBuildLock.java

/**
 * Attempt to acquire a Docker file build log with multiples tries
 * //  w w  w.j  a  va2 s  . com
 * @param dockerImageName
 *            docker image name to create . ex. "joe/sshd:1.0.1"
 * @param dockerFilePath
 *            file path to the Dockerfile to use for build
 * @param maxRetries
 *            max number of retries to acquire lock
 * @param retryPause
 *            pause in secs between retries
 * @return Optional<DockerfileBuildLock>
 * @throws InterruptedException
 */
public static Optional<DockerfileBuildLock> acquire(String dockerImageName, File dockerFilePath, int maxRetries,
        int retryPause) throws InterruptedException {
    for (int i = 0; i < maxRetries; i++) {
        Optional<DockerfileBuildLock> lock = DockerfileBuildLock.acquire(dockerImageName, dockerFilePath);
        if (lock.isPresent()) {
            return lock;
        }
        try {
            TimeUnit.SECONDS.sleep(retryPause);
        } catch (InterruptedException ex) {
            logger.warning("Retry pause interrupted.");
            throw ex;
        }
    }
    logger.severe("Failed to acquire build lock associated with image [" + dockerImageName
            + "] for Dockerfile [" + dockerFilePath + "]");
    return Optional.absent();
}

From source file:org.opendaylight.netconf.notifications.impl.ops.CreateSubscription.java

private static StreamNameType parseStreamIfPresent(final XmlElement operationElement)
        throws DocumentedException {
    final Optional<XmlElement> stream = operationElement
            .getOnlyChildElementWithSameNamespaceOptionally("stream");
    return stream.isPresent() ? new StreamNameType(stream.get().getTextContent())
            : NetconfNotificationManager.BASE_STREAM_NAME;
}

From source file:org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes.java

public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent,
        final Iterable<PathArgument> relativePath) {
    checkNotNull(parent, "Parent must not be null");
    checkNotNull(relativePath, "Relative path must not be null");

    Optional<NormalizedNode<?, ?>> currentNode = parent;
    final Iterator<PathArgument> pathIterator = relativePath.iterator();
    while (currentNode.isPresent() && pathIterator.hasNext()) {
        currentNode = getDirectChild(currentNode.get(), pathIterator.next());
    }//  w w  w  .ja  v a  2s.  c o  m
    return currentNode;
}

From source file:net.caseif.flint.steel.util.helper.PlayerHelper.java

/**
 * Pops the given {@link Player}'s stored location from persistent storage,
 * teleporting them to it./*w  w w  .  j av  a  2s .c om*/
 *
 * @param player The {@link Player} to load the location of and teleport
 * @throws IllegalArgumentException If the player's location is not present
 *     in the persistent store or if an error occurs during deserialization
 * @throws InvalidConfigurationException If an exception occurs while
 *     loading from disk
 * @throws IOException If an exception occurs while saving to disk
 */
public static void popLocation(Player player)
        throws IllegalArgumentException, InvalidConfigurationException, IOException {
    Optional<Location3D> retLoc = CommonPlayerHelper.getReturnLocation(player.getUniqueId());
    if (!retLoc.isPresent()) {
        throw new IllegalArgumentException(
                "Location of player " + player.getName() + " not present in persistent store");
    }
    player.teleport(LocationHelper.convertLocation(retLoc.get()));
}