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

com.opsdatastore.collector.hidden.SingletonMap Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.opsdatastore.collector.hidden;

/*-
 * #%L
 * OpsDataStore SDK
 * %%
 * Copyright (C) 2017 OpsDataStore
 * %%
 * 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.
 * #L%
 */

import java.io.Serializable;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Collections;
import static java.util.Collections.singleton;
import java.util.Map;
import java.util.Set;

/**
 * Extension of Collections.SingletonMap that allows for the map to have one or
 * zero elements
 *
 * @author kguthrie
 * @param  Key type
 * @param  Value type
 */
public class SingletonMap
        implements Map, Serializable {

    /**
     * Returns true if the specified arguments are equal, or both null.
     *
     * NB: Do not replace with Object.equals until JDK-8015417 is resolved.
     */
    private static boolean eq(Object o1, Object o2) {
        return o1==null ? o2==null : o1.equals(o2);
    }

    private K k;
    private V v;

    private transient Set keySet;
    private transient Set> entrySet;
    private transient Collection values;


    public SingletonMap() {}

    public SingletonMap(K key, V value) {
        k = key;
        v = value;
    }

    @Override
    public int size() {
        return k == null ? 0 : 1;
    }

    @Override
    public boolean isEmpty() {
        return k == null;
    }

    @Override
    public boolean containsKey(Object key) {
        if (k == null) {
            return false;
        }

        return eq(key, k);
    }

    @Override
    public boolean containsValue(Object value) {
        if (k == null) {
            return false;
        }

        return eq(value, v);
    }

    @Override
    public V get(Object key) {
        if (k == null) {
            return null;
        }

        return (eq(key, k) ? v : null);
    }

    @Override
    public Set keySet() {

        if (k == null) {
            return Collections.EMPTY_SET;
        }

        if (keySet == null) {
            keySet = singleton(k);
        }

        return keySet;
    }

    @Override
    public Set> entrySet() {
        if (k == null) {
            return Collections.EMPTY_SET;
        }

        if (entrySet == null) {
            entrySet = Collections.>singleton(
                    new AbstractMap.SimpleImmutableEntry<>(k, v));
        }
        return entrySet;
    }

    @Override
    public Collection values() {
        if (k == null) {
            return Collections.EMPTY_LIST;
        }

        if (values == null) {
            values = singleton(v);
        }
        return values;
    }

    @Override
    public V put(K key, V value) {
        if (k != null) {
            throw new UnsupportedOperationException("Cannot add to a Singleton "
                    + "Map that is not empty");
        }

        keySet = null;
        entrySet = null;
        values = null;

        k = key;
        v = value;
        return null;
    }

    @Override
    public V remove(Object key) {
        if (k == null) {
            return null;
        }
        if (eq(k,key)) {
            k = null;
            return v;
        }
        return null;
    }

    @Override
    public void putAll(
            Map m) {
        m.forEach((key, value) -> put(key, value));
    }

    @Override
    public void clear() {
        k = null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy