Skip to content

Commit

Permalink
Simplify all assert calls by using the BuilderUtils.assert... functions
Browse files Browse the repository at this point in the history
Simplify all assert calls by using the BuilderUtils.assert... functions

#754
  • Loading branch information
smcvb committed Sep 10, 2018
1 parent 694d532 commit 684e6e5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
import org.axonframework.monitoring.MessageMonitor;
import org.axonframework.monitoring.NoOpMessageMonitor;

import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static org.axonframework.common.Assert.assertThat;
import static org.axonframework.common.BuilderUtils.assertNonNull;

/**
* Specialization of the SimpleCommandBus that processed Commands asynchronously from the calling thread. By default,
Expand Down Expand Up @@ -143,9 +142,7 @@ public Builder rollbackConfiguration(RollbackConfiguration rollbackConfiguration
* @return the current Builder instance, for a fluent interfacing
*/
public Builder executor(Executor executor) {
assertThat(executor,
Objects::nonNull,
() -> new AxonConfigurationException("Executor may not be null"));
assertNonNull(executor, "Executor may not be null");
this.executor = executor;
return this;
}
Expand All @@ -160,9 +157,7 @@ public AsynchronousCommandBus build() {
}

private void validate() {
assertThat(executor,
Objects::nonNull,
() -> new AxonConfigurationException("The Executor is a hard requirement and should be provided"));
assertNonNull(executor, "The Executor is a hard requirement and should be provided");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;

import static java.lang.String.format;
import static org.axonframework.common.Assert.assertThat;
import static org.axonframework.common.BuilderUtils.assertNonNull;

/**
* Implementation of the CommandBus that dispatches commands to the handlers subscribed to that specific command's name.
Expand Down Expand Up @@ -252,9 +251,7 @@ public static class Builder {
* @return the current Builder instance, for a fluent interfacing
*/
public Builder transactionManager(TransactionManager transactionManager) {
assertThat(transactionManager,
Objects::nonNull,
() -> new AxonConfigurationException("TransactionManager may not be null"));
assertNonNull(transactionManager, "TransactionManager may not be null");
this.transactionManager = transactionManager;
return this;
}
Expand All @@ -267,9 +264,7 @@ public Builder transactionManager(TransactionManager transactionManager) {
* @return the current Builder instance, for a fluent interfacing
*/
public Builder messageMonitor(MessageMonitor<? super CommandMessage<?>> messageMonitor) {
assertThat(messageMonitor,
Objects::nonNull,
() -> new AxonConfigurationException("MessageMonitor may not be null"));
assertNonNull(messageMonitor, "MessageMonitor may not be null");
this.messageMonitor = messageMonitor;
return this;
}
Expand All @@ -284,9 +279,7 @@ public Builder messageMonitor(MessageMonitor<? super CommandMessage<?>> messageM
* @return the current Builder instance, for a fluent interfacing
*/
public Builder rollbackConfiguration(RollbackConfiguration rollbackConfiguration) {
assertThat(rollbackConfiguration,
Objects::nonNull,
() -> new AxonConfigurationException("RollbackConfiguration may not be null"));
assertNonNull(rollbackConfiguration, "RollbackConfiguration may not be null");
this.rollbackConfiguration = rollbackConfiguration;
return this;
}
Expand All @@ -301,18 +294,10 @@ public SimpleCommandBus build() {
}

private void validate() {
assertThat(transactionManager,
Objects::nonNull,
() -> new AxonConfigurationException(
"The TransactionManager is a hard requirement and should be provided"));
assertThat(messageMonitor,
Objects::nonNull,
() -> new AxonConfigurationException(
"The MessageMonitor is a hard requirement and should be provided"));
assertThat(rollbackConfiguration,
Objects::nonNull,
() -> new AxonConfigurationException(
"The RollbackConfiguration is a hard requirement and should be provided"));
assertNonNull(transactionManager, "The TransactionManager is a hard requirement and should be provided");
assertNonNull(messageMonitor, "The MessageMonitor is a hard requirement and should be provided");
assertNonNull(rollbackConfiguration,
"The RollbackConfiguration is a hard requirement and should be provided");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
import java.util.concurrent.Executors;

import static java.lang.String.format;
import static org.axonframework.common.Assert.assertThat;
import static org.axonframework.common.BuilderUtils.assertNonNull;
import static org.axonframework.common.BuilderUtils.assertThat;

/**
* Asynchronous CommandBus implementation with very high performance characteristics. It divides the command handling
Expand Down Expand Up @@ -854,9 +855,7 @@ public Builder rescheduleCommandsOnCorruptState(boolean rescheduleCommandsOnCorr
* @return the current Builder instance, for a fluent interfacing
*/
public Builder coolingDownPeriod(long coolingDownPeriod) {
assertThat(coolingDownPeriod,
count -> count > 0,
() -> new AxonConfigurationException("The cooling down period must be a positive number"));
assertCoolingDownPeriod(coolingDownPeriod);
this.coolingDownPeriod = coolingDownPeriod;
return this;
}
Expand All @@ -873,9 +872,7 @@ public Builder coolingDownPeriod(long coolingDownPeriod) {
* @return the current Builder instance, for a fluent interfacing
*/
public Builder commandTargetResolver(CommandTargetResolver commandTargetResolver) {
assertThat(commandTargetResolver,
Objects::nonNull,
() -> new AxonConfigurationException("CommandTargetResolver may not be null"));
assertNonNull(commandTargetResolver, "CommandTargetResolver may not be null");
this.commandTargetResolver = commandTargetResolver;
return this;
}
Expand All @@ -891,9 +888,7 @@ public Builder commandTargetResolver(CommandTargetResolver commandTargetResolver
* @return the current Builder instance, for a fluent interfacing
*/
public Builder publisherThreadCount(int publisherThreadCount) {
assertThat(publisherThreadCount,
count -> count > 0,
() -> new AxonConfigurationException("The publisher thread count must at least be 1"));
assertPublisherThreadCount(publisherThreadCount);
this.publisherThreadCount = publisherThreadCount;
return this;
}
Expand All @@ -906,9 +901,7 @@ public Builder publisherThreadCount(int publisherThreadCount) {
* @return the current Builder instance, for a fluent interfacing
*/
public Builder messageMonitor(MessageMonitor<? super CommandMessage<?>> messageMonitor) {
assertThat(messageMonitor,
Objects::nonNull,
() -> new AxonConfigurationException("MessageMonitor may not be null"));
assertNonNull(messageMonitor, "MessageMonitor may not be null");
this.messageMonitor = messageMonitor;
return this;
}
Expand Down Expand Up @@ -936,9 +929,7 @@ public Builder transactionManager(TransactionManager transactionManager) {
* @return the current Builder instance, for a fluent interfacing
*/
public Builder rollbackConfiguration(RollbackConfiguration rollbackConfiguration) {
assertThat(rollbackConfiguration,
Objects::nonNull,
() -> new AxonConfigurationException("RollbackConfiguration may not be null"));
assertNonNull(rollbackConfiguration, "RollbackConfiguration may not be null");
this.rollbackConfiguration = rollbackConfiguration;
return this;
}
Expand All @@ -952,9 +943,7 @@ public Builder rollbackConfiguration(RollbackConfiguration rollbackConfiguration
* @return the current Builder instance, for a fluent interfacing
*/
public Builder bufferSize(int bufferSize) {
assertThat(bufferSize,
size -> size > 0 && size % 2 == 0,
() -> new AxonConfigurationException("The buffer size must be positive and a power of 2"));
assertBufferSize(bufferSize);
this.bufferSize = bufferSize;
return this;
}
Expand All @@ -968,9 +957,7 @@ public Builder bufferSize(int bufferSize) {
* @return the current Builder instance, for a fluent interfacing
*/
public Builder producerType(ProducerType producerType) {
assertThat(producerType,
Objects::nonNull,
() -> new AxonConfigurationException("ProducerType may not be null"));
assertNonNull(producerType, "ProducerType may not be null");
this.producerType = producerType;
return this;
}
Expand All @@ -995,9 +982,7 @@ public Builder producerType(ProducerType producerType) {
* @see com.lmax.disruptor.YieldingWaitStrategy YieldingWaitStrategy
*/
public Builder waitStrategy(WaitStrategy waitStrategy) {
assertThat(waitStrategy,
Objects::nonNull,
() -> new AxonConfigurationException("WaitStrategy may not be null"));
assertNonNull(waitStrategy, "WaitStrategy may not be null");
this.waitStrategy = waitStrategy;
return this;
}
Expand All @@ -1014,9 +999,7 @@ public Builder waitStrategy(WaitStrategy waitStrategy) {
* @return the current Builder instance, for a fluent interfacing
*/
public Builder invokerThreadCount(int invokerThreadCount) {
assertThat(invokerThreadCount,
count -> count > 0,
() -> new AxonConfigurationException("The invoker thread count must be at least 1"));
assertInvokerThreadCount(invokerThreadCount);
this.invokerThreadCount = invokerThreadCount;
return this;
}
Expand All @@ -1032,9 +1015,7 @@ public Builder invokerThreadCount(int invokerThreadCount) {
* @return the current Builder instance, for a fluent interfacing
*/
public Builder cache(Cache cache) {
assertThat(cache,
Objects::nonNull,
() -> new AxonConfigurationException("Cache may not be null"));
assertNonNull(cache, "Cache may not be null");
this.cache = cache;
return this;
}
Expand All @@ -1049,41 +1030,35 @@ public DisruptorCommandBus build() {
}

private void validate() {
assertThat(coolingDownPeriod,
count -> count > 0,
() -> new AxonConfigurationException("The cooling down period must be a positive number"));
assertThat(commandTargetResolver,
Objects::nonNull,
() -> new AxonConfigurationException(
"The CommandTargetResolver is a hard requirement and should be provided"));
assertThat(publisherThreadCount,
count -> count > 0,
() -> new AxonConfigurationException("The publisher thread count must at least be 1"));
assertThat(messageMonitor,
Objects::nonNull,
() -> new AxonConfigurationException(
"The MessageMonitor is a hard requirement and should be provided"));
assertThat(rollbackConfiguration,
Objects::nonNull,
() -> new AxonConfigurationException(
"The RollbackConfiguration is a hard requirement and should be provided"));
assertThat(bufferSize,
size -> size > 0 && size % 2 == 0,
() -> new AxonConfigurationException("The buffer size must be positive and a power of 2"));
assertThat(producerType,
Objects::nonNull,
() -> new AxonConfigurationException(
"The ProducerType is a hard requirement and should be provided"));
assertThat(waitStrategy,
Objects::nonNull,
() -> new AxonConfigurationException(
"The WaitStrategy is a hard requirement and should be provided"));
assertThat(invokerThreadCount,
count -> count > 0,
() -> new AxonConfigurationException("The invoker thread count must be at least 1"));
assertThat(cache,
Objects::nonNull,
() -> new AxonConfigurationException("The Cache is a hard requirement and should be provided"));
assertCoolingDownPeriod(coolingDownPeriod);
assertNonNull(commandTargetResolver,
"The CommandTargetResolver is a hard requirement and should be provided");
assertPublisherThreadCount(publisherThreadCount);
assertNonNull(messageMonitor, "The MessageMonitor is a hard requirement and should be provided");
assertNonNull(rollbackConfiguration,
"The RollbackConfiguration is a hard requirement and should be provided");
assertBufferSize(bufferSize);
assertNonNull(producerType, "The ProducerType is a hard requirement and should be provided");
assertNonNull(waitStrategy, "The WaitStrategy is a hard requirement and should be provided");
assertInvokerThreadCount(invokerThreadCount);
assertNonNull(cache, "The Cache is a hard requirement and should be provided");
}

private void assertCoolingDownPeriod(long coolingDownPeriod) {
assertThat(coolingDownPeriod, count -> count > 0, "The cooling down period must be a positive number");
}

private void assertBufferSize(int bufferSize) {
assertThat(bufferSize, size -> size > 0 && size % 2 == 0,
"The buffer size must be positive and a power of 2");
}

private void assertPublisherThreadCount(int publisherThreadCount) {
assertThat(publisherThreadCount, count -> count > 0, "The publisher thread count must at least be 1");
}

private void assertInvokerThreadCount(int invokerThreadCount) {
assertThat(invokerThreadCount, count -> count > 0, "The invoker thread count must be at least 1");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@
import org.axonframework.monitoring.NoOpMessageMonitor;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;

import static java.lang.String.format;
import static org.axonframework.common.Assert.assertThat;
import static org.axonframework.common.BuilderUtils.assertNonNull;

/**
* Implementation of a {@link CommandBus} that is aware of multiple instances of a CommandBus working together to
Expand Down Expand Up @@ -233,9 +232,7 @@ public static class Builder {
* @return the current Builder instance, for a fluent interfacing
*/
public Builder commandRouter(CommandRouter commandRouter) {
assertThat(commandRouter,
Objects::nonNull,
() -> new AxonConfigurationException("CommandRouter may not be null"));
assertNonNull(commandRouter, "CommandRouter may not be null");
this.commandRouter = commandRouter;
return this;
}
Expand All @@ -249,9 +246,7 @@ public Builder commandRouter(CommandRouter commandRouter) {
* @return the current Builder instance, for a fluent interfacing
*/
public Builder connector(CommandBusConnector connector) {
assertThat(connector,
Objects::nonNull,
() -> new AxonConfigurationException("CommandBusConnector may not be null"));
assertNonNull(connector, "CommandBusConnector may not be null");
this.connector = connector;
return this;
}
Expand All @@ -264,9 +259,7 @@ public Builder connector(CommandBusConnector connector) {
* @return the current Builder instance, for a fluent interfacing
*/
public Builder messageMonitor(MessageMonitor<? super CommandMessage<?>> messageMonitor) {
assertThat(messageMonitor,
Objects::nonNull,
() -> new AxonConfigurationException("MessageMonitor may not be null"));
assertNonNull(messageMonitor, "MessageMonitor may not be null");
this.messageMonitor = messageMonitor;
return this;
}
Expand All @@ -281,18 +274,9 @@ public DistributedCommandBus build() {
}

private void validate() {
assertThat(commandRouter,
Objects::nonNull,
() -> new AxonConfigurationException(
"The CommandRouter is a hard requirement and should be provided"));
assertThat(connector,
Objects::nonNull,
() -> new AxonConfigurationException(
"The CommandBusConnector is a hard requirement and should be provided"));
assertThat(messageMonitor,
Objects::nonNull,
() -> new AxonConfigurationException(
"The MessageMonitor is a hard requirement and should be provided"));
assertNonNull(commandRouter, "The CommandRouter is a hard requirement and should be provided");
assertNonNull(connector, "The CommandBusConnector is a hard requirement and should be provided");
assertNonNull(messageMonitor, "The MessageMonitor is a hard requirement and should be provided");
}
}
}
Loading

0 comments on commit 684e6e5

Please sign in to comment.