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

fj.data.hamt.Node Maven / Gradle / Ivy

Go to download

Functional Java is an open source library that supports closures for the Java programming language

There is a newer version: 5.0
Show newest version
package fj.data.hamt;

import fj.F;
import fj.P2;
import fj.Show;
import fj.data.Either;
import fj.data.Option;
import fj.data.Stream;

/**
 * A Hash Array Mapped Trie node that is either a key-value pair or a
 * Hash Array Mapped Trie.
 *
 * Created by maperr on 31/05/2016.
 */
public final class Node {

    private final Either, HashArrayMappedTrie> either;

    public Node(final Either, HashArrayMappedTrie> e) {
        either = e;
    }

    public Node(final P2 simpleNode) {
        this(Either.left(simpleNode));
    }

    public Node(final HashArrayMappedTrie hamt) {
        this(Either.right(hamt));
    }

    public static  Node p2Node(final P2 p) {
        return new Node<>(p);
    }

    public static  Node hamtNode(final HashArrayMappedTrie hamt) {
        return new Node<>(hamt);
    }

    /**
     * Performs a reduction on this Node using the given arguments.
     */
    public  B match(final F, B> f, final F, B> g) {
        return either.either(f, g);
    }

    public Stream> toStream() {
        return match(Stream::single, HashArrayMappedTrie::toStream);
    }

    @Override
    public String toString() {
        return Show.hamtNodeShow(Show.anyShow(), Show.anyShow()).showS(this);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy