| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (

This example Groovy source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

closure, closure, collection, k, map, map, mapwithdefault, mapwithdefault, object, override, set, util, v, v

The Groovy source code

 * Copyright 2003-2010 the original author or authors.
 * 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
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * 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<K, V> implements Map {

    private final Map<K, V> delegate;
    private final Closure initClosure;

    private MapWithDefault(Map<K, V> m, Closure initClosure) {
        delegate = m;
        this.initClosure = initClosure;

    public static <K, V> Map newInstance(Map m, Closure initClosure) {
        return new MapWithDefault<K, V>(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) 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<? extends K, ? extends V> m) {

    public void clear() {

    public Set<K> keySet() {
        return delegate.keySet();

    public Collection<V> values() {
        return delegate.values();

    public Set<Map.Entry entrySet() {
        return delegate.entrySet();

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

    public int hashCode() {
        return delegate.hashCode();

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.