List of usage examples for java.util Optional ofNullable
@SuppressWarnings("unchecked") public static <T> Optional<T> ofNullable(T value)
From source file:Main.java
/** * Convert a sequence of elements to the list. * * @param <I> Type of objects for input collection * @param <O> Type of objects for output collection * @param iterable The sequence of elements to convert. If the input * sequence is empty, then the resulting collection will be empty * @param transformer The conversion function. If the transfer function is * not set, the resulting collection will be empty * @return Reformed collection of input elements *///from ww w . ja v a 2 s . com public static <I, O> List<O> transformWithoutNull(final Iterable<I> iterable, final Function<I, O> transformer) { return StreamSupport .stream(Optional.ofNullable(iterable).orElse(Collections.<I>emptySet()).spliterator(), false) .map(Optional.ofNullable(transformer).orElse(i -> null)).filter(Objects::nonNull) .collect(Collectors.toList()); }
From source file:ru.mystamps.web.support.spring.security.SecurityContextUtils.java
/** * @author Sergey Chechenev// w w w . j a va 2s .c o m * @author Slava Semushin */ public static Integer getUserId() { return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication()) .map(Authentication::getPrincipal).filter(CustomUserDetails.class::isInstance) .map(CustomUserDetails.class::cast).map(CustomUserDetails::getUserId).orElse(null); }
From source file:ch.ifocusit.livingdoc.plugin.utils.AsciidocUtil.java
public static Optional<String> getTitle(Asciidoctor asciidoctor, String pageContent) { return Optional.ofNullable(asciidoctor.readDocumentHeader(pageContent).getDocumentTitle()) .map(title -> StringEscapeUtils.unescapeHtml4(title.getMain())); }
From source file:pitayaa.nail.msg.core.account.service.AccountLicenseServiceImpl.java
@Override public Optional<AccountLicense> findOne(UUID id) { return Optional.ofNullable(accountLicenseRepo.findOne(id)); }
From source file:cn.edu.zjnu.acm.judge.mapper.BestSubmissionsBuilder.java
public static String bestSubmissions(@Param("problemId") long problemId, @Param("pageable") Pageable pageable) { Set<String> dejaVu = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); Sort sort = Optional.ofNullable(pageable.getSort()).map(s -> s.and(DEFAULT_SORT)).orElse(DEFAULT_SORT); Sort.Order[] orders = StreamSupport.stream(sort.spliterator(), false) .filter(order -> ALLOW_COLUMNS.contains(order.getProperty()) && dejaVu.add(order.getProperty())) .toArray(Sort.Order[]::new); final int length = orders.length; log.debug("{}", Arrays.asList(orders)); StringBuilder sb = new StringBuilder( "select " + SubmissionMapper.LIST_COLUMNS + " from solution s where problem_id=").append(problemId) .append(" and score=100 "); for (int i = length - 1; i >= 0; --i) { sb.append("and(user_id"); for (int j = 0; j <= i; ++j) { sb.append(',').append(orders[j].getProperty()); }//from w w w . ja v a 2s . com sb.append(")in(select user_id"); for (int j = 0; j < i; ++j) { sb.append(',').append(orders[j].getProperty()); } sb.append(',').append(orders[i].isAscending() ? "min" : "max").append("(") .append(orders[i].getProperty()).append(")").append(orders[i].getProperty()) .append(" from solution where problem_id=").append(problemId).append(" and score=100 "); } for (int i = 0; i < length; ++i) { sb.append("group by user_id)"); } if (length > 0) { sb.append(" order by "); for (int i = 0; i < length; ++i) { if (i > 0) { sb.append(","); } sb.append(orders[i].getProperty()); if (!orders[i].isAscending()) { sb.append(" desc"); } } } return sb.append(" limit ").append(pageable.getOffset()).append(",").append(pageable.getPageSize()) .toString(); }
From source file:pitayaa.nail.msg.core.license.service.LicenseServiceImpl.java
@Override public Optional<License> findOne(UUID uid) { return Optional.ofNullable(licenseRepo.findOne(uid)); }
From source file:Main.java
/** * @param <T> the key type/*from w w w .ja v a 2s .c om*/ * @param s the set * @return an <b>UNMODIFIABLE</b> Set<T> */ public static <T> Set<T> unmodifiableSet(Set<? extends T> s) { return Optional.ofNullable(s).map(Collections::unmodifiableSet).orElseGet(Collections::emptySet); }
From source file:org.zalando.riptide.Actions.java
public static ThrowingFunction<ClientHttpResponse, URI, IOException> contentLocation() { return response -> Optional.ofNullable(response.getHeaders().getFirst(CONTENT_LOCATION)).map(URI::create) .orElse(null);/* w w w .j a va 2 s . c o m*/ }
From source file:eu.esdihumboldt.hale.io.haleconnect.internal.UserServiceHelper.java
/** * Build a {@link Credentials} object. Any null values passed in will be * converted to an empty string.//w ww. j ava2s .co m * * @param username the user name * @param password the password * @return a Credentials object with the given credentials */ public static Credentials buildCredentials(String username, String password) { Credentials credentials = new Credentials(); credentials.setUsername(Optional.ofNullable(username).orElse("")); credentials.setPassword(Optional.ofNullable(password).orElse("")); return credentials; }
From source file:Main.java
/** * @param <K> the key type// www . j a v a 2 s. com * @param <V> the value type * @param m the map * * @return an <b>UNMODIFIABLE</b> Map<K, V> */ public static <K, V> Map<? extends K, ? extends V> unmodifiableMap(Map<? extends K, ? extends V> m) { return Optional.ofNullable(m).map(Collections::unmodifiableMap).orElseGet(Collections::emptyMap); }