001/*
002 * Copyright 2002-2020 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.core.env;
018
019import java.util.function.Predicate;
020
021/**
022 * Profile predicate that may be {@linkplain Environment#acceptsProfiles(Profiles)
023 * accepted} by an {@link Environment}.
024 *
025 * <p>May be implemented directly or, more usually, created using the
026 * {@link #of(String...) of(...)} factory method.
027 *
028 * @author Phillip Webb
029 * @author Sam Brannen
030 * @since 5.1
031 */
032@FunctionalInterface
033public interface Profiles {
034
035        /**
036         * Test if this {@code Profiles} instance <em>matches</em> against the given
037         * active profiles predicate.
038         * @param activeProfiles a predicate that tests whether a given profile is
039         * currently active
040         */
041        boolean matches(Predicate<String> activeProfiles);
042
043
044        /**
045         * Create a new {@link Profiles} instance that checks for matches against
046         * the given <em>profile strings</em>.
047         * <p>The returned instance will {@linkplain Profiles#matches(Predicate) match}
048         * if any one of the given profile strings matches.
049         * <p>A profile string may contain a simple profile name (for example
050         * {@code "production"}) or a profile expression. A profile expression allows
051         * for more complicated profile logic to be expressed, for example
052         * {@code "production & cloud"}.
053         * <p>The following operators are supported in profile expressions.
054         * <ul>
055         * <li>{@code !} - A logical <em>NOT</em> of the profile or profile expression</li>
056         * <li>{@code &} - A logical <em>AND</em> of the profiles or profile expressions</li>
057         * <li>{@code |} - A logical <em>OR</em> of the profiles or profile expressions</li>
058         * </ul>
059         * <p>Please note that the {@code &} and {@code |} operators may not be mixed
060         * without using parentheses. For example {@code "a & b | c"} is not a valid
061         * expression; it must be expressed as {@code "(a & b) | c"} or
062         * {@code "a & (b | c)"}.
063         * <p>As of Spring Framework 5.1.17, two {@code Profiles} instances returned
064         * by this method are considered equivalent to each other (in terms of
065         * {@code equals()} and {@code hashCode()} semantics) if they are created
066         * with identical <em>profile strings</em>.
067         * @param profiles the <em>profile strings</em> to include
068         * @return a new {@link Profiles} instance
069         */
070        static Profiles of(String... profiles) {
071                return ProfilesParser.parse(profiles);
072        }
073
074}