// SPDX-FileCopyrightText: 2021 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0

package dan200.computercraft.api.lua;

import org.jspecify.annotations.Nullable;

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

/**
 * An implementation of {@link LuaTable} based on a standard Java {@link Map}.
 */
public class ObjectLuaTable implements LuaTable<Object, Object> {
    private final Map<Object, Object> map;

    public ObjectLuaTable(Map<?, ?> map) {
        this.map = Collections.unmodifiableMap(map);
    }

    @Override
    public int size() {
        return map.size();
    }

    @Override
    public boolean isEmpty() {
        return map.isEmpty();
    }

    @Override
    public boolean containsKey(Object o) {
        return map.containsKey(o);
    }

    @Override
    public boolean containsValue(Object o) {
        return map.containsKey(o);
    }

    @Nullable
    @Override
    public Object get(Object o) {
        return map.get(o);
    }

    @Override
    public Set<Object> keySet() {
        return map.keySet();
    }

    @Override
    public Collection<Object> values() {
        return map.values();
    }

    @Override
    public Set<Entry<Object, Object>> entrySet() {
        return map.entrySet();
    }

    @Override
    public boolean equals(Object o) {
        return this == o || o instanceof Map<?, ?> otherMap && map.equals(otherMap);
    }

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