Skip to content

Commit

Permalink
deep clone. (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
portlek authored Oct 6, 2024
1 parent e236aa4 commit 82747ac
Show file tree
Hide file tree
Showing 103 changed files with 226 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public final class InjectionServiceInjector<C> implements InjectionService<C> {
this.registry = registry;
}

@NotNull
@Override
public String key() {
return InjectionServiceInjector.KEY;
Expand Down
67 changes: 3 additions & 64 deletions common/src/main/java/net/infumia/frame/pipeline/PipelineBase.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package net.infumia.frame.pipeline;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import net.infumia.frame.service.Implementation;
import net.infumia.frame.service.Service;
import net.infumia.frame.util.Cloned;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface PipelineBase<B extends PipelineContext, R, Self extends PipelineBase<B, R, Self>> {
public interface PipelineBase<B extends PipelineContext, R, Self extends PipelineBase<B, R, Self>>
extends Cloned<Self> {
@NotNull
Self apply(@NotNull Implementation<B, R> operation);

Expand All @@ -23,63 +21,4 @@ public interface PipelineBase<B extends PipelineContext, R, Self extends Pipelin
default Self register(@NotNull final Service<B, R> service) {
return this.apply(Implementation.register(service));
}

@NotNull
default Self register(
@NotNull final Service<B, R> service,
@Nullable final Collection<Predicate<B>> filters
) {
return this.apply(Implementation.register(service, filters));
}

@NotNull
default Self replace(
@NotNull final String serviceKey,
@NotNull final UnaryOperator<Service<B, R>> operation
) {
return this.apply(Implementation.replace(serviceKey, operation));
}

@NotNull
default Self replace(
@NotNull final String serviceKey,
@NotNull final UnaryOperator<Service<B, R>> operation,
@Nullable final Collection<Predicate<B>> filters
) {
return this.apply(Implementation.replace(serviceKey, operation, filters));
}

@NotNull
default Self registerAfter(
@NotNull final String serviceKey,
@NotNull final Service<B, R> service
) {
return this.apply(Implementation.registerAfter(serviceKey, service));
}

@NotNull
default Self registerAfter(
@NotNull final String serviceKey,
@NotNull final Service<B, R> service,
@Nullable final Collection<Predicate<B>> filters
) {
return this.apply(Implementation.registerAfter(serviceKey, service, filters));
}

@NotNull
default Self registerBefore(
@NotNull final String serviceKey,
@NotNull final Service<B, R> service
) {
return this.apply(Implementation.registerBefore(serviceKey, service));
}

@NotNull
default Self registerBefore(
@NotNull final String serviceKey,
@NotNull final Service<B, R> service,
@Nullable final Collection<Predicate<B>> filters
) {
return this.apply(Implementation.registerBefore(serviceKey, service, filters));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
import org.jetbrains.annotations.Nullable;

public interface Implementation<Context, Result> {
@NotNull
static <Context, Result> Implementation<Context, Result> remove(
@NotNull final String serviceKey
) {
return new Remove<>(serviceKey);
}

@NotNull
static <Context, Result> Implementation<Context, Result> register(
@NotNull final Service<Context, Result> service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import net.infumia.frame.util.Keyed;
import net.infumia.frame.util.Preconditions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -50,7 +49,7 @@ public void handle(@NotNull final ServiceRepository<Context, Result> repository)
implementations
.stream()
.map(wrapper -> wrapper.implementation)
.map(Keyed::key)
.map(Service::key)
.collect(Collectors.toSet())
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.ListIterator;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -42,6 +43,10 @@ public void handle(@NotNull final ServiceRepository<Context, Result> repository)
"Service '%s' not found in the implementation list '%s'!",
this.serviceKey,
implementations
.stream()
.map(wrapper -> wrapper.implementation)
.map(Service::key)
.collect(Collectors.toSet())
)
);
}
Expand Down
19 changes: 19 additions & 0 deletions common/src/main/java/net/infumia/frame/service/Remove.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.infumia.frame.service;

import org.jetbrains.annotations.NotNull;

final class Remove<Context, Result> implements Implementation<Context, Result> {

private final String serviceKey;

Remove(@NotNull final String serviceKey) {
this.serviceKey = serviceKey;
}

@Override
public void handle(@NotNull final ServiceRepository<Context, Result> repository) {
repository.implementations.removeIf(wrapper ->
wrapper.implementation.key().equals(this.serviceKey)
);
}
}
3 changes: 1 addition & 2 deletions common/src/main/java/net/infumia/frame/service/Replace.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import net.infumia.frame.util.Keyed;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -52,7 +51,7 @@ public void handle(@NotNull final ServiceRepository<Context, Result> repository)
implementations
.stream()
.map(wrapper -> wrapper.implementation)
.map(Keyed::key)
.map(Service::key)
.collect(Collectors.toSet())
)
);
Expand Down
6 changes: 4 additions & 2 deletions common/src/main/java/net/infumia/frame/service/Service.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.infumia.frame.service;

import java.util.concurrent.CompletableFuture;
import net.infumia.frame.util.Keyed;
import org.jetbrains.annotations.NotNull;

public interface Service<Context, Result> extends Keyed<String> {
public interface Service<Context, Result> {
@NotNull
CompletableFuture<Result> handle(Context context);

@NotNull
String key();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import net.infumia.frame.util.Cloned;
import org.jetbrains.annotations.NotNull;

public final class ServiceRepository<Context, Result> {
public final class ServiceRepository<Context, Result>
implements Cloned<ServiceRepository<Context, Result>> {

private final ServicePipeline pipeline;
private final Service<Context, Result> defaultImplementation;
final List<ServiceWrapper<Context, Result>> implementations;
final TypeToken<? extends Service<Context, Result>> serviceType;

Expand All @@ -20,12 +23,19 @@ public ServiceRepository(
) {
this.pipeline = pipeline;
this.serviceType = serviceType;
this.defaultImplementation = defaultImplementation;
this.implementations = new LinkedList<>();
this.implementations.add(
new ServiceWrapper<>(serviceType, defaultImplementation, true, null)
);
}

@NotNull
@Override
public ServiceRepository<Context, Result> cloned() {
return new ServiceRepository<>(this.pipeline, this.serviceType, this.defaultImplementation);
}

public void apply(@NotNull final Implementation<Context, Result> operation) {
synchronized (this.implementations) {
operation.handle(this);
Expand Down
8 changes: 8 additions & 0 deletions common/src/main/java/net/infumia/frame/util/Cloned.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.infumia.frame.util;

import org.jetbrains.annotations.NotNull;

public interface Cloned<S extends Cloned<S>> {
@NotNull
S cloned();
}
6 changes: 3 additions & 3 deletions core/src/main/java/net/infumia/frame/FrameImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void register(
) {
Preconditions.state(
!this.registered.get(),
"This inventory manager is already registered! #register() method cannot be called twice!"
"This frame is already registered! #register() method cannot be called twice!"
);
this.registered.set(true);
this.executeViewCreation(this.unregisteredViews, instanceConfigurer)
Expand Down Expand Up @@ -172,7 +172,7 @@ public void unregister() {
@NotNull
@Override
public Frame with(@NotNull final Class<?> viewClass) {
Preconditions.argument(!this.registered.get(), "This inventory manager is registered!");
Preconditions.argument(!this.registered.get(), "This frame is registered!");
this.intoUnregisteredViews(viewClass);
return this;
}
Expand Down Expand Up @@ -264,7 +264,7 @@ public PipelineExecutorFrame pipelines() {
private View findView(@NotNull final Class<?> viewClass) {
Preconditions.state(
this.registered.get(),
"Before you open a view you must register this inventory manager!"
"Before you open a view you must register this frame!"
);
return Preconditions.argumentNotNull(
this.registeredViews.get(viewClass),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ public final class PipelineConsumerImpl<B extends PipelineContext> implements Pi

private final ServiceRepository<B, ConsumerService.State> repository;

private PipelineConsumerImpl(
@NotNull final ServiceRepository<B, ConsumerService.State> repository
) {
this.repository = repository;
}

public PipelineConsumerImpl(
@NotNull final TypeToken<PipelineServiceConsumer<B>> type,
@NotNull final PipelineServiceConsumer<B> defaultService
) {
this.repository = ServicePipelineBuilder.newBuilder().build().create(type, defaultService);
this(ServicePipelineBuilder.newBuilder().build().create(type, defaultService));
}

@NotNull
Expand All @@ -39,4 +45,10 @@ public CompletableFuture<ConsumerService.State> completeWith(@NotNull final B co
public CompletableFuture<ConsumerService.State> completeWithAsync(@NotNull final B context) {
return this.repository.completeWithAsync(context);
}

@NotNull
@Override
public PipelineConsumer<B> cloned() {
return new PipelineConsumerImpl<>(this.repository.cloned());
}
}
12 changes: 11 additions & 1 deletion core/src/main/java/net/infumia/frame/pipeline/PipelineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ public class PipelineImpl<B extends PipelineContext, R> implements Pipeline<B, R

private final ServiceRepository<B, R> repository;

private PipelineImpl(@NotNull final ServiceRepository<B, R> repository) {
this.repository = repository;
}

public PipelineImpl(
@NotNull final TypeToken<PipelineService<B, R>> type,
@NotNull final PipelineService<B, R> defaultService
) {
this.repository = ServicePipelineBuilder.newBuilder().build().create(type, defaultService);
this(ServicePipelineBuilder.newBuilder().build().create(type, defaultService));
}

@NotNull
Expand All @@ -36,4 +40,10 @@ public CompletableFuture<R> completeWith(@NotNull final B context) {
public CompletableFuture<R> completeWithAsync(@NotNull final B context) {
return this.repository.completeWithAsync(context);
}

@NotNull
@Override
public Pipeline<B, R> cloned() {
return new PipelineImpl<>(this.repository.cloned());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

public final class PipelineExecutorElementImpl implements PipelineExecutorElement {

private final PipelineHolderElement pipelines = PipelineHolderElement.BASE.createNew();
private final PipelineHolderElement pipelines = PipelineHolderElement.BASE.cloned();
private final ElementRich element;

public PipelineExecutorElementImpl(@NotNull final ElementRich element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public final class PipelineExecutorFrameImpl implements PipelineExecutorFrame {

private final PipelineHolderFrame pipelines = PipelineHolderFrame.BASE.createNew();
private final PipelineHolderFrame pipelines = PipelineHolderFrame.BASE.cloned();
private final Frame frame;

public PipelineExecutorFrameImpl(@NotNull final Frame frame) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public final class PipelineExecutorRenderImpl implements PipelineExecutorRender {

private final PipelineHolderRender pipelines = PipelineHolderRender.BASE.createNew();
private final PipelineHolderRender pipelines = PipelineHolderRender.BASE.cloned();
private final ContextRender context;

public PipelineExecutorRenderImpl(@NotNull final ContextRender context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public final class PipelineExecutorStateImpl implements PipelineExecutorState {

private final PipelineHolderState pipelines = PipelineHolderState.BASE.createNew();
private final PipelineHolderState pipelines = PipelineHolderState.BASE.cloned();
private final ContextBase context;

public PipelineExecutorStateImpl(@NotNull final ContextBase context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public final class PipelineExecutorViewImpl implements PipelineExecutorView {

private final PipelineHolderView pipelines = PipelineHolderView.BASE.createNew();
private final PipelineHolderView pipelines = PipelineHolderView.BASE.cloned();
private final View view;

public PipelineExecutorViewImpl(@NotNull final View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public final class PipelineExecutorViewerImpl implements PipelineExecutorViewer {

private final PipelineHolderViewer pipelines = PipelineHolderViewer.BASE.createNew();
private final PipelineHolderViewer pipelines = PipelineHolderViewer.BASE.cloned();
private final ContextRender context;

public PipelineExecutorViewerImpl(@NotNull final ContextRender context) {
Expand Down
Loading

0 comments on commit 82747ac

Please sign in to comment.