Created
November 24, 2015 14:40
-
-
Save benkn/98de3fa28368e20babe7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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