@Test public void testStringValueAndMapWithDefault() { Long defaultValue = 2L; put(key, value); assertEquals((Long) 1L, ConnectorOptions.extractOptionAndMap(parameters, key, this::convertLong, defaultValue)); assertEquals(defaultValue, ConnectorOptions.extractOptionAndMap(parameters, missingKey, this::convertLong, defaultValue)); }
public static <T> T extractOptionAndMap(Map<String, ?> options, String key, Function<? super String, T> mappingFn, T defaultValue) { if (options == null) { return defaultValue; } try { return Optional.ofNullable(options.get(key)) .map(Object::toString) .filter(v -> v.length() > 0) .map(mappingFn) .orElse(defaultValue); } catch (Exception ex) { LOG.error("Evaluation of option '" + key + "' failed. Returning default value.", ex); return defaultValue; } }
ConnectorOptions { public static <T> T extractOptionAndMap(Map<String, ?> options, String key, Function<? super String, T> mappingFn, T defaultValue) { if (options == null) { return defaultValue; } try { return Optional.ofNullable(options.get(key)) .map(Object::toString) .filter(v -> v.length() > 0) .map(mappingFn) .orElse(defaultValue); } catch (Exception ex) { LOG.error("Evaluation of option '" + key + "' failed. Returning default value.", ex); return defaultValue; } } }
ConnectorOptions { public static <T> T extractOptionAndMap(Map<String, ?> options, String key, Function<? super String, T> mappingFn, T defaultValue) { if (options == null) { return defaultValue; } try { return Optional.ofNullable(options.get(key)) .map(Object::toString) .filter(v -> v.length() > 0) .map(mappingFn) .orElse(defaultValue); } catch (Exception ex) { LOG.error("Evaluation of option '" + key + "' failed. Returning default value.", ex); return defaultValue; } } private  ConnectorOptions(); }
ConnectorOptions { public static <T> T extractOptionAndMap(Map<String, ?> options, String key, Function<? super String, T> mappingFn, T defaultValue) { if (options == null) { return defaultValue; } try { return Optional.ofNullable(options.get(key)) .map(Object::toString) .filter(v -> v.length() > 0) .map(mappingFn) .orElse(defaultValue); } catch (Exception ex) { LOG.error("Evaluation of option '" + key + "' failed. Returning default value.", ex); return defaultValue; } } private  ConnectorOptions(); static String extractOption(Map<String, ?> options, String key, String defaultValue); static String extractOption(Map<String, ?> options, String key); static String popOption(Map<String, ?> options, String key); static T extractOptionAndMap(Map<String, ?> options, String key,
                                                                    Function<? super String, T> mappingFn, T defaultValue); static T extractOptionAndMap(Map<String, ?> options, String key,
                                                                    Function<? super String, T> mappingFn); static void extractOptionAndConsume(Map<String, ?> options, String key,
                                                                   Consumer<String> consumer); static T extractOptionAsType(Map<String, ?> options,
                                                            String key, Class<T> requiredClass, T defaultValue); static T extractOptionAsType(Map<String, ?> options,
                                                            String key, Class<T> requiredClass); }
ConnectorOptions { public static <T> T extractOptionAndMap(Map<String, ?> options, String key, Function<? super String, T> mappingFn, T defaultValue) { if (options == null) { return defaultValue; } try { return Optional.ofNullable(options.get(key)) .map(Object::toString) .filter(v -> v.length() > 0) .map(mappingFn) .orElse(defaultValue); } catch (Exception ex) { LOG.error("Evaluation of option '" + key + "' failed. Returning default value.", ex); return defaultValue; } } private  ConnectorOptions(); static String extractOption(Map<String, ?> options, String key, String defaultValue); static String extractOption(Map<String, ?> options, String key); static String popOption(Map<String, ?> options, String key); static T extractOptionAndMap(Map<String, ?> options, String key,
                                                                    Function<? super String, T> mappingFn, T defaultValue); static T extractOptionAndMap(Map<String, ?> options, String key,
                                                                    Function<? super String, T> mappingFn); static void extractOptionAndConsume(Map<String, ?> options, String key,
                                                                   Consumer<String> consumer); static T extractOptionAsType(Map<String, ?> options,
                                                            String key, Class<T> requiredClass, T defaultValue); static T extractOptionAsType(Map<String, ?> options,
                                                            String key, Class<T> requiredClass);  }