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

tech.ytsaurus.ysontree.YTreeMapNode Maven / Gradle / Ivy

The newest version!
package tech.ytsaurus.ysontree;

import java.util.Collection;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

public interface YTreeMapNode extends YTreeCompositeNode> {

    Set keys();

    Collection values();

    boolean containsKey(String key);

    Optional remove(String key);

    Optional put(String key, YTreeNode value);

    void putAll(Map map);

    Optional get(String key);

    default YTreeNode getOrThrow(String key) {
        return getOrThrow(key, () -> "Key not found: " + key);
    }

    default YTreeNode getOrThrow(String key, Supplier createMessage) {
        return get(key).orElseThrow(() -> new NoSuchElementException(createMessage.get()));
    }

    default YTreeNode getOrDefault(String key, Supplier defaultNode) {
        return get(key).orElseGet(defaultNode);
    }

    default String getString(String key) {
        return getOrThrow(key).stringValue();
    }

    default Optional getStringO(String key) {
        return getFilterNull(key).map(YTreeNode::stringValue);
    }

    default byte[] getBytes(String key) {
        return getOrThrow(key).bytesValue();
    }

    default Optional getBytesO(String key) {
        return getFilterNull(key).map(YTreeNode::bytesValue);
    }

    default int getInt(String key) {
        return getOrThrow(key).intValue();
    }

    default Optional getIntO(String key) {
        return getFilterNull(key).map(YTreeNode::intValue);
    }

    default long getLong(String key) {
        return getOrThrow(key).longValue();
    }

    default Optional getLongO(String key) {
        return getFilterNull(key).map(YTreeNode::longValue);
    }

    default boolean getBool(String key) {
        return getOrThrow(key).boolValue();
    }

    default Optional getBoolO(String key) {
        return getFilterNull(key).map(YTreeNode::boolValue);
    }

    default double getDouble(String key) {
        return getOrThrow(key).doubleValue();
    }

    default Optional getDoubleO(String key) {
        return getFilterNull(key).map(YTreeNode::doubleValue);
    }

    default YTreeListNode getList(String key) {
        return getOrThrow(key).listNode();
    }

    default Optional getListO(String key) {
        return getFilterNull(key).map(YTreeNode::listNode);
    }

    default YTreeMapNode getMap(String key) {
        return getOrThrow(key).mapNode();
    }

    default Optional getMapO(String key) {
        return getFilterNull(key).map(YTreeNode::mapNode);
    }

    default Optional getFilterNull(String key) {
        return get(key).filter(node -> !node.isEntityNode());
    }

    default YTreeBuilder toMapBuilder() {
        YTreeBuilder builder = YTree.builder();
        Map attributes = getAttributes();
        if (!attributes.isEmpty()) {
            builder.beginAttributes();
            for (Map.Entry entry : attributes.entrySet()) {
                builder.key(entry.getKey()).value(entry.getValue());
            }
            builder.endAttributes();
        }
        builder.beginMap();
        for (Map.Entry entry : asMap().entrySet()) {
            builder.key(entry.getKey()).value(entry.getValue());
        }
        return builder;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy