All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.data.relational.core.conversion.DefaultRootAggregateChange Maven / Gradle / Ivy

There is a newer version: 3.3.2
Show newest version
/*
 * Copyright 2022-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.data.relational.core.conversion;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
 * Represents the change happening to the aggregate (as used in the context of Domain Driven Design) as a whole.
 *
 * @author Chirag Tailor
 * @since 3.0
 */
class DefaultRootAggregateChange implements RootAggregateChange {

	private final Kind kind;

	/** Type of the aggregate root to be changed */
	private final Class entityType;

	private final List> actions = new ArrayList<>();

	private DbAction.WithRoot rootAction;

	/** The previous version assigned to the instance being changed, if available */
	@Nullable private final Number previousVersion;

	DefaultRootAggregateChange(Kind kind, Class entityType, @Nullable Number previousVersion) {

		this.kind = kind;
		this.entityType = entityType;
		this.previousVersion = previousVersion;
	}

	/**
	 * Adds an action to this {@code AggregateChange}.
	 *
	 * @param action must not be {@literal null}.
	 */
	@Override
	public void addAction(DbAction action) {

		Assert.notNull(action, "Action must not be null");

		actions.add(action);
	}

	@Override
	public Kind getKind() {
		return this.kind;
	}

	@Override
	public Class getEntityType() {
		return this.entityType;
	}

	@Override
	public void setRoot(T aggregateRoot) {

		Assert.isInstanceOf(this.entityType, aggregateRoot,
				String.format("AggregateRoot must be of type %s", entityType.getName()));

		rootAction.setEntity(aggregateRoot);
	}

	@Override
	public void setRootAction(DbAction.WithRoot action) {

		Assert.isNull(this.rootAction, "The rootAction must be set exactly once");

		this.rootAction = action;
	}

	@Nullable
	@Override
	public Number getPreviousVersion() {
		return previousVersion;
	}

	@Override
	public T getRoot() {
		return this.rootAction.getEntity();
	}

	@Override
	public void forEachAction(Consumer> consumer) {

		Assert.notNull(consumer, "Consumer must not be null");
		Assert.notNull(rootAction, "DbAction.WithRoot must not be null");

		consumer.accept(rootAction);
		actions.forEach(consumer);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy