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:com.example.app.repository.ui.RepositoryItemValueEditor.java

@Nullable
@Override//from  w ww .  ja  v  a2 s  .c o  m
public RI getUIValue(Level logErrorLevel) {
    RI result = super.getUIValue(logErrorLevel);
    return Optional.ofNullable(result)
            .orElseThrow(() -> new IllegalStateException("Result RepositoryItem was null."));
}

From source file:io.syndesis.core.json.StringTrimmingJsonDeserializerTest.java

private JsonParser parserWithString(final String value) throws JsonParseException, IOException {
    final JsonParser parser = new ObjectMapper().getFactory()
            .createParser(Optional.ofNullable(value).map(v -> "[\"" + v + "\"]").orElse("[null]"));
    parser.nextToken();// array
    parser.nextToken();// string

    return parser;
}

From source file:com.ibm.watson.catalyst.objectio.writers.CSVOutputWriter.java

/**
 * @param aOutFile the file to write to/* w w  w  . j  a  v a 2 s  .  c o  m*/
 * @param aEncoding the encoding to use
 * @param aHeader the header to include
 */
public CSVOutputWriter(final File aOutFile, final String aEncoding, final List<String> aHeader) {
    _outFile = aOutFile;
    _encoding = aEncoding;
    _header = Optional.ofNullable(aHeader);
}

From source file:io.appium.java_client.internal.ElementMap.java

/**
 * Gets element class by {@code platform} and mobile {@code automation} type.
 *
 * @param platform is the mobile platform. See {@link MobilePlatform}.
 * @param automation is the mobile automation type. See {@link AutomationName}
 * @return subclass of {@link RemoteWebElement} that convenient to current session details.
 *//*from  ww w  .  ja  va2 s . com*/
public static Class<? extends RemoteWebElement> getElementClass(String platform, String automation) {
    if (isBlank(platform) && isBlank(automation)) {
        return RemoteWebElement.class;
    }
    ElementMap element = Optional
            .ofNullable(mobileElementMap.get(String.valueOf(platform).toLowerCase().trim()))
            .orElseGet(() -> mobileElementMap.get(String.valueOf(automation).toLowerCase().trim()));
    if (element == null) {
        return RemoteWebElement.class;
    }
    return element.getElementClass();
}

From source file:io.knotx.repository.impl.FilesystemRepositoryConnectorProxyImpl.java

@Override
public void process(ClientRequest request, Handler<AsyncResult<ClientResponse>> result) {
    final String localFilePath = catalogue + StringUtils.stripStart(request.getPath(), "/");
    final Optional<String> contentType = Optional.ofNullable(MimeMapping.getMimeTypeForFilename(localFilePath));

    LOGGER.trace("Fetching file `{}` from local repository.", localFilePath);

    ObservableFuture<Buffer> fileObservable = RxHelper.observableFuture();
    fileObservable/*from w ww  .  ja v  a  2  s. co m*/
            .map(buffer -> new ClientResponse().setStatusCode(HttpResponseStatus.OK.code())
                    .setHeaders(headers(contentType)).setBody(buffer))
            .defaultIfEmpty(new ClientResponse().setStatusCode(HttpResponseStatus.NOT_FOUND.code()))
            .subscribe(response -> result.handle(Future.succeededFuture(response)), error -> {
                LOGGER.error(ERROR_MESSAGE, error);
                result.handle(Future.succeededFuture(processError(error)));
            });

    fileSystem.readFile(localFilePath, fileObservable.toHandler());
}

From source file:io.gravitee.repository.redis.management.RedisPageRepository.java

@Override
public Optional<Page> findById(String pageId) throws TechnicalException {
    RedisPage redisPage = pageRedisRepository.find(pageId);
    return Optional.ofNullable(convert(redisPage));
}

From source file:io.knotx.knot.templating.impl.HandlebarsKnotProxyImpl.java

@Override
protected Single<KnotContext> processRequest(KnotContext knotContext) {
    return Single.create(observer -> {
        try {//from www .j a va 2  s.c om
            knotContext.setTransition(DEFAULT_TRANSITION);
            Optional.ofNullable(knotContext.getFragments()).ifPresent(fragments -> fragments.stream()
                    .filter(fragment -> fragment.knots().contains(SUPPORTED_FRAGMENT_KNOT))
                    .forEach(fragment -> fragment.content(startComment()
                            + new HandlebarsFragment(fragment).compileWith(handlebars) + endComment())));
            observer.onSuccess(knotContext);
        } catch (Exception e) {
            observer.onError(e);
        }
    });
}

From source file:io.gravitee.repository.redis.management.RedisSubscriptionRepository.java

@Override
public Optional<Subscription> findById(String subscription) throws TechnicalException {
    RedisSubscription redisSubscription = subscriptionRedisRepository.find(subscription);
    return Optional.ofNullable(convert(redisSubscription));
}

From source file:ddf.catalog.transformer.OverlayMetacardTransformerTest.java

@Before
public void setUp() {
    final BiFunction<Metacard, Map<String, Serializable>, Optional<BufferedImage>> supplier = (metacard,
            arguments) -> {//from w w  w.j av  a  2  s .c o  m
        try (final InputStream inputStream = getClass().getClassLoader().getResourceAsStream("flower.jpg")) {
            return Optional.ofNullable(ImageIO.read(inputStream));
        } catch (IOException e) {
            return Optional.empty();
        }
    };

    transformer = new OverlayMetacardTransformer(supplier);
}

From source file:org.ow2.proactive.connector.iaas.cloud.provider.vmware.VMWareProviderVirtualMachineUtil.java

public Optional<VirtualMachine> searchVirtualMachineByName(String name, Folder rootFolder) {
    try {//from   www .j  a  va 2s.  c  om
        return Optional.ofNullable((VirtualMachine) new InventoryNavigator(rootFolder)
                .searchManagedEntity(EntityType.VM.getValue(), name));
    } catch (RemoteException e) {
        throw new RuntimeException("ERROR when retrieving VMWare virtual machine with name: " + name, e);
    }
}