001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.messaging.handler;
018
019import java.util.Collection;
020import java.util.StringJoiner;
021
022import org.springframework.lang.Nullable;
023
024/**
025 * Base class for {@code MessageCondition's} that pre-declares abstract methods
026 * {@link #getContent()} and {@link #getToStringInfix()} in order to provide
027 * implementations of {@link #equals(Object)}, {@link #hashCode()}, and
028 * {@link #toString()}.
029 *
030 * @author Rossen Stoyanchev
031 * @since 4.0
032 * @param <T> the kind of condition that this condition can be combined with or compared to
033 */
034public abstract class AbstractMessageCondition<T extends AbstractMessageCondition<T>> implements MessageCondition<T> {
035
036        @Override
037        public boolean equals(@Nullable Object other) {
038                if (this == other) {
039                        return true;
040                }
041                if (other == null || getClass() != other.getClass()) {
042                        return false;
043                }
044                return getContent().equals(((AbstractMessageCondition<?>) other).getContent());
045        }
046
047        @Override
048        public int hashCode() {
049                return getContent().hashCode();
050        }
051
052        @Override
053        public String toString() {
054                StringJoiner joiner = new StringJoiner(getToStringInfix(), "[", "]");
055                for (Object expression : getContent()) {
056                        joiner.add(expression.toString());
057                }
058                return joiner.toString();
059        }
060
061
062        /**
063         * Return the collection of objects the message condition is composed of
064         * (e.g. destination patterns), never {@code null}.
065         */
066        protected abstract Collection<?> getContent();
067
068        /**
069         * The notation to use when printing discrete items of content.
070         * For example " || " for URL patterns or " && " for param expressions.
071         */
072        protected abstract String getToStringInfix();
073
074}