Skip to content

Commit

Permalink
refactor: improve TypeTransformer related interfaces (#2350)
Browse files Browse the repository at this point in the history
* Avoid duplication between type transformer registries

* Make transformer context handle eventual transformation's failure

* Make input object on transformer not nullable, cleanup

* Make the context call the transformer directly

* Fix project
  • Loading branch information
ndr-brt authored Dec 16, 2022
1 parent c733d1a commit 857cb5d
Show file tree
Hide file tree
Showing 97 changed files with 420 additions and 1,376 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,104 +16,11 @@

import org.eclipse.edc.protocol.ids.spi.transform.IdsTransformerRegistry;
import org.eclipse.edc.protocol.ids.spi.transform.IdsTypeTransformer;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.result.Result;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.eclipse.edc.transform.spi.TransformerContextImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.eclipse.edc.transform.spi.TypeTransformerRegistryImpl;

/**
* Implements a {@link IdsTransformerRegistry} that recursively dispatches to transformers for type conversion.
* Marker class to allow for a type-safe registry that only holds implementations of the {@link IdsTypeTransformer} interface.
*/
public class IdsTransformerRegistryImpl implements IdsTransformerRegistry {
private final Map<TransformKey, IdsTypeTransformer<?, ?>> transformers = new HashMap<>();

@Override
public void register(IdsTypeTransformer<?, ?> transformer) {
Objects.requireNonNull(transformer);
transformers.put(new TransformKey(transformer.getInputType(), transformer.getOutputType()), transformer);
}

@Override
public <INPUT, OUTPUT> Result<OUTPUT> transform(@NotNull INPUT object, @NotNull Class<OUTPUT> outputType) {
var context = new TransformerContextImpl(this);
var output = transform(object, outputType, context);
return context.hasProblems() ? Result.failure(context.getProblems()) : Result.success(output);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private <INPUT, OUTPUT> @Nullable OUTPUT transform(INPUT object, Class<OUTPUT> outputType, TransformerContext context) {
Objects.requireNonNull(object);

IdsTypeTransformer idsTypeTransformer = findEligibleTransformer(object, outputType);
if (idsTypeTransformer == null) {
throw new EdcException("Transformer not found for pair:" + new TransformKey(object.getClass(), outputType)); // this is a programming error
}
return outputType.cast(idsTypeTransformer.transform(object, context));
}

@SuppressWarnings({ "unchecked" })
private <INPUT, OUTPUT> IdsTypeTransformer<INPUT, OUTPUT> findEligibleTransformer(INPUT object, Class<OUTPUT> outputType) {
IdsTypeTransformer<INPUT, OUTPUT> idsTypeTransformer;

Class<?> inputClass = object.getClass();
do {
idsTypeTransformer = (IdsTypeTransformer<INPUT, OUTPUT>) transformers.get(new TransformKey(inputClass, outputType));
if (idsTypeTransformer == null) {
for (Class<?> anInterface : inputClass.getInterfaces()) {
idsTypeTransformer = (IdsTypeTransformer<INPUT, OUTPUT>) transformers.get(new TransformKey(anInterface, outputType));
if (idsTypeTransformer != null) {
break;
}
}
}

inputClass = inputClass.getSuperclass();
} while (inputClass != null && idsTypeTransformer == null);

return idsTypeTransformer;
}

private static class TransformKey {
private final Class<?> input;
private final Class<?> output;

TransformKey(Class<?> input, Class<?> output) {
Objects.requireNonNull(input);
Objects.requireNonNull(output);
this.input = input;
this.output = output;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TransformKey transformKey = (TransformKey) o;
return input.equals(transformKey.input) && output.equals(transformKey.output);
}

@Override
public int hashCode() {
return Objects.hash(input, output);
}

@Override
public String toString() {
return "TKey{" +
"input=" + input +
", output=" + output +
'}';
}
}
public class IdsTransformerRegistryImpl extends TypeTransformerRegistryImpl<IdsTypeTransformer<?, ?>> implements IdsTransformerRegistry {

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.UUID;

public class AssetFromIdsArtifactTransformer implements IdsTypeTransformer<Artifact, Asset> {
Expand All @@ -40,12 +39,7 @@ public Class<Asset> getOutputType() {
}

@Override
public @Nullable Asset transform(Artifact object, @NotNull TransformerContext context) {
Objects.requireNonNull(context);
if (object == null) {
return null;
}

public @Nullable Asset transform(@NotNull Artifact object, @NotNull TransformerContext context) {
var result = IdsId.from(object.getId());
if (result.failed()) {
context.reportProblem("id of incoming IDS artifact expected to be not null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.UUID;

/**
Expand All @@ -46,12 +45,7 @@ public Class<Asset> getOutputType() {
}

@Override
public @Nullable Asset transform(Representation object, @NotNull TransformerContext context) {
Objects.requireNonNull(context);
if (object == null) {
return null;
}

public @Nullable Asset transform(@NotNull Representation object, @NotNull TransformerContext context) {
var result = IdsId.from(object.getId());
if (result.failed()) {
context.reportProblem("id of incoming IDS representation expected to be not null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.UUID;

/**
Expand All @@ -48,12 +47,7 @@ public Class<Asset> getOutputType() {
}

@Override
public @Nullable Asset transform(Resource object, @NotNull TransformerContext context) {
Objects.requireNonNull(context);
if (object == null) {
return null;
}

public @Nullable Asset transform(@NotNull Resource object, @NotNull TransformerContext context) {
var result = IdsId.from(object.getId());
if (result.failed()) {
context.reportProblem("id of incoming IDS resource expected to be not null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.jetbrains.annotations.Nullable;

import java.math.BigInteger;
import java.util.Objects;

public class AssetToIdsArtifactTransformer implements IdsTypeTransformer<Asset, Artifact> {

Expand All @@ -41,12 +40,7 @@ public Class<Artifact> getOutputType() {
}

@Override
public @Nullable Artifact transform(Asset object, @NotNull TransformerContext context) {
Objects.requireNonNull(context);
if (object == null) {
return null;
}

public @Nullable Artifact transform(@NotNull Asset object, @NotNull TransformerContext context) {
var id = IdsId.Builder.newInstance().value(object.getId()).type(IdsType.ARTIFACT).build().toUri();
var builder = new ArtifactBuilder(id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;

public class AssetToIdsRepresentationTransformer implements IdsTypeTransformer<Asset, Representation> {

Expand All @@ -44,12 +43,7 @@ public Class<Representation> getOutputType() {
}

@Override
public @Nullable Representation transform(Asset object, @NotNull TransformerContext context) {
Objects.requireNonNull(context);
if (object == null) {
return null;
}

public @Nullable Representation transform(@NotNull Asset object, @NotNull TransformerContext context) {
var artifact = context.transform(object, Artifact.class);
var id = IdsId.Builder.newInstance().value(object.getId()).type(IdsType.REPRESENTATION).build().toUri();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;

public class AssetToIdsResourceTransformer implements IdsTypeTransformer<Asset, Resource> {

Expand All @@ -47,12 +46,7 @@ public Class<Resource> getOutputType() {
}

@Override
public @Nullable Resource transform(Asset object, @NotNull TransformerContext context) {
Objects.requireNonNull(context);
if (object == null) {
return null;
}

public @Nullable Resource transform(@NotNull Asset object, @NotNull TransformerContext context) {
var result = context.transform(object, Representation.class);
var id = IdsId.Builder.newInstance().value(object.getId()).type(IdsType.RESOURCE).build().toUri();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ public Class<Resource> getOutputType() {
}

@Override
public @Nullable Resource transform(OfferedAsset object, @NotNull TransformerContext context) {
if (object == null) {
return null;
}

public @Nullable Resource transform(@NotNull OfferedAsset object, @NotNull TransformerContext context) {
var asset = object.getAsset();
var id = IdsId.Builder.newInstance().value(asset.getId()).type(IdsType.RESOURCE).build().toUri();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.jetbrains.annotations.Nullable;

import java.util.LinkedList;
import java.util.Objects;

public class CatalogFromIdsResourceCatalogTransformer implements IdsTypeTransformer<ResourceCatalog, Catalog> {
@Override
Expand All @@ -42,12 +41,7 @@ public Class<Catalog> getOutputType() {
}

@Override
public @Nullable Catalog transform(@Nullable ResourceCatalog object, @NotNull TransformerContext context) {
Objects.requireNonNull(context);
if (object == null) {
return null;
}

public @Nullable Catalog transform(@NotNull ResourceCatalog object, @NotNull TransformerContext context) {
var builder = Catalog.Builder.newInstance();

var result = IdsId.from(object.getId().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Objects;
import java.util.stream.Collectors;

public class CatalogToIdsResourceCatalogTransformer implements IdsTypeTransformer<Catalog, ResourceCatalog> {
Expand All @@ -45,12 +44,7 @@ public Class<ResourceCatalog> getOutputType() {
}

@Override
public @Nullable ResourceCatalog transform(Catalog object, @NotNull TransformerContext context) {
Objects.requireNonNull(context);
if (object == null) {
return null;
}

public @Nullable ResourceCatalog transform(@NotNull Catalog object, @NotNull TransformerContext context) {
var id = IdsId.Builder.newInstance().value(object.getId()).type(IdsType.CATALOG).build().toUri();
var builder = new ResourceCatalogBuilder(id);

Expand Down
Loading

0 comments on commit 857cb5d

Please sign in to comment.