Skip to content

Instantly share code, notes, and snippets.

@mo-gr
Created July 17, 2012 10:01
Show Gist options
  • Save mo-gr/3128498 to your computer and use it in GitHub Desktop.
Save mo-gr/3128498 to your computer and use it in GitHub Desktop.
List getter/setter
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);
}
}
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