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.Map;
020
021import org.springframework.lang.Nullable;
022import org.springframework.util.StringUtils;
023
024/**
025 * {@link PropertySource} that reads keys and values from a {@code Map} object.
026 * The underlying map should not contain any {@code null} values in order to
027 * comply with {@link #getProperty} and {@link #containsProperty} semantics.
028 *
029 * @author Chris Beams
030 * @author Juergen Hoeller
031 * @since 3.1
032 * @see PropertiesPropertySource
033 */
034public class MapPropertySource extends EnumerablePropertySource<Map<String, Object>> {
035
036        /**
037         * Create a new {@code MapPropertySource} with the given name and {@code Map}.
038         * @param name the associated name
039         * @param source the Map source (without {@code null} values in order to get
040         * consistent {@link #getProperty} and {@link #containsProperty} behavior)
041         */
042        public MapPropertySource(String name, Map<String, Object> source) {
043                super(name, source);
044        }
045
046
047        @Override
048        @Nullable
049        public Object getProperty(String name) {
050                return this.source.get(name);
051        }
052
053        @Override
054        public boolean containsProperty(String name) {
055                return this.source.containsKey(name);
056        }
057
058        @Override
059        public String[] getPropertyNames() {
060                return StringUtils.toStringArray(this.source.keySet());
061        }
062
063}