-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ContainerProvider): add PartitionContainerProvider (GitHub #122)
- Loading branch information
1 parent
214b8f6
commit d90b41f
Showing
2 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
crane4j-core/src/main/java/cn/crane4j/core/container/PartitionContainerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters