Skip to content

Instantly share code, notes, and snippets.

@benkn
Created November 24, 2015 14:40
Show Gist options
  • Save benkn/98de3fa28368e20babe7 to your computer and use it in GitHub Desktop.
Save benkn/98de3fa28368e20babe7 to your computer and use it in GitHub Desktop.
import javax.annotation.Nonnull;
import java.util.Collection;
/**
* Utility class for setters of collections in objects.
*
* <pre>
* {@code
* private Set<String> items = new HashSet<>();
* public void setItems(Collection<String> items) {
* CollectionSetter.refill(this.items, items);
* }
* }
* </pre>
*/
public final class CollectionSetter {
/**
* Clears the collection and puts values from the new collection into it
* @param collection Collection (not null)
* @param newCollection Collection
*/
public static <T> void refill(@Nonnull Collection<? super T> collection, Collection<? extends T> newCollection) {
collection.clear();
if (newCollection != null) {
collection.addAll(newCollection);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment