List of usage examples for java.util Optional orElse
public T orElse(T other)
From source file:com.netflix.imfutility.cpl._2013.Cpl2013ContextBuilderStrategy.java
@Override protected String getDefaultCplLanguage() { // assume default language to be first language of first locale LocaleList localeList = cpl2013.getLocaleList(); // no locales defined if (localeList == null) { return null; }//from w ww .j av a2 s . c o m Optional<LocaleType> locale = localeList.getLocale().stream().findFirst(); if (!locale.isPresent()) { return null; } // no languages defined LanguageList languageList = locale.get().getLanguageList(); if (languageList == null) { return null; } Optional<String> language = languageList.getLanguage().stream().findFirst(); return language.orElse(null); }
From source file:com.netflix.imfutility.cpl._2016.Cpl2016ContextBuilderStrategy.java
@Override protected String getDefaultCplLanguage() { // assume default language to be first language of first locale LocaleList localeList = cpl2016.getLocaleList(); // no locales defined if (localeList == null) { return null; }/*ww w. j a v a2 s. c o m*/ Optional<LocaleType> locale = localeList.getLocale().stream().findFirst(); if (!locale.isPresent()) { return null; } // no languages defined LanguageList languageList = locale.get().getLanguageList(); if (languageList == null) { return null; } Optional<String> language = languageList.getLanguage().stream().findFirst(); return language.orElse(null); }
From source file:org.apache.metron.stellar.common.BaseStellarProcessor.java
private ParseException createException(String rule, VariableResolver resolver, Throwable t) { String message = "Unable to parse: " + rule + " due to: " + t.getMessage(); Set<String> variablesUsed = variablesUsed(rule); if (variablesUsed.isEmpty()) { return new ParseException(message, t); }// w w w . j ava2 s. com List<Map.Entry<String, Object>> messagesUsed = new ArrayList<>(variablesUsed.size()); for (String v : variablesUsed) { Optional<Object> resolved = Optional.ofNullable(resolver.resolve(v)); messagesUsed.add(new AbstractMap.SimpleEntry<>(v, resolved.orElse("missing"))); } return new ParseException(message + " with relevant variables " + Joiner.on(",").join(messagesUsed), t); }
From source file:com.khartec.waltz.data.changelog.ChangeLogDao.java
public List<ChangeLog> findByParentReference(EntityReference ref, Optional<Integer> limit) { checkNotNull(ref, "ref must not be null"); return dsl.select().from(CHANGE_LOG).where(CHANGE_LOG.PARENT_ID.eq(ref.id())) .and(CHANGE_LOG.PARENT_KIND.eq(ref.kind().name())).orderBy(CHANGE_LOG.CREATED_AT.desc()) .limit(limit.orElse(Integer.MAX_VALUE)).fetch(mapper); }
From source file:ch.ralscha.extdirectspring.provider.RemoteProviderOptional.java
@ExtDirectMethod(group = "optional") public String method17( @CookieValue(required = false, defaultValue = "theDefaultValue") Optional<String> stringCookie) { return stringCookie.orElse("anotherDefault"); }
From source file:org.jboss.tools.openshift.internal.core.server.debug.RouteTimeout.java
private IRoute getRoute(IResource resource, Connection connection, IProgressMonitor monitor) { SubMonitor routeMonitor = SubMonitor.convert(monitor); routeMonitor.beginTask("Determine route to set the haproxy timeout for...", 2); if (routeMonitor.isCanceled()) { return null; }/*w w w . ja va 2s . c om*/ List<IService> services = connection.getResources(ResourceKind.SERVICE, resource.getNamespace()); Collection<IService> matchingServices = ResourceUtils.getServicesFor(resource, services); routeMonitor.worked(1); if (routeMonitor.isCanceled()) { return null; } List<IRoute> routes = connection.getResources(ResourceKind.ROUTE, resource.getNamespace()); // TODO: support multiple matching routes, for now only get first Optional<IRoute> matchingRoute = matchingServices.stream() .flatMap(service -> ResourceUtils.getRoutesFor(service, routes).stream()).findFirst(); routeMonitor.worked(1); routeMonitor.done(); return matchingRoute.orElse(null); }
From source file:com.devicehive.service.NetworkService.java
@Transactional public NetworkVO createOrUpdateNetworkByUser(Optional<NetworkVO> networkNullable, UserVO user) { //case network is not defined if (networkNullable == null || networkNullable.orElse(null) == null) { return null; }//from ww w . ja v a 2 s .c o m NetworkVO network = networkNullable.orElse(null); Optional<NetworkVO> storedOpt = findNetworkByIdOrName(network); if (storedOpt.isPresent()) { NetworkVO stored = validateNetworkKey(storedOpt.get(), network); if (!userService.hasAccessToNetwork(user, stored)) { throw new ActionNotAllowedException(Messages.NO_ACCESS_TO_NETWORK); } return stored; } else { if (network.getId() != null) { throw new IllegalParametersException(Messages.INVALID_REQUEST_PARAMETERS); } boolean allowed = configurationService.getBoolean(ALLOW_NETWORK_AUTO_CREATE, false); if (user.isAdmin() || allowed) { NetworkWithUsersAndDevicesVO newNetwork = new NetworkWithUsersAndDevicesVO(network); networkDao.persist(newNetwork); network.setId(newNetwork.getId()); userService.assignNetwork(user.getId(), network.getId()); // Assign created network to user } else { throw new ActionNotAllowedException(Messages.NETWORK_CREATION_NOT_ALLOWED); } return network; } }
From source file:org.haiku.haikudepotserver.api1.PkgJobApiImpl.java
@Override public QueuePkgScreenshotExportArchiveJobResult queuePkgScreenshotExportArchiveJob( QueuePkgScreenshotExportArchiveJobRequest request) { Preconditions.checkArgument(null != request, "the request must be supplied"); final ObjectContext context = serverRuntime.newContext(); Optional<User> user = tryObtainAuthenticatedUser(context); if (!authorizationService.check(context, user.orElse(null), null, Permission.BULK_PKGSCREENSHOTEXPORTARCHIVE)) { LOGGER.warn("attempt to export pkg screenshots as an archive, but was not authorized"); throw new AuthorizationFailureException(); }/*from w w w.jav a 2 s .c o m*/ PkgScreenshotExportArchiveJobSpecification specification = new PkgScreenshotExportArchiveJobSpecification(); specification.setOwnerUserNickname(user.get().getNickname()); specification.setPkgName(request.pkgName); return new QueuePkgScreenshotExportArchiveJobResult( jobService.submit(specification, JobSnapshot.COALESCE_STATUSES_NONE)); }
From source file:ch.ralscha.extdirectspring.provider.RemoteProviderOptional.java
@ExtDirectMethod(group = "optional") public String method1(Optional<Integer> i, Optional<BigDecimal> bd, String s) { return String.format("method1() called-%d-%.3f-%s", i.orElse(-1), bd.orElse(new BigDecimal("3.141")), s); }
From source file:org.haiku.haikudepotserver.api1.PkgJobApiImpl.java
@Override public QueuePkgIconArchiveImportJobResult queuePkgIconArchiveImportJob( QueuePkgIconArchiveImportJobRequest request) throws ObjectNotFoundException { Preconditions.checkArgument(null != request, "the request must be supplied"); Preconditions.checkArgument(!Strings.isNullOrEmpty(request.inputDataGuid), "the input data must be identified by guid"); final ObjectContext context = serverRuntime.newContext(); Optional<User> user = tryObtainAuthenticatedUser(context); if (!authorizationService.check(context, user.orElse(null), null, Permission.BULK_PKGICONIMPORTARCHIVE)) { LOGGER.warn("attempt to import package icons, but was not authorized"); throw new AuthorizationFailureException(); }/* ww w. j a va 2s .c o m*/ // now check that the data is present. jobService.tryGetData(request.inputDataGuid).orElseThrow( () -> new ObjectNotFoundException(JobData.class.getSimpleName(), request.inputDataGuid)); // setup and go PkgIconImportArchiveJobSpecification spec = new PkgIconImportArchiveJobSpecification(); spec.setOwnerUserNickname(user.map(_User::getNickname).orElse(null)); spec.setInputDataGuid(request.inputDataGuid); return new QueuePkgIconArchiveImportJobResult(jobService.submit(spec, JobSnapshot.COALESCE_STATUSES_NONE)); }