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.web.servlet.mvc.condition;
018
019import java.util.Collection;
020import java.util.StringJoiner;
021
022import org.springframework.lang.Nullable;
023
024/**
025 * A base class for {@link RequestCondition} types providing implementations of
026 * {@link #equals(Object)}, {@link #hashCode()}, and {@link #toString()}.
027 *
028 * @author Rossen Stoyanchev
029 * @since 3.1
030 * @param <T> the type of objects that this RequestCondition can be combined
031 * with and compared to
032 */
033public abstract class AbstractRequestCondition<T extends AbstractRequestCondition<T>> implements RequestCondition<T> {
034
035        /**
036         * Indicates whether this condition is empty, i.e. whether or not it
037         * contains any discrete items.
038         * @return {@code true} if empty; {@code false} otherwise
039         */
040        public boolean isEmpty() {
041                return getContent().isEmpty();
042        }
043
044        /**
045         * Return the discrete items a request condition is composed of.
046         * <p>For example URL patterns, HTTP request methods, param expressions, etc.
047         * @return a collection of objects (never {@code null})
048         */
049        protected abstract Collection<?> getContent();
050
051        /**
052         * The notation to use when printing discrete items of content.
053         * <p>For example {@code " || "} for URL patterns or {@code " && "}
054         * for param expressions.
055         */
056        protected abstract String getToStringInfix();
057
058
059        @Override
060        public boolean equals(@Nullable Object other) {
061                if (this == other) {
062                        return true;
063                }
064                if (other == null || getClass() != other.getClass()) {
065                        return false;
066                }
067                return getContent().equals(((AbstractRequestCondition<?>) other).getContent());
068        }
069
070        @Override
071        public int hashCode() {
072                return getContent().hashCode();
073        }
074
075        @Override
076        public String toString() {
077                String infix = getToStringInfix();
078                StringJoiner joiner = new StringJoiner(infix, "[", "]");
079                for (Object expression : getContent()) {
080                        joiner.add(expression.toString());
081                }
082                return joiner.toString();
083        }
084
085}