Skip to content

Instantly share code, notes, and snippets.

@seanjreilly
Created July 1, 2015 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanjreilly/a646e53ac93925a06680 to your computer and use it in GitHub Desktop.
Save seanjreilly/a646e53ac93925a06680 to your computer and use it in GitHub Desktop.
Pattern for ensuring that an optional collection passed into an argument is immutable
import java.util.Optional;
import java.util.LinkedHashSet;
import java,util.Collections;
public final class Foo {
private final Optional<Set<String>> args;
public Foo(Optional<Set<String>> args) {
this.args = args.map(LinkedHashSet::new).map(Collections::unmodifiableSet);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment