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

software.amazon.event.ruler.SetOperations Maven / Gradle / Ivy

package software.amazon.event.ruler;

import java.util.Set;
import java.util.function.Function;

class SetOperations {

    private SetOperations() { }

    /**
     * Add each element of the intersection of two sets to a third set. This is optimized for performance as it iterates
     * through the smaller set.
     *
     * @param set1 First set involved in intersection.
     * @param set2 Second set involved in intersection.
     * @param addTo Add intersection to this set.
     * @param  Type of all three sets.
     */
    public static  void intersection(final Set set1, final Set set2, final Set addTo) {
        intersection(set1, set2, addTo, t -> t);
    }

    /**
     * Add the transformation of each element of the intersection of two sets to a third set. This is optimized for
     * performance as it iterates through the smaller set.
     *
     * @param set1 First set involved in intersection.
     * @param set2 Second set involved in intersection.
     * @param addTo Add intersection to this set.
     * @param transform Transform each element of intersection into an element of the third set.
     * @param  Type of first two sets.
     * @param  Type of addTo set.
     */
    public static  void intersection(final Set set1, final Set set2, final Set addTo,
                                           final Function transform) {
        Set smaller = set1.size() <= set2.size() ? set1 : set2;
        Set larger = set1.size() <= set2.size() ? set2 : set1;
        for (T element : smaller) {
            if (larger.contains(element)) {
                addTo.add(transform.apply(element));
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy