Skip to content

Commit

Permalink
Move DataSet to main package
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Feb 18, 2022
1 parent f8be442 commit 52a1a59
Show file tree
Hide file tree
Showing 48 changed files with 312 additions and 337 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ public DataStructure getStructure(DataflowRef flowRef) throws IOException {
}

@Override
public Collection<Series> getData(DataRef dataRef) throws IOException {
public DataSet getData(DataRef dataRef) throws IOException {
Objects.requireNonNull(dataRef);

Collection<Series> result;
DataSet result;

try {
result = delegate.getData(dataRef);
Expand Down
18 changes: 17 additions & 1 deletion sdmx-dl-api/src/main/java/sdmxdl/DataRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,28 @@
*/
package sdmxdl;

import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.Objects;

/**
* @author Philippe Charles
*/
@lombok.Value(staticConstructor = "of")
@lombok.Value
@lombok.Builder(toBuilder = true)
public class DataRef {

public static @NonNull DataRef of(@NonNull DataflowRef flowRef, @NonNull Key key, @NonNull DataFilter filter) {
Objects.requireNonNull(flowRef);
Objects.requireNonNull(key);
Objects.requireNonNull(filter);
return new DataRef(flowRef, key, filter);
}

public static @NonNull DataRef of(@NonNull DataflowRef flowRef) {
return of(flowRef, Key.ALL, DataFilter.FULL);
}

@lombok.NonNull
DataflowRef flowRef;

Expand Down
78 changes: 78 additions & 0 deletions sdmx-dl-api/src/main/java/sdmxdl/DataSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2020 National Bank of Belgium
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved
* by the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
package sdmxdl;

import nbbrd.design.MightBePromoted;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author Philippe Charles
*/
@lombok.Value
@lombok.Builder(toBuilder = true)
@lombok.EqualsAndHashCode(callSuper = false)
public class DataSet {

@lombok.NonNull
DataRef ref;

@lombok.NonNull
@lombok.Singular("series")
Collection<Series> data;

public @NonNull DataSet getData(@NonNull DataRef other) {
Objects.requireNonNull(other);
checkFlowRef(other.getFlowRef());
return isNoFilter(other) ? this : filter(data.stream(), other).collect(toDataSet(other));
}

public @NonNull Stream<Series> getDataStream(@NonNull DataRef other) {
Objects.requireNonNull(other);
checkFlowRef(other.getFlowRef());
return isNoFilter(other) ? data.stream() : filter(data.stream(), other);
}

private void checkFlowRef(DataflowRef flowRef) throws IllegalArgumentException {
Objects.requireNonNull(flowRef);
if (!ref.getFlowRef().contains(flowRef)) {
throw new IllegalArgumentException(flowRef.toString());
}
}

public static @NonNull Collector<Series, ?, DataSet> toDataSet(@NonNull DataRef ref) {
Objects.requireNonNull(ref);
return Collectors.collectingAndThen(Collectors.toList(), list -> DataSet.builder().ref(ref).data(list).build());
}

@MightBePromoted
private static Stream<Series> filter(Stream<Series> data, DataRef filter) {
return data
.filter(filter.getKey()::containsKey)
.map(filter.getFilter()::apply);
}

@MightBePromoted
private static boolean isNoFilter(@NonNull DataRef ref) {
return Key.ALL.equals(ref.getKey()) && DataFilter.FULL.equals(ref.getFilter());
}
}
2 changes: 0 additions & 2 deletions sdmx-dl-api/src/main/java/sdmxdl/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import nbbrd.design.SealedType;
import org.checkerframework.checker.nullness.qual.NonNull;
import sdmxdl.repo.DataSet;


/**
Expand All @@ -27,7 +26,6 @@
* @author Philippe Charles
*/
@SealedType({
DataSet.class,
DataStructure.class,
Dataflow.class,
Codelist.class
Expand Down
15 changes: 5 additions & 10 deletions sdmx-dl-api/src/main/java/sdmxdl/SdmxConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,15 @@ public interface SdmxConnection extends Closeable {

void testConnection() throws IOException;

@NonNull
Collection<Dataflow> getFlows() throws IOException;
@NonNull Collection<Dataflow> getFlows() throws IOException;

@NonNull
Dataflow getFlow(@NonNull DataflowRef flowRef) throws IOException, IllegalArgumentException;
@NonNull Dataflow getFlow(@NonNull DataflowRef flowRef) throws IOException, IllegalArgumentException;

@NonNull
DataStructure getStructure(@NonNull DataflowRef flowRef) throws IOException, IllegalArgumentException;
@NonNull DataStructure getStructure(@NonNull DataflowRef flowRef) throws IOException, IllegalArgumentException;

@NonNull
Collection<Series> getData(@NonNull DataRef dataRef) throws IOException, IllegalArgumentException;
@NonNull DataSet getData(@NonNull DataRef dataRef) throws IOException, IllegalArgumentException;

@NonNull
Stream<Series> getDataStream(@NonNull DataRef dataRef) throws IOException, IllegalArgumentException;
@NonNull Stream<Series> getDataStream(@NonNull DataRef dataRef) throws IOException, IllegalArgumentException;

boolean isDetailSupported() throws IOException;
}
12 changes: 2 additions & 10 deletions sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import sdmxdl.*;

import java.io.IOException;
import java.util.Collection;
import java.util.stream.Stream;

/**
* @author Philippe Charles
Expand All @@ -48,13 +46,7 @@ default DataStructure getStructure() throws IOException {
return getStructure(getDataflowRef());
}

@NonNull
default Collection<Series> getData(@NonNull Key key, @NonNull DataFilter filter) throws IOException, IllegalArgumentException {
return getData(DataRef.of(getDataflowRef(), key, filter));
}

@NonNull
default Stream<Series> getDataStream(@NonNull Key key, @NonNull DataFilter filter) throws IOException, IllegalArgumentException {
return getDataStream(DataRef.of(getDataflowRef(), key, filter));
default @NonNull DataRef getDataSetRef(@NonNull Key key, @NonNull DataFilter filter) throws IOException {
return DataRef.of(getDataflowRef(), key, filter);
}
}
95 changes: 0 additions & 95 deletions sdmx-dl-api/src/main/java/sdmxdl/repo/DataSet.java

This file was deleted.

8 changes: 4 additions & 4 deletions sdmx-dl-api/src/main/java/sdmxdl/repo/SdmxRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Optional<DataSet> getDataSet(@NonNull DataflowRef ref) {
Objects.requireNonNull(ref);
return dataSets
.stream()
.filter(ref::containsRef)
.filter(dataSet -> ref.contains(dataSet.getRef().getFlowRef()))
.findFirst();
}

Expand Down Expand Up @@ -146,11 +146,11 @@ public DataStructure getStructure(DataflowRef flowRef) throws IOException {
}

@Override
public Collection<Series> getData(DataRef dataRef) throws IOException {
public DataSet getData(DataRef dataRef) throws IOException {
checkState();
return repo
.getDataSet(dataRef.getFlowRef())
.map(dataSet -> dataSet.getData(dataRef.getKey(), dataRef.getFilter()))
.map(dataSet -> dataSet.getData(dataRef))
.orElseThrow(() -> SdmxException.missingData(repo.getName(), dataRef));
}

Expand All @@ -159,7 +159,7 @@ public Stream<Series> getDataStream(DataRef dataRef) throws IOException {
checkState();
return repo
.getDataSet(dataRef.getFlowRef())
.map(dataSet -> dataSet.getDataStream(dataRef.getKey(), dataRef.getFilter()))
.map(dataSet -> dataSet.getDataStream(dataRef))
.orElseThrow(() -> SdmxException.missingData(repo.getName(), dataRef));
}

Expand Down
11 changes: 5 additions & 6 deletions sdmx-dl-api/src/test/java/_test/sdmxdl/TestConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -55,13 +54,13 @@ public DataStructure getStructure(DataflowRef flowRef) {
}

@Override
public List<Series> getData(DataRef dataRef) {
public DataSet getData(DataRef dataRef) {
return DATA;
}

@Override
public Stream<Series> getDataStream(DataRef dataRef) {
return DATA.stream();
return DATA.getData().stream();
}

@Override
Expand Down Expand Up @@ -100,7 +99,7 @@ public DataStructure getStructure(DataflowRef flowRef) {
}

@Override
public List<Series> getData(DataRef dataRef) {
public DataSet getData(DataRef dataRef) {
throw new CustomException();
}

Expand Down Expand Up @@ -145,7 +144,7 @@ public DataStructure getStructure(DataflowRef flowRef) {
}

@Override
public List<Series> getData(DataRef dataRef) {
public DataSet getData(DataRef dataRef) {
return null;
}

Expand All @@ -172,7 +171,7 @@ public void close() {
public static final DataStructureRef STRUCT_REF = DataStructureRef.parse("struct");
public static final Dataflow FLOW = Dataflow.of(FLOW_REF, STRUCT_REF, "label");
public static final DataStructure STRUCT = DataStructure.builder().ref(STRUCT_REF).primaryMeasureId("").label("").build();
public static final List<Series> DATA = Collections.emptyList();
public static final DataSet DATA = DataSet.builder().ref(DataRef.of(FLOW_REF)).build();
public static final Key KEY = Key.ALL;
public static final DataFilter FILTER = DataFilter.FULL;
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void testGetDataStream() throws IOException {

failsafe.reset();
Assertions.assertThat(validDriver.getDataStream(dataRef))
.containsExactlyElementsOf(TestConnection.DATA);
.containsExactlyElementsOf(TestConnection.DATA.getData());
failsafe.assertEmpty();

failsafe.reset();
Expand Down
Loading

0 comments on commit 52a1a59

Please sign in to comment.