@Test public void testOptional() { Optional<String> fullName = Optional.ofNullable(null); Assert.assertEquals("Optional.empty", fullName.toString()); Assert.assertEquals("[none]", fullName.orElseGet(() -> "[none]")); Assert.assertEquals("[none2]", fullName.orElse("[none2]")); }
public void addTeam(ActionEvent event) { TextInputDialog dialog = new TextInputDialog(); dialog.setTitle("Text Input Dialog"); dialog.setHeaderText(null); dialog.setContentText("Please enter Team name:"); // Traditional way to get the response value. Optional<String> result = dialog.showAndWait(); if (result.isPresent()) { String actualresult = result.toString().replaceAll("Optional", ""); actualresult = actualresult.replaceAll("[^a-zA-Z0-9]", ""); list1.add(actualresult); } }
@Override public String serializeResult( final Optional<Object> o, final ResultCode code, final Optional<RequestMessage> requestMessage) { try { final Map<String, Object> result = new HashMap<>(); result.put(TOKEN_CODE, code.getValue()); result.put(TOKEN_RESULT, o.isPresent() ? o.get() : null); result.put( TOKEN_REQUEST, requestMessage.isPresent() ? requestMessage.get().getRequestId() : null); return mapper.writeValueAsString(result); } catch (Exception ex) { logger.warn( "Result [{}] could not be serialized by {}.", o.toString(), JsonMessageSerializerV1d0.class.getName()); throw new RuntimeException("Error during serialization.", ex); } }