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

sdmxdl.desktop.SingleSeries Maven / Gradle / Ivy

The newest version!
package sdmxdl.desktop;

import lombok.NonNull;
import sdmxdl.*;
import sdmxdl.provider.ext.SeriesMeta;
import sdmxdl.provider.ext.SeriesMetaFactory;
import sdmxdl.web.SdmxWebManager;

import java.io.IOException;

import static internal.sdmxdl.desktop.Collectors2.single;

@lombok.Value
public class SingleSeries {

    @NonNull Structure dsd;

    @NonNull Series series;

    @lombok.Getter(lazy = true)
    @NonNull SeriesMeta meta = SeriesMetaFactory.getDefault(getDsd()).get(getSeries());

    @lombok.Getter(lazy = true)
    Duration duration = computeGlobalDuration();

    public static SingleSeries load(SdmxWebManager manager, Languages languages, DataSetRef ref) throws IOException {
        try (Connection conn = manager.getConnection(ref.getDataSourceRef().getSource(), languages)) {
            return new SingleSeries(
                    conn.getStructure(ref.getDataSourceRef().getFlow()),
                    conn.getDataStream(ref.getDataSourceRef().getFlow(), Query.builder().key(ref.getKey()).build())
                            .findFirst()
                            .orElseGet(() -> Series.builder().key(ref.getKey()).build())
            );
        }
    }

    private Duration computeGlobalDuration() {
        return getSeries()
                .getObs()
                .stream()
                .map(Obs::getPeriod)
                .map(TimeInterval::getDuration)
                .distinct()
                .collect(single())
                .orElse(null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy