Skip to content

Commit

Permalink
last minute refactoring and reformat. fabric8io#4350
Browse files Browse the repository at this point in the history
  • Loading branch information
xRodney committed Aug 26, 2022
1 parent 99390b5 commit 0d15b10
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 70 deletions.
Expand Up @@ -29,7 +29,6 @@
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

import static io.sundr.model.utils.Types.BOOLEAN_REF;
import static io.sundr.model.utils.Types.DOUBLE_REF;
Expand Down Expand Up @@ -122,58 +121,10 @@ public static String getSchemaTypeFor(TypeRef typeRef) {
protected T internalFrom(TypeDef definition, String... ignore) {
InternalSchemaSwaps schemaSwaps = new InternalSchemaSwaps();
T ret = internalFromImpl(definition, new HashSet<>(), schemaSwaps, ignore);
validateRemainingSchemaSwaps(schemaSwaps);
schemaSwaps.throwIfUnmatchedSwaps();
return ret;
}

private static class InternalSchemaSwap {
final ClassRef targetType;
final ClassRef originalType;
final String fieldName;

public InternalSchemaSwap(ClassRef originalType, String fieldName, ClassRef targetType) {
this.originalType = originalType;
this.fieldName = fieldName;
this.targetType = targetType;
}

public ClassRef getTargetType() {
return targetType;
}

public ClassRef getOriginalType() {
return originalType;
}

public String getFieldName() {
return fieldName;
}

@Override
public String toString() {
return "{" +
"targetType=" + targetType +
", originalType=" + originalType +
", fieldName='" + fieldName + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
InternalSchemaSwap that = (InternalSchemaSwap) o;
return targetType.equals(that.targetType) && originalType.equals(that.originalType) && fieldName.equals(that.fieldName);
}

@Override
public int hashCode() {
return Objects.hash(targetType, originalType, fieldName);
}
}

private static ClassRef extractClassRef(Object type) {
if (type != null) {
if (type instanceof ClassRef) {
Expand Down Expand Up @@ -225,15 +176,6 @@ private void extractSchemaSwap(ClassRef definitionType, Object annotation, Inter
}
}

private void validateRemainingSchemaSwaps(InternalSchemaSwaps schemaSwaps) {
String unmatchedSchemaSwaps = schemaSwaps.getUnusedSwaps()
.map(Object::toString)
.collect(Collectors.joining(","));
if (!unmatchedSchemaSwaps.isEmpty()) {
throw new IllegalArgumentException("Unmatched SchemaSwaps: " + unmatchedSchemaSwaps);
}
}

private T internalFromImpl(TypeDef definition, Set<String> visited, InternalSchemaSwaps schemaSwaps, String... ignore) {
final B builder = newBuilder();
Set<String> ignores = ignore.length > 0 ? new LinkedHashSet<>(Arrays.asList(ignore))
Expand Down
Expand Up @@ -22,22 +22,18 @@
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.stream.Stream;
import java.util.stream.Collectors;

public class InternalSchemaSwaps {
private final Map<Key, InternalSchemaSwap> swaps = new HashMap<>();
private final Map<Key, Value> swaps = new HashMap<>();

public void registerSwap(ClassRef definitionType, ClassRef originalType, String fieldName, ClassRef targetType) {
InternalSchemaSwap value = new InternalSchemaSwap(definitionType, originalType, fieldName, targetType);
Value value = new Value(definitionType, originalType, fieldName, targetType);
swaps.put(new Key(originalType, fieldName), value);
}

public Stream<InternalSchemaSwap> getUnusedSwaps() {
return swaps.values().stream().filter(value -> !value.used);
}

public Optional<ClassRef> lookupAndMark(ClassRef originalType, String name) {
InternalSchemaSwap value = swaps.get(new Key(originalType, name));
Value value = swaps.get(new Key(originalType, name));
if (value != null) {
value.markUsed();
return Optional.of(value.getTargetType());
Expand All @@ -46,6 +42,15 @@ public Optional<ClassRef> lookupAndMark(ClassRef originalType, String name) {
}
}

public void throwIfUnmatchedSwaps() {
String unmatchedSchemaSwaps = swaps.values().stream().filter(value -> !value.used)
.map(Object::toString)
.collect(Collectors.joining(", "));
if (!unmatchedSchemaSwaps.isEmpty()) {
throw new IllegalArgumentException("Unmatched SchemaSwaps: " + unmatchedSchemaSwaps);
}
}

private static final class Key {
private final ClassRef originalType;
private final String fieldName;
Expand Down Expand Up @@ -89,14 +94,14 @@ public String toString() {
}
}

public static class InternalSchemaSwap {
private static class Value {
private final ClassRef originalType;
private final String fieldName;
private final ClassRef targetType;
private boolean used;
private final ClassRef definitionType;

public InternalSchemaSwap(ClassRef definitionType, ClassRef originalType, String fieldName, ClassRef targetType) {
public Value(ClassRef definitionType, ClassRef originalType, String fieldName, ClassRef targetType) {
this.definitionType = definitionType;
this.originalType = originalType;
this.fieldName = fieldName;
Expand Down
Expand Up @@ -25,7 +25,7 @@
*
* @see SchemaFrom
*/
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE_USE, ElementType.TYPE})
@Target({ ElementType.ANNOTATION_TYPE, ElementType.TYPE_USE, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(SchemaSwaps.class)
public @interface SchemaSwap {
Expand Down

0 comments on commit 0d15b10

Please sign in to comment.