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

org.springframework.amqp.core.AbstractDeclarable Maven / Gradle / Ivy

There is a newer version: 3.1.6
Show newest version
/*
 * Copyright 2002-2019 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.amqp.core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

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

/**
 * Base class for {@link Declarable} classes.
 *
 * @author Gary Russell
 * @since 1.2
 *
 */
public abstract class AbstractDeclarable implements Declarable {

	private boolean shouldDeclare = true;

	private Collection declaringAdmins = new ArrayList();

	private boolean ignoreDeclarationExceptions;

	private final Map arguments;

	public AbstractDeclarable() {
		this(null);
	}

	/**
	 * Construct an instance with the supplied arguments, or an empty map if null.
	 * @param arguments the arguments.
	 * @since 2.2.2
	 */
	public AbstractDeclarable(@Nullable Map arguments) {
		if (arguments != null) {
			this.arguments = new HashMap<>(arguments);
		}
		else {
			this.arguments = new HashMap();
		}
	}

	@Override
	public boolean shouldDeclare() {
		return this.shouldDeclare;
	}

	/**
	 * Whether or not this object should be automatically declared
	 * by any {@code AmqpAdmin}. Default is {@code true}.
	 * @param shouldDeclare true or false.
	 */
	public void setShouldDeclare(boolean shouldDeclare) {
		this.shouldDeclare = shouldDeclare;
	}

	@Override
	public Collection getDeclaringAdmins() {
		return Collections.unmodifiableCollection(this.declaringAdmins);
	}

	@Override
	public boolean isIgnoreDeclarationExceptions() {
		return this.ignoreDeclarationExceptions;
	}

	/**
	 * Set to true to ignore exceptions such as mismatched properties when declaring.
	 * @param ignoreDeclarationExceptions the ignoreDeclarationExceptions.
	 * @since 1.6
	 */
	public void setIgnoreDeclarationExceptions(boolean ignoreDeclarationExceptions) {
		this.ignoreDeclarationExceptions = ignoreDeclarationExceptions;
	}

	@Override
	public void setAdminsThatShouldDeclare(Object... adminArgs) {
		Collection admins = new ArrayList();
		if (adminArgs != null) {
			if (adminArgs.length > 1) {
				Assert.noNullElements(adminArgs, "'admins' cannot contain null elements");
			}
			if (adminArgs.length > 0 && !(adminArgs.length == 1 && adminArgs[0] == null)) {
				admins.addAll(Arrays.asList(adminArgs));
			}
		}
		this.declaringAdmins = admins;
	}

	@Override
	public synchronized void addArgument(String argName, Object argValue) {
		this.arguments.put(argName, argValue);
	}

	@Override
	public synchronized Object removeArgument(String name) {
		return this.arguments.remove(name);
	}

	public Map getArguments() {
		return this.arguments;
	}

}