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

groovy.lang.MapWithDefault Maven / Gradle / Ivy

There is a newer version: 3.0.21
Show newest version
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 groovy.lang;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * A wrapper for Map which allows a default value to be specified.
 *
 * @author Paul King
 * @since 1.7.1
 */
public final class MapWithDefault implements Map {

    private final Map delegate;
    private final Closure initClosure;

    private MapWithDefault(Map m, Closure initClosure) {
        delegate = m;
        this.initClosure = initClosure;
    }

    public static  Map newInstance(Map m, Closure initClosure) {
        return new MapWithDefault(m, initClosure);
    }

    public int size() {
        return delegate.size();
    }

    public boolean isEmpty() {
        return delegate.isEmpty();
    }

    public boolean containsKey(Object key) {
        return delegate.containsKey(key);
    }

    public boolean containsValue(Object value) {
        return delegate.containsValue(value);
    }

    public V get(Object key) {
        if (!delegate.containsKey(key)) {
            delegate.put((K)key, (V)initClosure.call(new Object[]{key}));
        }
        return delegate.get(key);
    }

    public V put(K key, V value) {
        return delegate.put(key, value);
    }

    public V remove(Object key) {
        return delegate.remove(key);
    }

    public void putAll(Map m) {
        delegate.putAll(m);
    }

    public void clear() {
        delegate.clear();
    }

    public Set keySet() {
        return delegate.keySet();
    }

    public Collection values() {
        return delegate.values();
    }

    public Set> entrySet() {
        return delegate.entrySet();
    }

    @Override
    public boolean equals(Object obj) {
        return delegate.equals(obj);
    }

    @Override
    public int hashCode() {
        return delegate.hashCode();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy