Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve issue with registration references #3094

Merged
merged 1 commit into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class DataProviderRegistrator {

protected final DataProviderRegistratorBuilder builder;

public DataProviderRegistrator(DataProviderRegistratorBuilder builder) {
public DataProviderRegistrator(final DataProviderRegistratorBuilder builder) {
this.builder = builder;
}

Expand All @@ -63,26 +63,6 @@ public <T> ImmutableRegistrator<T> asImmutable(final Class<T> target) {
return new ImmutableRegistrator<>(this.builder, target);
}

@SuppressWarnings({"unchecked", "UnstableApiUsage"})
protected static <T, K> Function<T, Optional<K>> toOptionalFunc(final Key<? extends Value<K>> key, final Function<T, K> getter) {
// Optimize boolean optionals
if (key.getElementToken().getRawType() == Boolean.class) {
return h -> (Optional<K>) OptBool.of((Boolean) getter.apply(h));
} else {
return h -> Optional.ofNullable(getter.apply(h));
}
}

@SuppressWarnings({"unchecked", "UnstableApiUsage"})
protected static <T, K> BiFunction<T, K, Optional<T>> toOptionalBiFunc(final Key<? extends Value<K>> key, final BiFunction<T, K, T> biFunc) {
// Optimize boolean optionals
if (key.getElementToken().getRawType() == Boolean.class) {
return (h, v) -> (Optional<T>) OptBool.of((Boolean) biFunc.apply(h, v));
} else {
return (h, v) -> Optional.ofNullable(biFunc.apply(h, v));
}
}

public static final class MutableRegistrator<T> extends DataProviderRegistrator {

private final Class<T> target;
Expand Down Expand Up @@ -114,10 +94,11 @@ public <K> MutableRegistration<K, T> create(final Key<? extends Value<K>> key) {
return registration;
}

@SuppressWarnings({"unchecked", "UnstableApiUsage"})
protected <K> MutableRegistrator<T> register(final MutableRegistration<K, T> registration) {
final Function<T, Optional<K>> optionalGetter = toOptionalFunc(registration.key, registration.get);
this.builder.register(
new GenericMutableDataProvider<T, K>(registration.key, this.target) {
final boolean isBooleanKey = registration.key.getElementToken().getRawType() == Boolean.class;

@Override
protected Value<K> constructValue(final T dataHolder, final K element) {
Expand All @@ -129,7 +110,10 @@ protected Value<K> constructValue(final T dataHolder, final K element) {

@Override
protected Optional<K> getFrom(final T dataHolder) {
return optionalGetter.apply(dataHolder);
if (this.isBooleanKey) {
return (Optional<K>) OptBool.of((Boolean) registration.get.apply(dataHolder));
}
return Optional.ofNullable(registration.get.apply(dataHolder));
}

@Override
Expand Down Expand Up @@ -213,11 +197,11 @@ public <K> ImmutableRegistration<K, T> create(final Key<? extends Value<K>> key)
return registration;
}

@SuppressWarnings({"unchecked", "UnstableApiUsage"})
protected <K> ImmutableRegistrator<T> register(final ImmutableRegistration<K, T> registration) {
final Function<T, Optional<K>> optionalGetter = toOptionalFunc(registration.key, registration.get);
final BiFunction<T, K, Optional<T>> optionalSetter = toOptionalBiFunc(registration.key, registration.set);
this.builder.register(
new GenericImmutableDataProvider<T, K>(registration.key) {
final boolean isBooleanKey = registration.key.getElementToken().getRawType() == Boolean.class;

@Override
protected Value<K> constructValue(final T dataHolder, final K element) {
Expand All @@ -229,12 +213,18 @@ protected Value<K> constructValue(final T dataHolder, final K element) {

@Override
protected Optional<K> getFrom(final T dataHolder) {
return optionalGetter.apply(dataHolder);
if (this.isBooleanKey) {
return (Optional<K>) OptBool.of((Boolean) registration.get.apply(dataHolder));
}
return Optional.ofNullable(registration.get.apply(dataHolder));
}

@Override
protected Optional<T> set(final T dataHolder, final K value) {
return optionalSetter.apply(dataHolder, value);
if (this.isBooleanKey) {
return (Optional<T>) OptBool.of((Boolean) registration.set.apply(dataHolder, value));
}
return Optional.ofNullable(registration.set.apply(dataHolder, value));
}

@Override
Expand Down Expand Up @@ -330,7 +320,9 @@ public <NK> MutableRegistration<NK, T> create(final Supplier<? extends Key<? ext
}

public <NK> MutableRegistration<NK, T> create(final Key<? extends Value<NK>> key) {
return new MutableRegistration<>(this.registrator, key);
final MutableRegistration<NK, T> registration = new MutableRegistration<>(this.registrator, key);
this.registrator.register(registration);
return registration;
}

/**
Expand Down Expand Up @@ -400,7 +392,9 @@ public <NK> ImmutableRegistration<NK, T> create(final Supplier<? extends Key<? e
}

public <NK> ImmutableRegistration<NK, T> create(final Key<? extends Value<NK>> key) {
return new ImmutableRegistration<>(this.registrator, key);
final ImmutableRegistration<NK, T> registration = new ImmutableRegistration<>(this.registrator, key);
this.registrator.register(registration);
return registration;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ private static <D> D getter(final Slot holder, final Supplier<Key<Value<D>>> sup
final Lens parentLens = ((InventoryBridge) holder.parent()).bridge$getAdapter().inventoryAdapter$getRootLens();
final Lens childLens = ((InventoryBridge) holder).bridge$getAdapter().inventoryAdapter$getRootLens();
final Map<Key<?>, Object> dataMap = parentLens.getDataFor(childLens);
return (D) dataMap.get(suppliedKey.get().getKey());
return (D) dataMap.get(suppliedKey.get());
}
}