Skip to content

Commit

Permalink
feat(ContainerProvider): add PartitionContainerProvider (GitHub #122)
Browse files Browse the repository at this point in the history
  • Loading branch information
Createsequence committed Aug 2, 2023
1 parent 214b8f6 commit c43495a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cn.crane4j.core.container;

import lombok.Setter;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

/**
* <p>A {@link ContainerProvider} implementation for conveniently registering container.<br/>
* When get container by given namespace, it will return the container registered by {@link #registerContainer(Container)},
* if not exist, it will return the container created by {@link #defaultContainerFactory}.
*
* @author huangchengxing
*/
public class PartitionContainerProvider implements ContainerProvider {

/**
* Container map.
*/
private final Map<String, Container<Object>> containerMap = new HashMap<>();

/**
* Default container factory for non-existent container.
*/
@NonNull
@Setter
private Function<String, Container<Object>> defaultContainerFactory = namespace -> Containers.empty();

/**
* Get container comparator by given namespace
*
* @param namespace namespace of container
* @return container comparator
*/
@SuppressWarnings("unchecked")
@Override
public @Nullable <K> Container<K> getContainer(String namespace) {
return (Container<K>)containerMap.getOrDefault(namespace, defaultContainerFactory.apply(namespace));
}

/**
* Register container.
*
* @param container container
*/
public void registerContainer(@NonNull Container<Object> container) {
Objects.requireNonNull(container, "Container must not null");
containerMap.put(container.getNamespace(), container);
}

/**
* Whether this provider has container of given {@code namespace}.
*
* @param namespace namespace
* @return boolean
*/
@Override
public boolean containsContainer(String namespace) {
return Objects.nonNull(getContainer(namespace));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import cn.crane4j.core.util.ReflectUtils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -51,11 +53,13 @@ public abstract class AbstractAssembleAnnotationHandler<T extends Annotation> im

protected final Class<T> annotationType;
protected final AnnotationFinder annotationFinder;
protected final Comparator<KeyTriggerOperation> operationComparator;
@NonNull
@Setter
protected Comparator<KeyTriggerOperation> operationComparator;
protected final Crane4jGlobalConfiguration globalConfiguration;

/**
* Create an {@link AbstractAssembleAnnotationHandler} comparator.
* Create an {@link AbstractAssembleAnnotationHandler} instance.
*
* @param annotationType annotation type
* @param annotationFinder annotation finder
Expand All @@ -64,13 +68,25 @@ public abstract class AbstractAssembleAnnotationHandler<T extends Annotation> im
*/
protected AbstractAssembleAnnotationHandler(
Class<T> annotationType, AnnotationFinder annotationFinder,
Comparator<KeyTriggerOperation> operationComparator, Crane4jGlobalConfiguration globalConfiguration) {
@NonNull Comparator<KeyTriggerOperation> operationComparator, Crane4jGlobalConfiguration globalConfiguration) {
this.annotationType = annotationType;
this.annotationFinder = annotationFinder;
this.operationComparator = operationComparator;
this.globalConfiguration = globalConfiguration;
}

/**
* Create an {@link AbstractAssembleAnnotationHandler} instance.
*
* @param annotationType annotation type
* @param annotationFinder annotation finder
* @param globalConfiguration global configuration
*/
protected AbstractAssembleAnnotationHandler(
Class<T> annotationType, AnnotationFinder annotationFinder, Crane4jGlobalConfiguration globalConfiguration) {
this(annotationType, annotationFinder, Crane4jGlobalSorter.comparator(), globalConfiguration);
}

/**
* Resolve operations from type
*
Expand Down

0 comments on commit c43495a

Please sign in to comment.