From 9f70ed17db8f90b639905ef08b92d3bd8c52ee75 Mon Sep 17 00:00:00 2001 From: "Damith C. Rajapakse" Date: Wed, 19 Oct 2016 19:20:12 +0800 Subject: [PATCH] [#159] Remove non-static initializers Remove non-static initializers except one (keep one as an example) --- src/main/java/seedu/address/model/AddressBook.java | 7 +++++++ .../address/storage/XmlSerializableAddressBook.java | 11 +++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index 674b07f0ce6f..3332eb6792dc 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -18,6 +18,13 @@ public class AddressBook implements ReadOnlyAddressBook { private final UniquePersonList persons; private final UniqueTagList tags; + /* + * The 'unusual' code block below is an non-static initialization block, sometimes used to avoid duplication + * between constructors. See https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html + * + * Note that non-static init blocks are not recommended to use. There are other ways to avoid duplication + * among constructors. + */ { persons = new UniquePersonList(); tags = new UniqueTagList(); diff --git a/src/main/java/seedu/address/storage/XmlSerializableAddressBook.java b/src/main/java/seedu/address/storage/XmlSerializableAddressBook.java index 2f71f94e66d4..821bc1ce9643 100644 --- a/src/main/java/seedu/address/storage/XmlSerializableAddressBook.java +++ b/src/main/java/seedu/address/storage/XmlSerializableAddressBook.java @@ -25,20 +25,19 @@ public class XmlSerializableAddressBook implements ReadOnlyAddressBook { @XmlElement private List tags; - { - persons = new ArrayList<>(); - tags = new ArrayList<>(); - } - /** * Empty constructor required for marshalling */ - public XmlSerializableAddressBook() {} + public XmlSerializableAddressBook() { + persons = new ArrayList<>(); + tags = new ArrayList<>(); + } /** * Conversion */ public XmlSerializableAddressBook(ReadOnlyAddressBook src) { + this(); persons.addAll(src.getPersonList().stream().map(XmlAdaptedPerson::new).collect(Collectors.toList())); tags.addAll(src.getTagList().stream().map(XmlAdaptedTag::new).collect(Collectors.toList())); }