com.shapesecurity.functional.data.ImmutableSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shape-functional-java Show documentation
Show all versions of shape-functional-java Show documentation
Functional programming library
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());
}
}