All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.archaius.config.MapConfig Maven / Gradle / Ivy

There is a newer version: 2.8.2
Show newest version
/**
 * Copyright 2015 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.netflix.archaius.config;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.function.BiConsumer;

/**
 * Config backed by an immutable map.
 */
public class MapConfig extends AbstractConfig {

    /**
     * The builder only provides convenience for fluent style adding of properties
     * 
     * {@code
     * 
     * MapConfig.builder()
     *      .put("foo", "bar")
     *      .put("baz", 123)
     *      .build()
     * 
* } */ public static class Builder { Map map = new HashMap(); String named; public Builder put(String key, T value) { map.put(key, value.toString()); return this; } public Builder putAll(Map props) { map.putAll(props); return this; } public Builder putAll(Properties props) { props.forEach((k, v) -> map.put(k.toString(), v.toString())); return this; } public Builder name(String name) { this.named = name; return this; } public MapConfig build() { return new MapConfig(named == null ? generateUniqueName("immutable-") : named, map); } } public static Builder builder() { return new Builder(); } public static MapConfig from(Properties props) { return new MapConfig(props); } public static MapConfig from(Map props) { return new MapConfig(props); } private Map props = new HashMap(); public MapConfig(String name, Map props) { super(name); this.props.putAll(props); this.props = Collections.unmodifiableMap(this.props); } /** * Construct a MapConfig as a copy of the provided Map * @param name * @param props */ public MapConfig(Map props) { super(generateUniqueName("immutable-")); this.props.putAll(props); this.props = Collections.unmodifiableMap(this.props); } /** * Construct a MapConfig as a copy of the provided properties * @param name * @param props */ public MapConfig(Properties props) { super(generateUniqueName("immutable-")); for (Entry entry : props.entrySet()) { this.props.put(entry.getKey().toString(), entry.getValue().toString()); } this.props = Collections.unmodifiableMap(this.props); } @Override public Object getRawProperty(String key) { return props.get(key); } @Override public boolean containsKey(String key) { return props.containsKey(key); } @Override public boolean isEmpty() { return props.isEmpty(); } @Override public Iterator getKeys() { return props.keySet().iterator(); } @Override public void forEachProperty(BiConsumer consumer) { props.forEach(consumer); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy