Review a generated wildcard transfer

from Wildcards and PECS
Java 25 LTS advanced 8 min 4 issues to find

Review this generated collection utility before it becomes a shared API.

Append accepted source values not already present in the destination, preserve source encounter order, never mutate the source, handle an empty source, and keep the method silent.

Java
import java.util.List;
import java.util.function.Predicate;

public class GeneratedTransfer {
    static <T> int copyUnique(
            List<? extends T> source,
            List<? super T> destination,
            Predicate<? super T> accepted) {
        T first = source.getFirst();
        source.sort(null);

        int copied = 0;
        for (T value : source) {
            if (accepted.test(value)
                    && !destination.contains(value)) {
                destination.add(value);
                copied++;
            }
        }
        System.out.println("first=" + first);
        return copied;
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error