Example usage for java.util Optional ofNullable

List of usage examples for java.util Optional ofNullable

Introduction

In this page you can find the example usage for java.util Optional ofNullable.

Prototype

@SuppressWarnings("unchecked")
public static <T> Optional<T> ofNullable(T value) 

Source Link

Document

Returns an Optional describing the given value, if non- null , otherwise returns an empty Optional .

Usage

From source file:alfio.controller.api.admin.CheckInApiController.java

@RequestMapping(value = "/check-in/{eventId}/ticket/{ticketIdentifier}", method = GET)
public TicketAndCheckInResult findTicketWithUUID(@PathVariable("eventId") int eventId,
        @PathVariable("ticketIdentifier") String ticketIdentifier, @RequestParam("qrCode") String qrCode) {
    return checkInManager.evaluateTicketStatus(eventId, ticketIdentifier, Optional.ofNullable(qrCode));
}

From source file:com.netflix.genie.common.internal.dto.v4.Criterion.java

/**
 * Get the id of the resource desired if it exists.
 *
 * @return {@link Optional} wrapping the id
 *//*ww  w .  j ava  2 s. co m*/
public Optional<String> getId() {
    return Optional.ofNullable(this.id);
}

From source file:com.netflix.genie.agent.execution.ExecutionContextImpl.java

/**
 * {@inheritDoc}
 */
@Override
public Optional<File> getJobDirectory() {
    return Optional.ofNullable(jobDirectoryRef.get());
}

From source file:com.example.app.profile.ui.user.UserPositionValueEditor.java

@Nullable
@Override/*ww w.  j  a  va2 s .c o  m*/
public UserPosition getUIValue(Level logErrorLevel) {
    UserPosition result = Optional.ofNullable(super.getUIValue(logErrorLevel))
            .orElseThrow(() -> new IllegalStateException("UserPosition was null.  This should not happen."));
    result.setUser(getUser());
    return result;
}

From source file:com.netflix.spinnaker.orca.pipeline.util.ArtifactResolver.java

public @Nonnull List<Artifact> getArtifacts(@Nonnull Stage stage) {
    if (stage.getContext() instanceof StageContext) {
        return (List<Artifact>) Optional
                .ofNullable((List) ((StageContext) stage.getContext()).getAll("artifacts"))
                .map(list -> list.stream().filter(Objects::nonNull).flatMap(it -> ((List) it).stream())
                        .map(a -> a instanceof Map ? objectMapper.convertValue(a, Artifact.class) : a)
                        .collect(Collectors.toList()))
                .orElse(emptyList());//from   w  ww  .  j  a  v  a 2 s . c  o  m
    } else {
        log.warn("Unable to read artifacts from unknown context type: {} ({})", stage.getContext().getClass(),
                stage.getExecution().getId());
        return emptyList();
    }
}

From source file:org.opendatakit.briefcase.reused.http.CommonsHttp.java

private static String readBody(HttpResponse res) {
    return Optional.ofNullable(res.getEntity()).map(e -> uncheckedHandleEntity(new BasicResponseHandler(), e))
            .orElse("");
}

From source file:com.jeanchampemont.notedown.user.UserService.java

/**
 * Find a user for this email if it exists
 * @param email/*from   www. ja va 2 s .c o  m*/
 * @return
 */
@Transactional(readOnly = true)
@Cacheable("user")
public Optional<User> getUserByEmail(String email) {
    return Optional.ofNullable(repo.findByEmailIgnoreCase(email));
}

From source file:com.javafxpert.wikibrowser.WikiIdLocatorController.java

@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> locatorEndpoint(
        @RequestParam(value = "name", defaultValue = "") String articleName,
        @RequestParam(value = "lang") String lang) {

    String language = wikiBrowserProperties.computeLang(lang);

    ItemInfo itemInfo = null;/*  w ww  .  j  a  v  a2s .  c  o  m*/
    if (!articleName.equals("")) {
        itemInfo = name2Id(articleName, language);
    }

    return Optional.ofNullable(itemInfo).map(cr -> new ResponseEntity<>((Object) cr, HttpStatus.OK))
            .orElse(new ResponseEntity<>("Wikipedia query unsuccessful", HttpStatus.INTERNAL_SERVER_ERROR));
}

From source file:curly.artifact.ArtifactServiceImpl.java

@Loggable
@Override/* w w  w.  j  av a2s .c  o  m*/
@Retryable
public Observable<Optional<Artifact>> save(Artifact artifact, OctoUser octoUser) {
    log.trace("Saving entity {} ...", artifact);
    return new ObservableResult<Optional<Artifact>>() {
        @Override
        public Optional<Artifact> invoke() {
            onSave(artifact, octoUser);
            return Optional.ofNullable(repository.save(artifact));
        }
    };
}

From source file:ac.simons.tweetarchive.tweets.TweetRepositoryImpl.java

@Override
@Transactional(readOnly = true)// w  w  w .j  ava  2s .co  m
public List<TweetEntity> searchByKeyword(final String keywords, final LocalDate from, final LocalDate to) {
    // Must be retrieved inside a transaction to take part of
    final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);

    // Prepare a search query builder
    final QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
            .forEntity(TweetEntity.class).get();

    // This is a boolean junction... I'll add at least a keyword query
    final BooleanJunction<BooleanJunction> outer = queryBuilder.bool();
    outer.must(queryBuilder.keyword().onFields("content").matching(keywords).createQuery());

    // And then 2 range queries if from and to are not null
    Optional.ofNullable(from).map(f -> f.atStartOfDay(UTC)) // Must be a zoned date time to fit the field
            .map(f -> queryBuilder.range().onField("created_at").above(f).createQuery())
            .ifPresent(q -> outer.must(q));
    Optional.ofNullable(to).map(f -> f.plusDays(1).atStartOfDay(UTC)) // Same here, but a day later
            .map(f -> queryBuilder.range().onField("created_at").below(f).excludeLimit().createQuery()) // which i exclude
            .ifPresent(q -> outer.must(q));
    return fullTextEntityManager.createFullTextQuery(outer.createQuery(), TweetEntity.class).getResultList();
}