List of usage examples for java.util Optional of
public static <T> Optional<T> of(T value)
From source file:enumj.EnumerableGenerator.java
public static Enumerable<EnumerableGenerator> generators() { final Random rnd = new Random(9691); return Enumerable.of(() -> Optional.of(new EnumerableGenerator(rnd.nextLong()))); }
From source file:de.mas.wiiu.jnus.utils.FSTUtils.java
public static Optional<FSTEntry> getFSTEntryByFullPath(FSTEntry root, String givenFullPath) { String fullPath = givenFullPath.replace(File.separator, "/"); if (!fullPath.startsWith("/")) { fullPath = "/" + fullPath; }//from w w w . j a v a 2 s . co m String dirPath = FilenameUtils.getFullPathNoEndSeparator(fullPath); Optional<FSTEntry> pathOpt = Optional.of(root); if (!dirPath.equals("/")) { pathOpt = getFileEntryDir(root, dirPath); } String path = fullPath; return pathOpt.flatMap(e -> e.getChildren().stream().filter(c -> c.getFullPath().equals(path)).findAny()); }
From source file:io.klerch.alexa.tellask.util.factory.AlexaSpeechletFactory.java
/** * Creates an AlexaSpeechlet from bytes of a speechlet request. It will extract the * locale from the request and uses it for creating a new instance of AlexaSpeechlet * @param serializedSpeechletRequest bytes of a speechlet request * @param speechletClass the class of your AlexaSpeechlet to instantiate * @param utteranceReader the reader AlexaSpeechlet should use when reading out utterances * @param <T> must extend AlexaSpeechlet * @return new instance of AlexaSpeechlet * @throws IOException thrown when something went wrong *///from w w w . j a v a 2 s . c o m public static <T extends AlexaSpeechlet> T createSpeechletFromRequest(final byte[] serializedSpeechletRequest, final Class<T> speechletClass, final UtteranceReader utteranceReader) throws IOException { final ObjectMapper mapper = new ObjectMapper(); final JsonNode parser = mapper.readTree(serializedSpeechletRequest); final String locale = Optional.of(parser.path("request")).filter(node -> !node.isMissingNode()) .map(node -> node.path("locale")).filter(node -> !node.isMissingNode()).map(JsonNode::textValue) .orElse(DEFAULT_LOCALE); try { return speechletClass.getConstructor(String.class, UtteranceReader.class).newInstance(locale, utteranceReader); } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new IOException("Could not create Speechlet from speechlet request", e); } }
From source file:io.helixservice.feature.configuration.locator.AbstractResourceLocator.java
/** * {@inheritDoc}//from www. ja v a2s .co m */ @Override public Optional<JsonObject> getJsonObject(String resourcePath) { Optional<String> string = getString(resourcePath); if (string.isPresent()) { return Optional.of(new JsonObject(string.get())); } else { return Optional.empty(); } }
From source file:com.ikanow.aleph2.data_model.utils.PropertiesUtils.java
/** Returns the sub-config of a config, or empty if * @param config the configuration object * @param key the root key of the sub object * @return/* w w w .ja v a2 s .c o m*/ */ public static Optional<Config> getSubConfig(final Config config, final String key) { try { return Optional.of(config.getConfig(key)); } catch (Exception e) { return Optional.empty(); } }
From source file:nu.yona.server.exceptions.UpstreamException.java
private UpstreamException(HttpStatus statusCode, String messageId, String message) { super(statusCode, messageId); this.message = Optional.of(message); }
From source file:org.zalando.riptide.StatusCodeSelector.java
@Override public Optional<Integer> attributeOf(final ClientHttpResponse response) throws IOException { return Optional.of(response.getRawStatusCode()); }
From source file:com.qpark.eip.core.model.analysis.operation.GetFlowOperation.java
/** * Translate the pattern do SQL <i>like</i>. * * @param namePattern//w w w . j a v a 2 s . c om * the given name pattern. * @return the translated pattern. */ private static Optional<String> translateNamePattern(final String namePattern) { Optional<String> value = Optional.empty(); if (Objects.nonNull(namePattern)) { String s = namePattern.replace('*', '%'); if (!s.endsWith("%")) { s = String.format("%s%s", s, "%"); } if (!s.startsWith("%")) { s = String.format("%s%s", "%", s); } value = Optional.of(s); } return value; }
From source file:io.github.mywarp.mywarp.bukkit.util.versionsupport.MinecraftLocaleParser.java
/** * Parses the given String into a Locale. * * <p>Returns an Optional with the resulting Locale or Optional.absent() if the String cannot be parsed into a * Locale.</p>/* w w w. j a va2s . c o m*/ * * @param rawLocale the String to convert * @return an Optional with the parsed Locale */ static Optional<Locale> parseLocale(String rawLocale) { Locale locale = cache.get(rawLocale); if (locale == null) { try { locale = toLocaleCaseInsensitive(rawLocale); } catch (RuntimeException e) { return Optional.empty(); } cache.put(rawLocale, locale); } return Optional.of(locale); }
From source file:org.zalando.riptide.StatusSelector.java
@Override public Optional<HttpStatus> attributeOf(final ClientHttpResponse response) throws IOException { return Optional.of(response.getStatusCode()); }