Skip to content

Commit

Permalink
Static method for resolving valid complementation
Browse files Browse the repository at this point in the history
  • Loading branch information
runeflobakk committed Dec 4, 2018
1 parent 918b1c7 commit 1a0f387
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
11 changes: 2 additions & 9 deletions src/main/java/no/digipost/api/datatypes/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import no.digipost.api.datatypes.validation.ComplementedBy;

import java.util.stream.Stream;

public interface DataType {

Expand All @@ -21,12 +18,8 @@ default DataTypeIdentifier getTypeIdentifier() {
default DataType withDefaultsForMissingOptionalValues() {
return this;
}

default boolean canBeComplementedBy(DataType target) {
return Stream.of(getClass().getAnnotationsByType(ComplementedBy.class))
.flatMap(a -> Stream.of(a.value()))
.anyMatch(
clazz -> clazz == target.getTypeIdentifier().getDataType()
);
return DataTypeIdentifier.validComplementation(this.getClass(), target.getClass());
}
}
21 changes: 20 additions & 1 deletion src/main/java/no/digipost/api/datatypes/DataTypeIdentifier.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package no.digipost.api.datatypes;

import no.digipost.api.datatypes.types.*;
import no.digipost.api.datatypes.types.Appointment;
import no.digipost.api.datatypes.types.Boligdetaljer;
import no.digipost.api.datatypes.types.Category;
import no.digipost.api.datatypes.types.ExternalLink;
import no.digipost.api.datatypes.types.Payslip;
import no.digipost.api.datatypes.types.Residence;
import no.digipost.api.datatypes.types.SignedDocument;
import no.digipost.api.datatypes.types.pickup.PickupNotice;
import no.digipost.api.datatypes.types.pickup.PickupNoticeStatus;
import no.digipost.api.datatypes.types.receipt.Receipt;
import no.digipost.api.datatypes.validation.ComplementedBy;

import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -81,4 +88,16 @@ public DataType getExample() {
public static Set<Class<? extends DataType>> getAllClasses() {
return byType.keySet();
}

static boolean validComplementation(Class<? extends DataType> start, Class<? extends DataType> successor) {
ComplementedBy complementedBy = start.getAnnotation(ComplementedBy.class);
if (complementedBy != null) {
for (Class<?> c : complementedBy.value()) {
if (c == successor) {
return true;
}
}
}
return false;
}
}

0 comments on commit 1a0f387

Please sign in to comment.