List of usage examples for java.util Optional map
public <U> Optional<U> map(Function<? super T, ? extends U> mapper)
From source file:org.silverpeas.core.notification.user.builder.AbstractTemplateUserNotificationBuilder.java
/** * @return a {@link Pair} instance which indicates first the StringTemplate repository as a * boolean, true if it is the components one, false if it is the core one. It indicates secondly * the template path into the root as a String. *//*from w w w . ja v a 2 s.com*/ private Pair<Boolean, String> getRootTemplatePath() { if (rootTemplatePath == null) { final Mutable<Boolean> componentRoot = Mutable.of(false); final Mutable<String> templatePath = Mutable.of(getTemplatePath()); Optional<SilverpeasComponentInstance> instance = SilverpeasComponentInstance .getById(getComponentInstanceId()); if (this instanceof FallbackToCoreTemplatePathBehavior) { instance.map(i -> i.getName() + "/" + templatePath.get()).filter( p -> SilverpeasStringTemplateUtil.isComponentTemplateExist(p, getTemplateFileName())) .ifPresent(p -> { componentRoot.set(true); templatePath.set(p); }); } else { componentRoot.set(instance.isPresent() || OrganizationControllerProvider.getOrganisationController() .isToolAvailable(getComponentInstanceId())); } rootTemplatePath = Pair.of(componentRoot.get(), templatePath.get()); } return rootTemplatePath; }
From source file:org.sonar.server.issue.IssueQueryFactory.java
@CheckForNull private String convertOrganizationKeyToUuid(DbSession dbSession, @Nullable String organizationKey) { if (organizationKey == null) { return null; }//from ww w. j a v a2 s. c o m Optional<OrganizationDto> organization = dbClient.organizationDao().selectByKey(dbSession, organizationKey); return organization.map(OrganizationDto::getUuid).orElse(UNKNOWN); }
From source file:org.sonar.server.issue.IssueQueryFactory.java
@CheckForNull private Date findCreatedAfterFromComponentUuid(DbSession dbSession, String uuid) { ComponentDto component = checkFoundWithOptional(dbClient.componentDao().selectByUuid(dbSession, uuid), "Component with id '%s' not found", uuid); Optional<SnapshotDto> snapshot = dbClient.snapshotDao().selectLastAnalysisByComponentUuid(dbSession, component.uuid());/*from w ww .j a v a 2s .c om*/ return snapshot.map(s -> longToDate(s.getPeriodDate())).orElse(null); }
From source file:org.sonar.server.property.ws.IndexAction.java
private boolean hasAdminPermission(Optional<ComponentDto> component) { return component.map(c -> userSession.hasComponentPermission(ADMIN, c)) .orElse(userSession.isSystemAdministrator()); }
From source file:org.sonar.server.user.AbstractUserSession.java
@Override public final boolean hasComponentUuidPermission(String permission, String componentUuid) { if (isRoot()) { return true; }//from w w w. j a v a2 s .c om Optional<String> projectUuid = componentUuidToProjectUuid(componentUuid); return projectUuid.map(s -> hasProjectUuidPermission(permission, s)).orElse(false); }
From source file:org.springframework.test.context.junit.jupiter.AbstractExpressionEvaluatingCondition.java
/** * Evaluate the expression configured via the supplied annotation type on * the {@link AnnotatedElement} for the supplied {@link ExtensionContext}. * @param annotationType the type of annotation to process * @param expressionExtractor a function that extracts the expression from * the annotation// w w w .j a va 2 s . com * @param reasonExtractor a function that extracts the reason from the * annotation * @param loadContextExtractor a function that extracts the {@code loadContext} * flag from the annotation * @param enabledOnTrue indicates whether the returned {@code ConditionEvaluationResult} * should be {@link ConditionEvaluationResult#enabled enabled} if the expression * evaluates to {@code true} * @param context the {@code ExtensionContext} * @return {@link ConditionEvaluationResult#enabled enabled} if the container * or test should be enabled; otherwise {@link ConditionEvaluationResult#disabled disabled} */ protected <A extends Annotation> ConditionEvaluationResult evaluateAnnotation(Class<A> annotationType, Function<A, String> expressionExtractor, Function<A, String> reasonExtractor, Function<A, Boolean> loadContextExtractor, boolean enabledOnTrue, ExtensionContext context) { Assert.state(context.getElement().isPresent(), "No AnnotatedElement"); AnnotatedElement element = context.getElement().get(); Optional<A> annotation = findMergedAnnotation(element, annotationType); if (!annotation.isPresent()) { String reason = String.format("%s is enabled since @%s is not present", element, annotationType.getSimpleName()); if (logger.isDebugEnabled()) { logger.debug(reason); } return ConditionEvaluationResult.enabled(reason); } String expression = annotation.map(expressionExtractor).map(String::trim).filter(StringUtils::hasLength) .orElseThrow(() -> new IllegalStateException( String.format("The expression in @%s on [%s] must not be blank", annotationType.getSimpleName(), element))); boolean loadContext = loadContextExtractor.apply(annotation.get()); boolean evaluatedToTrue = evaluateExpression(expression, loadContext, annotationType, context); if (evaluatedToTrue) { String adjective = (enabledOnTrue ? "enabled" : "disabled"); String reason = annotation.map(reasonExtractor).filter(StringUtils::hasText) .orElseGet(() -> String.format("%s is %s because @%s(\"%s\") evaluated to true", element, adjective, annotationType.getSimpleName(), expression)); if (logger.isInfoEnabled()) { logger.info(reason); } return (enabledOnTrue ? ConditionEvaluationResult.enabled(reason) : ConditionEvaluationResult.disabled(reason)); } else { String adjective = (enabledOnTrue ? "disabled" : "enabled"); String reason = String.format("%s is %s because @%s(\"%s\") did not evaluate to true", element, adjective, annotationType.getSimpleName(), expression); if (logger.isDebugEnabled()) { logger.debug(reason); } return (enabledOnTrue ? ConditionEvaluationResult.disabled(reason) : ConditionEvaluationResult.enabled(reason)); } }
From source file:org.springframework.test.context.junit.jupiter.DisabledIfCondition.java
private ConditionEvaluationResult evaluateDisabledIf(ExtensionContext extensionContext) { AnnotatedElement element = extensionContext.getElement().get(); Optional<DisabledIf> disabledIf = findMergedAnnotation(element, DisabledIf.class); Assert.state(disabledIf.isPresent(), () -> "@DisabledIf must be present on " + element); String expression = disabledIf.get().expression().trim(); if (isDisabled(expression, extensionContext)) { String reason = disabledIf.map(DisabledIf::reason).filter(StringUtils::hasText).orElseGet(() -> String .format("%s is disabled because @DisabledIf(\"%s\") evaluated to true", element, expression)); logger.info(reason);// w w w . j a va2 s . co m return ConditionEvaluationResult.disabled(reason); } else { String reason = String.format("%s is enabled because @DisabledIf(\"%s\") did not evaluate to true", element, expression); logger.debug(reason); return ConditionEvaluationResult.enabled(reason); } }
From source file:org.trellisldp.http.impl.HttpUtils.java
/** * Given a list of acceptable media types, get an RDF syntax. * * @param ioService the I/O service/* w ww. j a v a 2 s.c o m*/ * @param acceptableTypes the types from HTTP headers * @param mimeType an additional "default" mimeType to match * @return an RDFSyntax or null if there was an error */ public static Optional<RDFSyntax> getSyntax(final IOService ioService, final List<MediaType> acceptableTypes, final Optional<String> mimeType) { if (acceptableTypes.isEmpty()) { return mimeType.isPresent() ? empty() : of(TURTLE); } final Optional<MediaType> mt = mimeType.map(MediaType::valueOf); for (final MediaType type : acceptableTypes) { if (mt.filter(type::isCompatible).isPresent()) { return empty(); } final Optional<RDFSyntax> syntax = ioService.supportedReadSyntaxes().stream() .filter(s -> MediaType.valueOf(s.mediaType()).isCompatible(type)).findFirst(); if (syntax.isPresent()) { return syntax; } } LOGGER.debug("Valid syntax not found among {} or {}", acceptableTypes, mimeType); throw new NotAcceptableException(); }
From source file:org.trellisldp.http.impl.RdfUtils.java
/** * Given a list of acceptable media types, get an RDF syntax. * @param acceptableTypes the types from HTTP headers * @param mimeType an additional "default" mimeType to match * @return an RDFSyntax/*from w w w . j a v a2s . co m*/ */ public static Optional<RDFSyntax> getSyntax(final List<MediaType> acceptableTypes, final Optional<String> mimeType) { if (acceptableTypes.isEmpty()) { // TODO -- JDK9 refactor with Optional::or if (mimeType.isPresent()) { return empty(); } return of(TURTLE); } final Optional<MediaType> mt = mimeType.map(MediaType::valueOf); for (final MediaType type : acceptableTypes) { if (mt.filter(type::isCompatible).isPresent()) { return empty(); } final Optional<RDFSyntax> syntax = MEDIA_TYPES.stream().filter(type::isCompatible).findFirst() .map(MediaType::toString).flatMap(RDFSyntax::byMediaType); if (syntax.isPresent()) { return syntax; } } LOGGER.debug("Valid syntax not found among {} or {}", acceptableTypes, mimeType); throw new NotAcceptableException(); }
From source file:org.trellisldp.http.PartitionedLdpResource.java
/** * Perform a POST operation on a LDP Resource * @param req the request//ww w. jav a 2 s . c o m * @param body the body * @return the response */ @POST @Timed public Response createResource(@BeanParam final LdpRequest req, final File body) { final String baseUrl = partitions.get(req.getPartition()); final String path = req.getPartition() + req.getPath(); final String identifier = "/" + ofNullable(req.getSlug()).orElseGet(resourceService.getIdentifierSupplier()); final PostHandler postHandler = new PostHandler(req, identifier, body, resourceService, ioService, binaryService, baseUrl); // First check if this is a container final Optional<Resource> parent = resourceService.get(rdf.createIRI(TRELLIS_PREFIX + path)); if (parent.isPresent()) { final Optional<IRI> ixModel = parent.map(Resource::getInteractionModel); if (ixModel.filter(type -> ldpResourceTypes(type).anyMatch(LDP.Container::equals)).isPresent()) { return resourceService.get(rdf.createIRI(TRELLIS_PREFIX + path + identifier), MAX) .map(x -> status(CONFLICT)).orElseGet(postHandler::createResource).build(); } else if (parent.filter(RdfUtils::isDeleted).isPresent()) { return status(GONE).build(); } return status(METHOD_NOT_ALLOWED).build(); } return status(NOT_FOUND).build(); }