@Override public boolean containsProperty(String key) { RelaxedNames prefixes = new RelaxedNames(this.prefix); RelaxedNames keys = new RelaxedNames(key); for (String prefix : prefixes) { for (String relaxedKey : keys) { if (this.resolver.containsProperty(prefix + relaxedKey)) { return true; } } } return false; }
private static String getSubKey(String name, String rootPrefixes, RelaxedNames keyPrefix) { rootPrefixes = (rootPrefixes != null ? rootPrefixes : ""); for (String rootPrefix : new RelaxedNames(rootPrefixes)) { for (String candidateKeyPrefix : keyPrefix) { if (name.startsWith(rootPrefix + candidateKeyPrefix)) { return name.substring((rootPrefix + candidateKeyPrefix).length()); } } } return null; } }
@Override public <T> T getProperty(String key, Class<T> targetType, T defaultValue) { RelaxedNames prefixes = new RelaxedNames(this.prefix); RelaxedNames keys = new RelaxedNames(key); for (String prefix : prefixes) { for (String relaxedKey : keys) { if (this.resolver.containsProperty(prefix + relaxedKey)) { return this.resolver.getProperty(prefix + relaxedKey, targetType); } } } return defaultValue; }
/** * Return a {@link RelaxedNames} for the given source camelCase source name. * @param name the source name in camelCase * @return the relaxed names */ public static RelaxedNames forCamelCase(String name) { StringBuilder result = new StringBuilder(); for (char c : name.toCharArray()) { result.append(Character.isUpperCase(c) && result.length() > 0 && result.charAt(result.length() - 1) != '-' ? "-" + Character.toLowerCase(c) : c); } return new RelaxedNames(result.toString()); } }
@SuppressWarnings("unchecked") @Deprecated public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) { RelaxedNames prefixes = new RelaxedNames(this.prefix); RelaxedNames keys = new RelaxedNames(key); for (String prefix : prefixes) { for (String relaxedKey : keys) { if (this.resolver.containsProperty(prefix + relaxedKey)) { return (Class<T>) this.resolver.getProperty(prefix + relaxedKey, targetType); } } } return null; }
/** * Return a Map of all values from the specified {@link PropertySources} that start * with a particular key. * @param propertySources the property sources to scan * @param rootPrefix a root prefix to be prepended to the keyPrefix (can be * {@code null}) * @param keyPrefix the key prefixes to test * @return a map of all sub properties starting with the specified key prefixes. * @see #getSubProperties(PropertySources, String, String) */ public static Map<String, Object> getSubProperties(PropertySources propertySources, String rootPrefix, String keyPrefix) { RelaxedNames keyPrefixes = new RelaxedNames(keyPrefix); Map<String, Object> subProperties = new LinkedHashMap<String, Object>(); for (PropertySource<?> source : propertySources) { if (source instanceof EnumerablePropertySource) { for (String name : ((EnumerablePropertySource<?>) source) .getPropertyNames()) { String key = PropertySourceUtils.getSubKey(name, rootPrefix, keyPrefixes); if (key != null && !subProperties.containsKey(key)) { subProperties.put(key, source.getProperty(name)); } } } } return Collections.unmodifiableMap(subProperties); }