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

VCollections.src.org.violetlib.collections.impl.EmptyMap Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2023 Alan Snyder.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the license agreement. For details see
 * accompanying license terms.
 */

package org.violetlib.collections.impl;

import org.violetlib.collections.Binding;
import org.violetlib.collections.IIterator;
import org.violetlib.collections.IMap;
import org.violetlib.collections.ISet;
import org.violetlib.util.Extensions;

import org.jetbrains.annotations.*;
import org.violetlib.annotations.Immutable;

/**
  A map containing no bindings.
*/

public final @Immutable class EmptyMap
  implements IMap
{
    public static  @NotNull IMap get()
    {
        return (IMap) INSTANCE;
    }

    private static final @NotNull EmptyMap INSTANCE = new EmptyMap<>();

    private EmptyMap()
    {
    }

    @Override
    public boolean isEmpty()
    {
        return true;
    }

    @Override
    public int size()
    {
        return 0;
    }

    @Override
    public @Nullable V get(@NotNull K key)
    {
        return null;
    }

    @Override
    public boolean containsKey(@NotNull Object key)
    {
        return false;
    }

    @Override
    public void visit(@NotNull Visitor visitor)
    {
    }

    @Override
    public  @Nullable R find(@NotNull FVisitor visitor, @Nullable R defaultResult)
    {
        return null;
    }

    @Override
    public @NotNull ISet keySet()
    {
        return ISet.empty();
    }

    @Override
    public @NotNull ISet values()
    {
        return ISet.empty();
    }

    @Override
    public @NotNull IMap extending(@NotNull K key, @Nullable V value)
    {
        return value != null ? Impl.createSingletonMap(key, value) : this;
    }

    @Override
    public @NotNull IMap extending(@NotNull IMap bindings)
    {
        return bindings;
    }

    @Override
    public @NotNull IIterator> iterator()
    {
        return IMapBindingIterator.create(this);
    }

    @Override
    public int hashCode()
    {
        return MapEquality.computeHashCode(this);
    }

    @Override
    public boolean equals(@Nullable Object obj)
    {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        IMap otherMap = Extensions.getExtension(obj, IMap.class);
        if (otherMap == null) {
            return false;
        }
        return otherMap.isEmpty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy