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

com.yahoo.bard.webservice.util.DefaultingDictionary Maven / Gradle / Ivy

Go to download

Fili web service library provides core capabilities for RESTful aggregation navigation, query planning and metadata

There is a newer version: 1.1.13
Show newest version
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms.
package com.yahoo.bard.webservice.util;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * DefaultingDictionary is a Map that returns a default value if a given key is not present in the map, or returns the
 * value associated with the given key.
 *
 * @param  the type of keys maintained by this map
 * @param  the type of mapped values
 */
public class DefaultingDictionary extends LinkedHashMap {

    private final V defaultValue;

    /**
     * Construct the defaultingDictionary using the defaultValue provided.
     *
     * @param defaultValue  The defaultValue to be returned if a key is not present in the map
     */
    public DefaultingDictionary(V defaultValue) {
        this.defaultValue = defaultValue;
    }

    /**
     * Construct DefaultingDictionary using the given defaultValue and entries from the given defaultingDictionaryMap.
     *
     * @param defaultValue  The defaultValue to be returned if a key is not present in the map
     * @param defaultingDictionaryMap  A Map whose entries will be inserted into the DefaultingDictionary
     */
    public DefaultingDictionary(V defaultValue, Map defaultingDictionaryMap) {
        this.defaultValue = defaultValue;
        this.putAll(defaultingDictionaryMap);
    }

    /**
     * Return the defaultValue for this class.
     *
     * @return defaultValue for this class
     */
    public V getDefaultValue() {
        return defaultValue;
    }

    @Override
    public V get(Object key) {
        return getOrDefault(key, defaultValue);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy