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