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

com.shapesecurity.functional.data.ImmutableSet Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
package com.shapesecurity.functional.data;

import com.shapesecurity.functional.F2;
import com.shapesecurity.functional.Unit;

import org.jetbrains.annotations.NotNull;

public class ImmutableSet {
    @NotNull
    private final HashTable data;

    private ImmutableSet(@NotNull HashTable data) {
        this.data = data;
    }

    public static  ImmutableSet empty() { // object equality (.equals)
        return new ImmutableSet<>(HashTable.empty());
    }

    public static  ImmutableSet emptyP() { // object identity (==)
        return new ImmutableSet<>(HashTable.emptyP());
    }

    public ImmutableSet put(@NotNull T datum) {
        return new ImmutableSet<>(this.data.put(datum, Unit.unit));
    }

    public boolean contains(@NotNull T datum) {
        return this.data.containsKey(datum);
    }

    public ImmutableSet remove(@NotNull T datum) {
        return new ImmutableSet<>(this.data.remove(datum));
    }

    public  A foldAbelian(@NotNull F2 f, @NotNull A init) {
        return this.data.foldRight((p, acc) -> f.apply(p.a, acc), init);
    }

    public ImmutableSet union(@NotNull ImmutableSet other) {
        return new ImmutableSet<>(this.data.merge(other.data));
    }

    // Does not guarantee ordering of elements in resulting list.
    public ImmutableList toList() {
        return this.foldAbelian((v, acc) -> acc.cons(v), ImmutableList.nil());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy