Skip to content

Commit 4e30916

Browse files
authored
refactor: codehealth rename types to reflect axis of transformation (#1451)
this is entirely a change for code clarity, because if you read (before this patch) the `AnyToAnyImporter#import` generic-params you'll see `To` being passed in as the source of data (what should be a conceptual "From"). The cause of that confusion is that there's multiple different orthogonal transformations happening with/around the `TransferCompatibilityProvider` and its supporting `AnyToAnyImporter` class and DTP generally: 1. DTP moves data from a Exporter producing models to an Importer consuming models 1. we have static functions that convert model A to model B (eg: from Photos model to a Media model via [`MediaContainerResource::photoToMedia`][pToMedia]) - this is parallel to the `From` and `To` generic params of [`java.util.function.Function<From, To>`][Function]. 1. `TransferCompatibilityProvider` converts *an importer* (via `AnyToAnyImporter` and a model-transformer like above) from an importer of one model type to an importer of another model type. I'm now using the language of "stand-in importer" to indicate which importer was selected as the input (think the `g` in `f(g(x)`) to indicate the key moving piece in that last kind of transformation listed above. [Function]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/function/Function.html [pToMedia]: https://github.com/dtinit/data-transfer-project/blob/bca0c7926fb871c7fdd7e206908a87874ec624f2/portability-types-common/src/main/java/org/datatransferproject/types/common/models/media/MediaContainerResource.java#L45
1 parent 6ac51d3 commit 4e30916

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

portability-spi-transfer/src/main/java/org/datatransferproject/spi/transfer/provider/TransferCompatibilityProvider.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Exporter getCompatibleExporter(TransferExtension extension, DataVertical
4141
break;
4242
}
4343
if (exporter == null) {
44-
return extension.getExporter(jobType); // preserve original exception
44+
return extension.getExporter(jobType); // re-execute just for the potential exception
4545
}
4646
return exporter;
4747
}
@@ -64,25 +64,25 @@ public Importer getCompatibleImporter(TransferExtension extension, DataVertical
6464
break;
6565
}
6666
if (importer == null) {
67-
return extension.getImporter(jobType);
67+
return extension.getImporter(jobType); // re-execute just for the potential exception
6868
}
6969
return importer;
7070
}
7171

7272
private Importer<?, ?> getVideosImporter(TransferExtension extension) {
73-
Importer media = getImporterOrNull(extension, MEDIA);
74-
if (media == null) {
73+
Importer mediaImporter = getImporterOrNull(extension, MEDIA);
74+
if (mediaImporter == null) {
7575
return null;
7676
}
77-
return new AnyToAnyImporter<>(media, MediaContainerResource::videoToMedia);
77+
return new AnyToAnyImporter<>(mediaImporter, MediaContainerResource::videoToMedia);
7878
}
7979

8080
private Importer<?, ?> getPhotosImporter(TransferExtension extension) {
81-
Importer media = getImporterOrNull(extension, MEDIA);
82-
if (media == null) {
81+
Importer mediaImporter = getImporterOrNull(extension, MEDIA);
82+
if (mediaImporter == null) {
8383
return null;
8484
}
85-
return new AnyToAnyImporter<>(media, MediaContainerResource::photoToMedia);
85+
return new AnyToAnyImporter<>(mediaImporter, MediaContainerResource::photoToMedia);
8686
}
8787

8888
private Importer<?, ?> getMediaImporter(TransferExtension extension) {

portability-spi-transfer/src/main/java/org/datatransferproject/spi/transfer/provider/converter/AnyToAnyImporter.java

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,61 @@
1111
/**
1212
* Allows flexible bridging between adapters of different types with compatible functionality.
1313
*
14-
* @param <From> The container type supported by the available exporter.
15-
* @param <To> The container type that's desired.
14+
* ## Usage
15+
*
16+
* DTP transfers data by passing models from an Exporter (producing bytes)
17+
* to an {@link Importer} (consuming bytes). Models have a 1:1 correspondance to
18+
* {@link org.datatransferproject.types.common.models.DataVertical} (so a PHOTO
19+
* vertical is universally assumed in the DTP codebase to be used for
20+
* PhotosContainerResource transfers).
21+
*
22+
* ### Example Usage
23+
*
24+
* Given:
25+
*
26+
* - a DataVertical.PHOTO transfer
27+
* - a matching Exporter: an adapter producing {@link PhotosContainerResource} data
28+
* - a stand-in Importer: a good-enough adapter; something intended for a
29+
* semi-compatible but not identical data type (eg: {@link MediaContainerResource}).
30+
* - a conversion function to go from said PHOTOS objects to DataVertical.MEDIA
31+
* objects (eg: {@link MediaContainerResource} data).
32+
*
33+
* Then: one can create a synthetic Importer with this class by constructing with said conversion function.
34+
*
35+
* @param <StandinImporterType> Container type in which some extant exporter is
36+
* providing data. eg: MediaContainerResource.
37+
* @param <ExportingType> The original container type which is being exported
38+
* (that we will convert before arriving at our stand-in importer); eg:
39+
* PhotosContainerResource.
1640
*/
1741
public class AnyToAnyImporter<
1842
AD extends AuthData,
19-
From extends ContainerResource,
20-
To extends ContainerResource> implements Importer<AD, To> {
43+
StandinImporterType extends ContainerResource,
44+
ExportingType extends ContainerResource> implements Importer<AD, ExportingType> {
2145

22-
private final Importer<AD, From> importer;
23-
private final Function<To, From> converter;
46+
private final Importer<AD, StandinImporterType> standinImporter;
47+
private final Function<ExportingType, StandinImporterType> modelConverter;
2448

2549
/**
26-
* @param importer existing importer
27-
* @param converter function converting between the existing and desired containers.
50+
* @param standinImporter existing importer
51+
* @param modelConverter function converting between some existing
52+
* exporters' type to our stand-in importers' container types.
2853
*/
29-
public AnyToAnyImporter(Importer<AD, From> importer, Function<To, From> converter) {
30-
this.importer = importer;
31-
this.converter = converter;
54+
public AnyToAnyImporter(
55+
Importer<AD, StandinImporterType> standinImporter,
56+
Function<ExportingType, StandinImporterType> modelConverter
57+
) {
58+
this.standinImporter = standinImporter;
59+
this.modelConverter = modelConverter;
3260
}
3361

3462
@Override
35-
public ImportResult importItem(UUID jobId, IdempotentImportExecutor idempotentExecutor,
36-
AD authData, To data) throws Exception {
37-
return importer.importItem(jobId, idempotentExecutor, authData, converter.apply(data));
63+
public ImportResult importItem(
64+
UUID jobId,
65+
IdempotentImportExecutor idempotentExecutor,
66+
AD authData,
67+
ExportingType data
68+
) throws Exception {
69+
return standinImporter.importItem(jobId, idempotentExecutor, authData, modelConverter.apply(data));
3870
}
3971
}

0 commit comments

Comments
 (0)