Created
July 17, 2012 10:01
-
-
Save mo-gr/3128498 to your computer and use it in GitHub Desktop.
List getter/setter
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
private final List<Bla> blas = new ArrayList<Bla>(); | |
// foo.setBlas(foo.getBlas()) is neutral, but every getter invocation creates a new List | |
public List<Bla> getBlas() { return Collections.unmodifiableList(new ArrayList<Bla>(blas)); } | |
public void setBlas(List<Bla> blas) { | |
if (blas != null) { | |
this.blas.clear(); | |
this.blas.addAll(blas); | |
} | |
} |
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
private final List<Bla> blas = new ArrayList<Bla>(); | |
// foo.setBlas(foo.getBlas()) is not neutral. it clears the list! | |
public List<Bla> getBlas() { return Collections.unmodifiableList(blas); } | |
public void setBlas(List<Bla> blas) { | |
if (blas != null) { | |
this.blas.clear(); | |
this.blas.addAll(blas); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment