001/*
002 * Copyright 2012-2017 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 *      http://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.boot.context.properties.source;
018
019import java.util.function.Predicate;
020
021import org.springframework.util.Assert;
022
023/**
024 * The state of content from a {@link ConfigurationPropertySource}.
025 *
026 * @author Phillip Webb
027 * @since 2.0.0
028 */
029public enum ConfigurationPropertyState {
030
031        /**
032         * The {@link ConfigurationPropertySource} has at least one matching
033         * {@link ConfigurationProperty}.
034         */
035        PRESENT,
036
037        /**
038         * The {@link ConfigurationPropertySource} has no matching
039         * {@link ConfigurationProperty ConfigurationProperties}.
040         */
041        ABSENT,
042
043        /**
044         * It's not possible to determine if {@link ConfigurationPropertySource} has matching
045         * {@link ConfigurationProperty ConfigurationProperties} or not.
046         */
047        UNKNOWN;
048
049        /**
050         * Search the given iterable using a predicate to determine if content is
051         * {@link #PRESENT} or {@link #ABSENT}.
052         * @param <T> the data type
053         * @param source the source iterable to search
054         * @param predicate the predicate used to test for presence
055         * @return {@link #PRESENT} if the iterable contains a matching item, otherwise
056         * {@link #ABSENT}.
057         */
058        static <T> ConfigurationPropertyState search(Iterable<T> source,
059                        Predicate<T> predicate) {
060                Assert.notNull(source, "Source must not be null");
061                Assert.notNull(predicate, "Predicate must not be null");
062                for (T item : source) {
063                        if (predicate.test(item)) {
064                                return PRESENT;
065                        }
066                }
067                return ABSENT;
068        }
069
070}