Skip to content

Commit

Permalink
instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
portlek committed Sep 30, 2024
1 parent b5a34b1 commit 2398b7b
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 25 deletions.
2 changes: 2 additions & 0 deletions common/src/main/java/net/infumia/frame/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ static Frame create(

void register();

void register(@NotNull Consumer<TypedKeyStorageImmutableBuilder> instanceConfigurer);

void unregister();

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package net.infumia.frame.pipeline.context;

import java.util.Collection;
import java.util.function.Consumer;
import net.infumia.frame.Frame;
import net.infumia.frame.pipeline.PipelineContext;
import net.infumia.frame.typedkey.TypedKeyStorageImmutableBuilder;
import net.infumia.frame.view.View;
import org.jetbrains.annotations.NotNull;

Expand All @@ -18,6 +20,9 @@ interface ViewCreated extends PipelineContextManager {
interface ViewRegistered extends PipelineContextManager {
@NotNull
Collection<Object> registeredViews();

@NotNull
Consumer<TypedKeyStorageImmutableBuilder> instanceConfigurer();
}

interface ListenerRegistered extends PipelineContextManager {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import net.infumia.frame.pipeline.context.PipelineContextManager;
import net.infumia.frame.service.ConsumerService;
import net.infumia.frame.service.Implementation;
import net.infumia.frame.typedkey.TypedKeyStorageImmutableBuilder;
import net.infumia.frame.view.View;
import org.jetbrains.annotations.NotNull;

Expand All @@ -16,7 +18,8 @@ CompletableFuture<Collection<Object>> executeViewCreated(

@NotNull
CompletableFuture<Collection<View>> executeViewRegistered(
@NotNull Collection<Object> registeredViews
@NotNull Collection<Object> registeredViews,
@NotNull Consumer<TypedKeyStorageImmutableBuilder> instanceConfigurer
);

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ static TypedKeyStorageFactory create() {
@NotNull
TypedKeyStorage create(@NotNull Map<TypedKey<?>, Object> base);

@NotNull
TypedKeyStorageImmutableBuilder createImmutableBuilder();

@NotNull
TypedKeyStorageImmutableBuilder createImmutableBuilder(@NotNull Map<TypedKey<?>, Object> base);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.infumia.frame.typedkey;

import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull;

Expand All @@ -14,12 +13,6 @@ public TypedKeyStorage create(@NotNull final Map<TypedKey<?>, Object> base) {
return new TypedKeyStorageImpl(base);
}

@NotNull
@Override
public TypedKeyStorageImmutableBuilder createImmutableBuilder() {
return this.createImmutableBuilder(new HashMap<>());
}

@NotNull
@Override
public TypedKeyStorageImmutableBuilder createImmutableBuilder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ public interface TypedKeyStorageImmutable {
@NotNull
@UnmodifiableView
Collection<Map.Entry<TypedKey<?>, Object>> entries();

@NotNull
@UnmodifiableView
Map<TypedKey<?>, Object> map();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.infumia.frame.util.Preconditions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;

class TypedKeyStorageImmutableImpl implements TypedKeyStorageImmutable {

Expand Down Expand Up @@ -87,6 +88,13 @@ public Collection<Map.Entry<TypedKey<?>, Object>> entries() {
return Collections.unmodifiableCollection(this.map.entrySet());
}

@NotNull
@Override
@UnmodifiableView
public Map<TypedKey<?>, Object> map() {
return Collections.unmodifiableMap(this.map);
}

@Override
public String toString() {
return "TypedKeyStorageImmutableImpl{" + "map=" + this.map + '}';
Expand Down
18 changes: 13 additions & 5 deletions core/src/main/java/net/infumia/frame/FrameImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,19 @@ public void inventoryCreator(@NotNull final InventoryCreator inventoryCreator) {

@Override
public void register() {
this.register(builder -> {});
}

@Override
public void register(
@NotNull final Consumer<TypedKeyStorageImmutableBuilder> instanceConfigurer
) {
Preconditions.state(
!this.registered.get(),
"This inventory manager is already registered! #register() method cannot be called twice!"
);
this.registered.set(true);
this.executeViewCreation(this.unregisteredViews)
this.executeViewCreation(this.unregisteredViews, instanceConfigurer)
.thenCompose(views -> {
this.registeredViews.clear();
this.registeredViews.putAll(
Expand Down Expand Up @@ -210,7 +217,7 @@ public Frame with(@NotNull final Class<?> viewClass) {
return CompletableFuture.completedFuture(null);
}
final TypedKeyStorageImmutableBuilder builder =
this.storageFactory.createImmutableBuilder();
this.storageFactory.createImmutableBuilder(new HashMap<>());
initialDataConfigurer.accept(builder);
return CompletableFutureExtensions.logError(
((ViewEventHandler) view).simulateOpen(players, builder.build()),
Expand Down Expand Up @@ -278,10 +285,11 @@ private void intoUnregisteredViews(@NotNull final Class<?> viewClass) {

@NotNull
private CompletableFuture<Collection<View>> executeViewCreation(
@NotNull final Collection<Class<?>> views
@NotNull final Collection<Class<?>> views,
@NotNull final Consumer<TypedKeyStorageImmutableBuilder> instanceConfigurer
) {
return this.pipelines.executeViewCreated(views).thenCompose(
this.pipelines::executeViewRegistered
return this.pipelines.executeViewCreated(views).thenCompose(instances ->
this.pipelines.executeViewRegistered(instances, instanceConfigurer)
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.infumia.frame.pipeline.context;

import java.util.Collection;
import java.util.function.Consumer;
import net.infumia.frame.Frame;
import net.infumia.frame.typedkey.TypedKeyStorageImmutableBuilder;
import net.infumia.frame.view.View;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -36,13 +38,16 @@ final class ViewRegistered implements PipelineContextManager.ViewRegistered {

private final Frame manager;
private final Collection<Object> registeredViews;
private final Consumer<TypedKeyStorageImmutableBuilder> storageConfigurer;

public ViewRegistered(
@NotNull final Frame manager,
@NotNull final Collection<Object> registeredViews
@NotNull final Collection<Object> registeredViews,
@NotNull final Consumer<TypedKeyStorageImmutableBuilder> storageConfigurer
) {
this.manager = manager;
this.registeredViews = registeredViews;
this.storageConfigurer = storageConfigurer;
}

@NotNull
Expand All @@ -56,6 +61,12 @@ public Frame frame() {
public Collection<Object> registeredViews() {
return this.registeredViews;
}

@NotNull
@Override
public Consumer<TypedKeyStorageImmutableBuilder> instanceConfigurer() {
return this.storageConfigurer;
}
}

final class ListenerRegistered implements PipelineContextManager.ListenerRegistered {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import net.infumia.frame.Frame;
import net.infumia.frame.pipeline.context.PipelineContextManager;
import net.infumia.frame.pipeline.context.PipelineContextManagers;
import net.infumia.frame.pipeline.holder.PipelineHolderManager;
import net.infumia.frame.service.ConsumerService;
import net.infumia.frame.service.Implementation;
import net.infumia.frame.typedkey.TypedKeyStorageImmutableBuilder;
import net.infumia.frame.view.View;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -38,13 +40,15 @@ public CompletableFuture<Collection<Object>> executeViewCreated(
@NotNull
@Override
public CompletableFuture<Collection<View>> executeViewRegistered(
@NotNull final Collection<Object> registeredViews
@NotNull final Collection<Object> registeredViews,
@NotNull final Consumer<TypedKeyStorageImmutableBuilder> instanceConfigurer
) {
return this.pipelines.viewRegistered()
.completeWith(
new PipelineContextManagers.ViewRegistered(
this.manager,
Collections.unmodifiableCollection(registeredViews)
Collections.unmodifiableCollection(registeredViews),
instanceConfigurer
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import net.infumia.frame.Frame;
import net.infumia.frame.config.ViewConfigBuilderRich;
Expand All @@ -12,6 +12,8 @@
import net.infumia.frame.pipeline.PipelineService;
import net.infumia.frame.pipeline.context.PipelineContextManager;
import net.infumia.frame.state.StateRegistry;
import net.infumia.frame.typedkey.TypedKeyStorage;
import net.infumia.frame.typedkey.TypedKeyStorageImmutableBuilder;
import net.infumia.frame.view.View;
import net.infumia.frame.view.ViewEventHandler;
import net.infumia.frame.view.ViewImpl;
Expand Down Expand Up @@ -43,13 +45,16 @@ public CompletableFuture<Collection<View>> handle(
.registeredViews()
.stream()
.map(instance -> {
final TypedKeyStorageImmutableBuilder builder = frame
.storageFactory()
.createImmutableBuilder(new HashMap<>());
ctx.instanceConfigurer().accept(builder);
final TypedKeyStorage instances = frame
.storageFactory()
.create(builder.build().map());
final View view = new ViewImpl(
new ContextInitImpl(
new ContextImpl(
frame,
frame.storageFactory().create(new ConcurrentHashMap<>()),
new StateRegistry(frame.logger())
),
new ContextImpl(frame, instances, new StateRegistry(frame.logger())),
ViewConfigBuilderRich.create()
),
instance
Expand Down

0 comments on commit 2398b7b

Please sign in to comment.