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

net.shibboleth.utilities.java.support.collection.LazyMap Maven / Gradle / Ivy

There is a newer version: 8.0.0
Show newest version
/*
 * Licensed to the University Corporation for Advanced Internet Development, 
 * Inc. (UCAID) under one or more contributor license agreements.  See the 
 * NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.collection;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.annotation.concurrent.NotThreadSafe;

/**
 * A map that is lazy initialized. This map takes very little memory when storing zero or one item.
 * 
 * @param  the type of the map keys
 * @param  the type of the map values
 */
@NotThreadSafe
public class LazyMap implements Map, Serializable {

    /** Serial version UID. */
    private static final long serialVersionUID = 121425595164176639L;

    /** The delegate map. */
    private Map delegate = Collections.emptyMap();

    /** {@inheritDoc} */
    public void clear() {
        delegate = Collections.emptyMap();
    }

    /** {@inheritDoc} */
    public boolean containsKey(final Object key) {
        return delegate.containsKey(key);
    }

    /** {@inheritDoc} */
    public boolean containsValue(final Object value) {
        return delegate.containsValue(value);
    }

    /** {@inheritDoc} */
    public Set> entrySet() {
        delegate = buildMap();
        return delegate.entrySet();
    }

    /** {@inheritDoc} */
    public ValueType get(final Object key) {
        return delegate.get(key);
    }

    /** {@inheritDoc} */
    public boolean isEmpty() {
        return delegate.isEmpty();
    }

    /** {@inheritDoc} */
    public Set keySet() {
        delegate = buildMap();
        return delegate.keySet();
    }

    /** {@inheritDoc} */
    public ValueType put(final KeyType key, final ValueType value) {
        if (delegate.isEmpty()) {
            delegate = Collections.singletonMap(key, value);
            return null;
        } else {
            delegate = buildMap();
            return delegate.put(key, value);
        }
    }

    /** {@inheritDoc} */
    public void putAll(final Map t) {
        delegate = buildMap();
        delegate.putAll(t);
    }

    /** {@inheritDoc} */
    public ValueType remove(final Object key) {
        delegate = buildMap();
        return delegate.remove(key);
    }

    /** {@inheritDoc} */
    public int size() {
        return delegate.size();
    }

    /** {@inheritDoc} */
    public Collection values() {
        delegate = buildMap();
        return delegate.values();
    }

    /**
     * Builds an appropriate delegate map.
     * 
     * @return the delegate map
     */
    protected Map buildMap() {
        if (delegate instanceof HashMap) {
            return delegate;
        }

        return new HashMap<>(delegate);
    }

    /** {@inheritDoc} */
    public String toString() {
        return delegate.toString();
    }

    /** {@inheritDoc} */
    public int hashCode() {
        return delegate.hashCode();
    }

    /** {@inheritDoc} */
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null || this.getClass() != obj.getClass()) {
            return false;
        }

        return delegate.equals(((LazyMap) obj).delegate);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy