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.buildship.core.workspace.internal.JavaSourceSettingsUpdater.java

public static void update(IJavaProject project, OmniJavaSourceSettings sourceSettings, IProgressMonitor monitor)
        throws CoreException {
    SubMonitor progress = SubMonitor.convert(monitor, 1);
    String sourceVersion = sourceSettings.getSourceLanguageLevel().getName();
    String targetVersion = sourceSettings.getTargetBytecodeLevel().getName();
    File vmLocation = sourceSettings.getTargetRuntime().getHomeDirectory();

    IVMInstall vm = EclipseVmUtil.findOrRegisterStandardVM(targetVersion, vmLocation);
    Optional<IExecutionEnvironment> executionEnvironment = EclipseVmUtil
            .findExecutionEnvironment(targetVersion);
    if (executionEnvironment.isPresent()) {
        addExecutionEnvironmentToClasspath(project, executionEnvironment.get(), progress.newChild(1));
    } else {// w  w w .  j  a v a2s. c  om
        addVmToClasspath(project, vm, progress.newChild(1));
    }

    boolean compilerOptionChanged = false;
    compilerOptionChanged |= updateJavaProjectOptionIfNeeded(project, JavaCore.COMPILER_COMPLIANCE,
            sourceVersion);
    compilerOptionChanged |= updateJavaProjectOptionIfNeeded(project, JavaCore.COMPILER_SOURCE, sourceVersion);
    compilerOptionChanged |= updateJavaProjectOptionIfNeeded(project, JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
            targetVersion);

    if (compilerOptionChanged && isProjectAutoBuildingEnabled()) {
        scheduleJdtBuild(project.getProject());
    }
}

From source file:org.sonar.server.computation.queue.InternalCeQueueImpl.java

private static void updateTaskResult(CeActivityDto activityDto, @Nullable CeTaskResult taskResult) {
    if (taskResult != null) {
        java.util.Optional<String> analysisUuid = taskResult.getAnalysisUuid();
        if (analysisUuid.isPresent()) {
            activityDto.setAnalysisUuid(analysisUuid.get());
        }//from w  ww.  j  a  v  a 2 s.  co  m
    }
}

From source file:de.azapps.mirakel.helper.WidgetHelper.java

@NonNull
public static ListMirakel getList(final Context context, final int widgetId) {
    final int listId = getSettings(context).getInt(getKey(widgetId, "list_id"), 0);
    final Optional<ListMirakel> list = ListMirakel.get(listId);
    if (!list.isPresent()) {
        return SpecialList.firstSpecialSafe();
    } else {//from w  ww  .  j  a  v a 2 s.com
        return list.get();
    }
}

From source file:org.deephacks.confit.internal.jpa.JpaProperty.java

private static EntityManager getEmOrFail() {
    Optional<EntityManager> em = getEm();
    if (em.isPresent()) {
        return em.get();
    }// w  w  w .jav  a  2s.c om
    throw JpaEvents.JPA202_MISSING_THREAD_EM();
}

From source file:io.urmia.job.run.Main.java

private static ServiceInstance<NodeType> getMyMatchingODSInstance(ServiceInstance<NodeType> me)
        throws Exception {
    int rc = ns.getRunningCount(me);
    System.err.println("my running count: " + rc);
    Optional<ServiceInstance<NodeType>> ods = ns.getOfType(NodeType.ODS, me.getAddress(), rc);
    if (!ods.isPresent())
        throw new RuntimeException("unable to find matching ODS at: " + me.getAddress() + ", index: " + rc);
    return ods.get();
}

From source file:org.opendaylight.netvirt.vpnmanager.api.VpnExtraRouteHelper.java

public static List<Routes> getAllExtraRoutes(DataBroker broker, String vpnName, String vrfId) {
    Optional<ExtraRoutes> extraRoutes = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
            getVpnToExtrarouteIdentifier(vpnName, vrfId));
    List<Routes> extraRoutesList = new ArrayList<>();
    if (extraRoutes.isPresent()) {
        extraRoutesList = extraRoutes.get().getRoutes();
    }//from w w w .  j a v a 2 s  . com
    return extraRoutesList;
}

From source file:brooklyn.util.flags.MethodCoercions.java

