
com.jnape.palatable.lambda.lens.lenses.SetLens Maven / Gradle / Ivy
package com.jnape.palatable.lambda.lens.lenses;
import com.jnape.palatable.lambda.lens.Lens;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import static com.jnape.palatable.lambda.lens.Lens.simpleLens;
/**
* Lenses that operate on {@link Set}s.
*/
public final class SetLens {
private SetLens() {
}
/**
* A lens that focuses on whether a {@link Set} contains some value a
. Note that copyFn
is
* used to avoid mutating the {@link Set} in question.
*
* @param copyFn the copy function
* @param a the value in question
* @param the value type
* @param the set to focus on
* @return a lens that focuses on a value's inclusion in a given {@link Set}
*/
public static > Lens.Simple contains(
Function super SetA, ? extends SetA> copyFn, A a) {
return simpleLens(setA -> setA.contains(a),
(setA, include) -> {
SetA copy = copyFn.apply(setA);
if (include) copy.add(a);
else copy.remove(a);
return copy;
});
}
/**
* A lens that focuses on whether a {@link Set} contains some value a
. Like {@link #contains(Function,
* Object)} but with an implicit copy function that produces {@link HashSet}s
.
*
* @param a the value in question
* @param the value type
* @return a lens that focuses on a value's inclusion in a given {@link Set}
*/
public static Lens.Simple, Boolean> contains(A a) {
return contains(HashSet::new, a);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy