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:com.dangdang.ddframe.job.internal.reg.RegistryCenterFactory.java

/**
 * .// w ww  .  ja va 2 s .  c o m
 *
 * @param connectString 
 * @param namespace ??
 * @param digest ?
 * @return 
 */
public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(final String connectString,
        final String namespace, final Optional<String> digest) {
    Hasher hasher = Hashing.md5().newHasher().putString(connectString, Charsets.UTF_8).putString(namespace,
            Charsets.UTF_8);
    if (digest.isPresent()) {
        hasher.putString(digest.get(), Charsets.UTF_8);
    }
    HashCode hashCode = hasher.hash();
    if (registryCenterMap.containsKey(hashCode)) {
        return registryCenterMap.get(hashCode);
    }
    ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
    if (digest.isPresent()) {
        zkConfig.setDigest(digest.get());
    }
    CoordinatorRegistryCenter result = new ZookeeperRegistryCenter(zkConfig);
    result.init();
    registryCenterMap.putIfAbsent(hashCode, result);
    return result;
}

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  ww  w  .  j a  v  a2 s  . co m*/
        return projectNode.getEclipseProject().getProjectDirectory().getAbsolutePath();
    }
}

From source file:org.opendaylight.protocol.pcep.spi.AbstractMessageParser.java

public static Message createErrorMsg(final PCEPErrors e, final Optional<Rp> rp) {
    final PcerrMessageBuilder msgBuilder = new PcerrMessageBuilder();
    if (rp.isPresent()) {
        new RequestCaseBuilder()
                .setRequest(new RequestBuilder()
                        .setRps(Collections.singletonList(new RpsBuilder().setRp(rp.get()).build())).build())
                .build();/*from w  w  w .j av a  2  s.c  o m*/
    }
    return new PcerrBuilder().setPcerrMessage(msgBuilder.setErrors(Arrays.asList(new ErrorsBuilder()
            .setErrorObject(
                    new ErrorObjectBuilder().setType(e.getErrorType()).setValue(e.getErrorValue()).build())
            .build())).build()).build();
}

From source file:com.dangdang.ddframe.job.lite.internal.reg.RegistryCenterFactory.java

/**
 * ./*from   w  w w  . j av  a  2s.co  m*/
 *
 * @param connectString 
 * @param namespace ??
 * @param digest ?
 * @return 
 */
public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(final String connectString,
        final String namespace, final Optional<String> digest) {
    Hasher hasher = Hashing.md5().newHasher().putString(connectString, Charsets.UTF_8).putString(namespace,
            Charsets.UTF_8);
    if (digest.isPresent()) {
        hasher.putString(digest.get(), Charsets.UTF_8);
    }
    HashCode hashCode = hasher.hash();
    if (regCenterMap.containsKey(hashCode)) {
        return regCenterMap.get(hashCode);
    }
    ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
    if (digest.isPresent()) {
        zkConfig.setDigest(digest.get());
    }
    CoordinatorRegistryCenter result = new ZookeeperRegistryCenter(zkConfig);
    result.init();
    regCenterMap.putIfAbsent(hashCode, result);
    return result;
}

From source file:org.opendaylight.controller.netconf.util.xml.XmlUtil.java

public static Element createElement(final Document document, final String qName,
        final Optional<String> namespaceURI) {
    if (namespaceURI.isPresent()) {
        final Element element = document.createElementNS(namespaceURI.get(), qName);
        String name = XMLNS_ATTRIBUTE_KEY;
        if (element.getPrefix() != null) {
            name += ":" + element.getPrefix();
        }//  w  ww.ja  va  2s. c om
        element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
        return element;
    }
    return document.createElement(qName);
}

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 {/*  ww w .j a  v a 2  s.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 Optional<FixedRequestAttributes> getFixedRequestAttributes(ProjectNode projectNode) {
    Optional<IProject> workspaceProject = projectNode.getWorkspaceProject();
    if (workspaceProject.isPresent() && GradleProjectNature.isPresentOn(workspaceProject.get())) {
        ProjectConfiguration projectConfiguration = CorePlugin.projectConfigurationManager()
                .readProjectConfiguration(workspaceProject.get());
        return Optional
                .of(projectConfiguration.toRequestAttributes(ConversionStrategy.IGNORE_WORKSPACE_SETTINGS));
    } else {/*from w w w . j  a  va2 s .  co  m*/
        return Optional.absent();
    }
}

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();
        }//www. j  a v  a 2 s  .c  o  m
    }
    return reasonPhrase;
}

From source file:org.apache.gobblin.runtime.JobLauncherFactory.java

/**
 * Creates a new instance for a JobLauncher with a given type
 * @param sysProps          the system/environment properties
 * @param jobProps          the job properties
 * @param launcherTypeValue the type of the launcher; either a {@link JobLauncherType} value or
 *        the name of the class that extends {@link AbstractJobLauncher} and has a constructor
 *        that has a single Properties parameter..
 * @return the JobLauncher instance//from www.j a  va2 s  . c  o m
 * @throws RuntimeException if the instantiation fails
 */
public static JobLauncher newJobLauncher(Properties sysProps, Properties jobProps, String launcherTypeValue,
        SharedResourcesBroker<GobblinScopeTypes> instanceBroker) {
    Optional<JobLauncherType> launcherType = Enums.getIfPresent(JobLauncherType.class, launcherTypeValue);

    try {
        if (launcherType.isPresent()) {
            switch (launcherType.get()) {
            case LOCAL:
                return new LocalJobLauncher(
                        JobConfigurationUtils.combineSysAndJobProperties(sysProps, jobProps), instanceBroker);
            case MAPREDUCE:
                return new MRJobLauncher(JobConfigurationUtils.combineSysAndJobProperties(sysProps, jobProps),
                        instanceBroker);
            default:
                throw new RuntimeException("Unsupported job launcher type: " + launcherType.get().name());
            }
        }

        @SuppressWarnings("unchecked")
        Class<? extends AbstractJobLauncher> launcherClass = (Class<? extends AbstractJobLauncher>) Class
                .forName(launcherTypeValue);
        return launcherClass.getDeclaredConstructor(Properties.class)
                .newInstance(JobConfigurationUtils.combineSysAndJobProperties(sysProps, jobProps));
    } catch (Exception e) {
        throw new RuntimeException("Failed to create job launcher: " + e, e);
    }
}

From source file:org.opendaylight.faas.fabrics.vxlan.adapters.ovs.utils.OfFlowUtils.java

public static Table getTable(NodeBuilder nodeBuilder, short table, ReadOnlyTransaction readTx,
        final LogicalDatastoreType store) {
    try {/*from ww  w .  j a va 2 s .  com*/
        Optional<Table> data = readTx.read(store, createTablePath(nodeBuilder, table)).get();
        if (data.isPresent()) {
            return data.get();
        }
    } catch (InterruptedException | ExecutionException e) {
        LOG.error("Failed to get table {}", table, e);
    }

    LOG.info("Cannot find data for table {} in {}", table, store);
    return null;
}