001/*
002 * Copyright 2002-2015 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.Iterator;
021
022/**
023 * A base class for {@link MessageCondition} types providing implementations of
024 * {@link #equals(Object)}, {@link #hashCode()}, and {@link #toString()}.
025 *
026 * @author Rossen Stoyanchev
027 * @since 4.0
028 */
029public abstract class AbstractMessageCondition<T extends AbstractMessageCondition<T>> implements MessageCondition<T> {
030
031        @Override
032        public boolean equals(Object obj) {
033                if (this == obj) {
034                        return true;
035                }
036                if (obj != null && getClass() == obj.getClass()) {
037                        AbstractMessageCondition<?> other = (AbstractMessageCondition<?>) obj;
038                        return getContent().equals(other.getContent());
039                }
040                return false;
041        }
042
043        @Override
044        public int hashCode() {
045                return getContent().hashCode();
046        }
047
048        @Override
049        public String toString() {
050                StringBuilder builder = new StringBuilder("[");
051                for (Iterator<?> iterator = getContent().iterator(); iterator.hasNext();) {
052                        Object expression = iterator.next();
053                        builder.append(expression.toString());
054                        if (iterator.hasNext()) {
055                                builder.append(getToStringInfix());
056                        }
057                }
058                builder.append("]");
059                return builder.toString();
060        }
061
062
063        /**
064         * Return the collection of objects the message condition is composed of
065         * (e.g. destination patterns), never {@code null}.
066         */
067        protected abstract Collection<?> getContent();
068
069        /**
070         * The notation to use when printing discrete items of content.
071         * For example " || " for URL patterns or " && " for param expressions.
072         */
073        protected abstract String getToStringInfix();
074
075}