Example usage for java.util Optional of

List of usage examples for java.util Optional of

Introduction

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

Prototype

public static <T> Optional<T> of(T value) 

Source Link

Document

Returns an Optional describing the given non- null value.

Usage

From source file:org.zalando.riptide.SeriesSelector.java

@Override
public Optional<HttpStatus.Series> attributeOf(final ClientHttpResponse response) throws IOException {
    return Optional.of(response.getStatusCode().series());
}

From source file:com.ikanow.aleph2.core.shared.utils.TimeSliceDirUtils.java

/** Given a pair of (optional) human readable strings (which are assumed to refer to the past)
 *  returns a pair of optional dates/*  w w w  .ja  va 2s  .  c o  m*/
 * @param input_config
 * @return
 */
public static Tuple2<Optional<Date>, Optional<Date>> getQueryTimeRange(
        final AnalyticThreadJobInputConfigBean input_config, final Date now) {
    Function<Optional<String>, Optional<Date>> parseDate = maybe_date -> maybe_date
            .map(datestr -> TimeUtils.getSchedule(datestr, Optional.of(now))).filter(res -> res.isSuccess())
            .map(res -> res.success())
            // OK so this wants to be backwards in time always...
            .map(date -> {
                if (date.getTime() > now.getTime()) {
                    final long diff = date.getTime() - now.getTime();
                    return Date.from(now.toInstant().minusMillis(diff));
                } else
                    return date;
            });

    final Optional<Date> tmin = parseDate.apply(Optional.ofNullable(input_config.time_min()));
    final Optional<Date> tmax = parseDate.apply(Optional.ofNullable(input_config.time_max()));

    return Tuples._2T(tmin, tmax);
}

From source file:io.vertx.json.schema.internal.EmailFormatValidator.java

@Override
public Optional<String> validate(final String subject) {
    if (EmailValidator.getInstance(false, true).isValid(subject)) {
        return Optional.empty();
    }/*from w w  w  .j a  v a 2  s .  c om*/
    return Optional.of(String.format("[%s] is not a valid email address", subject));
}

From source file:net.hamnaberg.json.ValueFactory.java

public static Optional<Value> createOptionalValue(Object value) {
    Value v = createValue(value);//  www.  j  a va2 s.c  o m
    if (v.isNull()) {
        return Optional.empty();
    }
    return Optional.of(v);
}

From source file:io.github.retz.scheduler.MesosHTTPFetcher.java

public static Optional<String> sandboxDownloadUri(String master, String slaveId, String frameworkId,
        String executorId, String path) {
    Optional<String> base = sandboxUri("download", master, slaveId, frameworkId, executorId);
    if (base.isPresent()) {
        try {//w  w  w . j  a  v  a2 s . c  om
            String encodedPath = java.net.URLEncoder.encode(path,
                    java.nio.charset.StandardCharsets.UTF_8.toString());
            return Optional.of(base.get() + encodedPath);
        } catch (UnsupportedEncodingException e) {
            return Optional.empty();
        }
    }
    return Optional.empty();
}

From source file:com.astamuse.asta4d.web.form.flow.base.StepAwaredValidationFormHelper.java

static final Object getValidationTargetByAnnotation(Object form, String step) {
    String cacheKey = step + "@" + form.getClass().getName();
    Optional<Field> oField = ValidationTargetFieldCache.get(cacheKey);

    if (oField == null) {
        List<Field> list = new ArrayList<>(ClassUtil.retrieveAllFieldsIncludeAllSuperClasses(form.getClass()));
        Iterator<Field> it = list.iterator();
        while (it.hasNext()) {
            Field f = it.next();// w w  w. j  ava 2 s.c o  m
            StepAwaredValidationTarget vtAnno = f.getAnnotation(StepAwaredValidationTarget.class);
            if (vtAnno == null) {
                continue;
            }
            String representingStep = vtAnno.value();
            if (step.equals(representingStep)) {
                oField = Optional.of(f);
                break;
            }
        }
        if (oField == null) {
            oField = Optional.empty();
        }
        if (Configuration.getConfiguration().isCacheEnable()) {
            Map<String, Optional<Field>> newCache = new HashMap<>(ValidationTargetFieldCache);
            newCache.put(cacheKey, oField);
            ValidationTargetFieldCache = newCache;
        }
    }

    if (oField.isPresent()) {
        try {
            return FieldUtils.readField(oField.get(), form, true);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    } else {
        return form;
    }
}

From source file:cloudfoundry.norouter.f5.Json.java

public static <T> Optional<T> fromJsonish(Class<T> type, String jsonish) {
    if (jsonish == null || jsonish.trim().length() == 0) {
        return Optional.empty();
    }/*from ww  w.j a v a2s  .  c om*/
    try {
        return Optional.of(fromJson(type, unescape(jsonish)));
    } catch (IOException e) {
        return Optional.empty();
    }
}

From source file:com.epam.ta.reportportal.database.entity.item.issue.ExternalSystemType.java

public static Optional<String> knownIssue(String summary) {
    if (summary.trim().startsWith(ISSUE_MARKER)) {
        return Optional.of(StringUtils.substringAfter(summary, ISSUE_MARKER));
    } else {//from  ww  w. j  a v  a  2 s. co m
        return Optional.empty();
    }
}

From source file:junit.org.rapidpm.microservice.optionals.cli.MainTest002.java

@Test
public void test001() throws Exception {
    final Optional<String[]> args = Optional.of(new String[] { "-i /opt/hoppel/poppel/xx.properties" });
    Main.deploy(args);//from  ww w  . j  a  v a  2  s  .c  o  m
    Assertions.assertTrue(DefaultCmdLineOptions.wasproperlyExecuted);
    Main.stop();
}

From source file:nu.yona.server.subscriptions.service.UserPhotoService.java

@Transactional
public UserPhotoDto addUserPhoto(UUID userId, UserPhotoDto userPhoto) {
    UserPhoto userPhotoEntity = userPhotoRepository.save(UserPhoto.createInstance(userPhoto.getPngBytes()));
    userService.updateUserPhoto(userId, Optional.of(userPhotoEntity.getId()));
    return UserPhotoDto.createInstance(userPhotoEntity);
}