/**
 * Tries to find a multiple-parameter method with each parameter compatible with (can be coerced to) the
 * corresponding argument, and invokes it.
 *
 * @param instance the object to invoke the method on
 * @param methodName the name of the method to invoke
 * @param argument a list of the arguments to the method's parameters.
 * @return the result of the method call, or {@link brooklyn.util.guava.Maybe#absent()} if method could not be matched.
 */// w  w  w.j av  a2s. c  o  m
public static Maybe<?> tryFindAndInvokeMultiParameterMethod(final Object instance, final String methodName,
        final List<?> arguments) {
    Class<?> clazz = instance.getClass();
    Iterable<Method> methods = Arrays.asList(clazz.getMethods());
    Optional<Method> matchingMethod = Iterables.tryFind(methods,
            matchMultiParameterMethod(methodName, arguments));
    if (matchingMethod.isPresent()) {
        Method method = matchingMethod.get();
        try {
            int numOptionParams = ((List) arguments).size();
            Object[] coercedArguments = new Object[numOptionParams];
            for (int paramCount = 0; paramCount < numOptionParams; paramCount++) {
                Object argument = arguments.get(paramCount);
                Type paramType = method.getGenericParameterTypes()[paramCount];
                coercedArguments[paramCount] = TypeCoercions.coerce(argument, TypeToken.of(paramType));
            }
            return Maybe.of(method.invoke(instance, coercedArguments));
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw Exceptions.propagate(e);
        }
    } else {
        return Maybe.absent();
    }
}

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

/**
 * Tries to find a multiple-parameter method with each parameter compatible with (can be coerced to) the
 * corresponding argument, and invokes it.
 *
 * @param instance the object to invoke the method on
 * @param methodName the name of the method to invoke
 * @param argument a list of the arguments to the method's parameters.
 * @return the result of the method call, or {@link org.apache.brooklyn.util.guava.Maybe#absent()} if method could not be matched.
 *///from ww  w . j  av  a  2s.c o m
public static Maybe<?> tryFindAndInvokeMultiParameterMethod(final Object instance, final String methodName,
        final List<?> arguments) {
    Class<?> clazz = instance.getClass();
    Iterable<Method> methods = Arrays.asList(clazz.getMethods());
    Optional<Method> matchingMethod = Iterables.tryFind(methods,
            matchMultiParameterMethod(methodName, arguments));
    if (matchingMethod.isPresent()) {
        Method method = matchingMethod.get();
        try {
            int numOptionParams = ((List<?>) arguments).size();
            Object[] coercedArguments = new Object[numOptionParams];
            for (int paramCount = 0; paramCount < numOptionParams; paramCount++) {
                Object argument = arguments.get(paramCount);
                Type paramType = method.getGenericParameterTypes()[paramCount];
                coercedArguments[paramCount] = TypeCoercions.coerce(argument, TypeToken.of(paramType));
            }
            return Maybe.of(method.invoke(instance, coercedArguments));
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw Exceptions.propagate(e);
        }
    } else {
        return Maybe.absent();
    }
}

From source file:org.sonar.server.computation.task.projectanalysis.qualitymodel.NewQualityModelMeasuresVisitor.java

private static long getLongValue(Optional<Measure> measure, Period period) {
    if (!measure.isPresent()) {
        return 0L;
    }/*from w w  w . j av a  2  s  . c o m*/
    return getLongValue(measure.get(), period);
}

From source file:org.onos.yangtools.yang.model.repo.api.SourceIdentifier.java

/**
 * Returns filename for this YANG module as specified in RFC 6020.
 *
 * Returns filename in format//from   ww  w .  j  a  v  a 2s  .c o  m
 * <code>moduleName ['@' revision] '.yang'</code>
 *
 * Where Where revision-date is in format YYYY-mm-dd.
 *
 * <p>
 * See
 * http://tools.ietf.org/html/rfc6020#section-5.2
 *
 * @return Filename for this source identifier.
 */
public static final String toYangFileName(final String moduleName, final Optional<String> revision) {
    StringBuilder filename = new StringBuilder(moduleName);
    if (revision.isPresent()) {
        filename.append('@');
        filename.append(revision.get());
    }
    filename.append(".yang");
    return filename.toString();
}