001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.commons.settings;
018
019import java.util.*;
020
021/**
022 * Interface for pluggable property sources used by {@link Settings}.
023 *
024 * <p>
025 * A setting source provides a way to retrieve property values.
026 * Sources are checked in reverse order (last added is checked first) when looking up properties.
027 *
028 * <p>
029 * For writable sources that support modifying property values, see {@link SettingStore}.
030 *
031 * <h5 class='section'>Return Value Semantics:</h5>
032 * <ul class='spaced-list'>
033 *    <li><c>null</c> - The setting does not exist in this source. The lookup will continue to the next source.
034 *    <li><c>Optional.empty()</c> - The setting exists but has an explicitly null value. This will be returned
035 *       immediately, overriding any values from lower-priority sources.
036 *    <li><c>Optional.of(value)</c> - The setting exists and has a non-null value. This will be returned immediately.
037 * </ul>
038 *
039 * <h5 class='section'>Examples:</h5>
040 * <p class='bjava'>
041 *    <jc>// Create a read-only functional source directly from a lambda</jc>
042 *    FunctionalSource <jv>readOnly</jv> = name -&gt; opt(System.getProperty(name));
043 *
044 *    <jc>// Or use the factory method</jc>
045 *    FunctionalSource <jv>readOnly2</jv> = FunctionalSource.<jsf>of</jsf>(System::getProperty);
046 *
047 *    <jc>// Stores can be used as sources (they extend SettingSource)</jc>
048 *    MapStore <jv>store</jv> = <jk>new</jk> MapStore();
049 *    <jv>store</jv>.set(<js>"my.property"</js>, <js>"value"</js>);
050 *    Settings.<jsf>get</jsf>().addSource(<jv>store</jv>);  <jc>// Stores can be added as sources</jc>
051 * </p>
052 */
053public interface SettingSource {
054
055   /**
056    * Returns a setting in this setting source.
057    *
058    * <p>
059    * Return value semantics:
060    * <ul>
061    *    <li><c>null</c> - The setting does not exist in this source. The lookup will continue to the next source.
062    *    <li><c>Optional.empty()</c> - The setting exists but has an explicitly null value. This will be returned
063    *       immediately, overriding any values from lower-priority sources.
064    *    <li><c>Optional.of(value)</c> - The setting exists and has a non-null value. This will be returned immediately.
065    * </ul>
066    *
067    * @param name The property name.
068    * @return The property value, <c>null</c> if the property doesn't exist in this source, or <c>Optional.empty()</c>
069    *    if the property exists but has a null value.
070    */
071   Optional<String> get(String name);
072}