001/* 002 * Copyright 2012-2018 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.Arrays; 020import java.util.Collections; 021import java.util.Iterator; 022import java.util.List; 023import java.util.Map; 024 025import org.springframework.util.Assert; 026import org.springframework.util.LinkedMultiValueMap; 027import org.springframework.util.MultiValueMap; 028 029/** 030 * Maintains a mapping of {@link ConfigurationPropertyName} aliases. 031 * 032 * @author Phillip Webb 033 * @author Madhura Bhave 034 * @since 2.0.0 035 * @see ConfigurationPropertySource#withAliases(ConfigurationPropertyNameAliases) 036 */ 037public final class ConfigurationPropertyNameAliases 038 implements Iterable<ConfigurationPropertyName> { 039 040 private final MultiValueMap<ConfigurationPropertyName, ConfigurationPropertyName> aliases = new LinkedMultiValueMap<>(); 041 042 public ConfigurationPropertyNameAliases() { 043 } 044 045 public ConfigurationPropertyNameAliases(String name, String... aliases) { 046 addAliases(name, aliases); 047 } 048 049 public ConfigurationPropertyNameAliases(ConfigurationPropertyName name, 050 ConfigurationPropertyName... aliases) { 051 addAliases(name, aliases); 052 } 053 054 public void addAliases(String name, String... aliases) { 055 Assert.notNull(name, "Name must not be null"); 056 Assert.notNull(aliases, "Aliases must not be null"); 057 addAliases(ConfigurationPropertyName.of(name), 058 Arrays.stream(aliases).map(ConfigurationPropertyName::of) 059 .toArray(ConfigurationPropertyName[]::new)); 060 } 061 062 public void addAliases(ConfigurationPropertyName name, 063 ConfigurationPropertyName... aliases) { 064 Assert.notNull(name, "Name must not be null"); 065 Assert.notNull(aliases, "Aliases must not be null"); 066 this.aliases.addAll(name, Arrays.asList(aliases)); 067 } 068 069 public List<ConfigurationPropertyName> getAliases(ConfigurationPropertyName name) { 070 return this.aliases.getOrDefault(name, Collections.emptyList()); 071 } 072 073 public ConfigurationPropertyName getNameForAlias(ConfigurationPropertyName alias) { 074 return this.aliases.entrySet().stream() 075 .filter((e) -> e.getValue().contains(alias)).map(Map.Entry::getKey) 076 .findFirst().orElse(null); 077 } 078 079 @Override 080 public Iterator<ConfigurationPropertyName> iterator() { 081 return this.aliases.keySet().iterator(); 082 } 083 084}