@SafeVarargs public static <T> Optional<T> findFirstPresent(Supplier<Optional<T>>... suppliers) { return Stream.of(suppliers) .flatMap(supplier -> toStream(supplier.get())) .findFirst(); } }
/** * Obtain an {@link SqlArrayType} for the given array element type in the given context * * @param elementType the array element type. * @return an {@link SqlArrayType} for the given element type. */ public Optional<SqlArrayType<?>> findFor(Type elementType) { return factories.stream() .flatMap(factory -> toStream(factory.build(elementType, registry))) .findFirst(); }
/** * Obtain an argument for given value in the given context * * @param type the type of the argument. * @param value the argument value. * @return an Argument for the given value. */ public Optional<Argument> findFor(Type type, Object value) { return argumentFactories.stream() .flatMap(factory -> toStream(factory.build(type, value, registry))) .findFirst(); }
/** * Look up an argument by name * * @param name the key to lookup the value of * @param ctx the statement context * * @return the bound Argument */ public Optional<Argument> findForName(String name, StatementContext ctx) { if (named.containsKey(name)) { return Optional.of(named.get(name)); } return namedArgumentFinder.stream() .flatMap(arguments -> toStream(arguments.find(name, ctx))) .findFirst(); }
/** * Obtain a column mapper for the given type. * * @param type the target type to map to * @return a ColumnMapper for the given type, or empty if no column mapper is registered for the given type. */ public Optional<ColumnMapper<?>> findFor(Type type) { // ConcurrentHashMap can enter an infinite loop on nested computeIfAbsent calls. // Since column mappers can decorate other column mappers, we have to populate the cache the old fashioned way. // See https://bugs.openjdk.java.net/browse/JDK-8062841, https://bugs.openjdk.java.net/browse/JDK-8142175 ColumnMapper<?> cached = cache.get(type); if (cached != null) { return Optional.of(cached); } Optional<ColumnMapper<?>> mapper = factories.stream() .flatMap(factory -> toStream(factory.build(type, registry))) .findFirst(); mapper.ifPresent(m -> cache.put(type, m)); return mapper; }
/** * Obtain a row mapper for the given type in the given context. * * @param type the target type to map to * @return a RowMapper for the given type, or empty if no row mapper is registered for the given type. */ public Optional<RowMapper<?>> findFor(Type type) { // ConcurrentHashMap can enter an infinite loop on nested computeIfAbsent calls. // Since row mappers can decorate other row mappers, we have to populate the cache the old fashioned way. // See https://bugs.openjdk.java.net/browse/JDK-8062841, https://bugs.openjdk.java.net/browse/JDK-8142175 RowMapper<?> cached = cache.get(type); if (cached != null) { return Optional.of(cached); } Optional<RowMapper<?>> mapper = findFirstPresent( () -> factories.stream() .flatMap(factory -> toStream(factory.build(type, registry))) .findFirst(), () -> registry.get(ColumnMappers.class).findFor(type) .map(SingleColumnMapper::new)); mapper.ifPresent(m -> cache.put(type, m)); return mapper; }