All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.digipost.jdbc.AttributesRowMapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) Posten Norge AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package no.digipost.jdbc;

import no.digipost.util.AttributesMap;
import no.digipost.util.AttributesMap.Builder;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toMap;
import static java.util.stream.IntStream.rangeClosed;
import static no.digipost.DiggExceptions.asUnchecked;

/**
 * A {@link RowMapper} producing {@link AttributesMap}s.
 */
public class AttributesRowMapper implements RowMapper {

    private final Supplier attributeMapBuilderSupplier;
    private final Map> mappers;

    public AttributesRowMapper(AttributeMapper ... mappers) {
        this(AttributesMap::buildNew, mappers);
    }

    public AttributesRowMapper(Supplier attributeMapBuilderSupplier, AttributeMapper ... mappers) {
        this(attributeMapBuilderSupplier, Stream.of(mappers));
    }

    public AttributesRowMapper(Stream> mappers) {
        this(AttributesMap::buildNew, mappers);
    }

    public AttributesRowMapper(Supplier attributeMapBuilderSupplier, Stream> mappers) {
        this.attributeMapBuilderSupplier = attributeMapBuilderSupplier;
        this.mappers = mappers.collect(toMap(AttributeMapper::getAttributeName, Function.identity()));
    }


    @Override
    public AttributesMap map(ResultSet rs) throws SQLException {
        AttributesMap.Builder attributes = attributeMapBuilderSupplier.get();
        Iterator> mapperIter = applicableMappers(rs.getMetaData()).iterator();
        while (mapperIter.hasNext()) {
            attributes.and(mapperIter.next().attributeAndValue(rs));
        }
        return attributes.build();
    }

    private Stream> applicableMappers(ResultSetMetaData metadata) throws SQLException {
        return rangeClosed(1, metadata.getColumnCount())
                .limit(metadata.getColumnCount())
                .mapToObj(i -> {
                    try {
                        return metadata.getColumnLabel(i);
                    } catch (SQLException e) {
                        throw asUnchecked(e);
                    }
                })
                .filter(mappers::containsKey)
                .map(mappers::get);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy