Refactor LocatedItems to use the function addItem() instead of

initItemsIfNull().
This commit is contained in:
Assaf 2018-07-17 15:47:21 +03:00 committed by Ben Gruver
parent af8bc1d9cd
commit 1bf8f2544b

View File

@ -13,12 +13,6 @@ public abstract class LocatedItems<T extends ItemWithLocation> {
@Nullable @Nullable
private List<T> items = null; private List<T> items = null;
private void initItemsIfNull() {
if (items == null) {
items = new ArrayList<>(1);
}
}
@Nonnull @Nonnull
private List<T> getItems() { private List<T> getItems() {
if (items == null) { if (items == null) {
@ -70,13 +64,19 @@ public abstract class LocatedItems<T extends ItemWithLocation> {
throw new IllegalArgumentException(getAddLocatedItemError()); throw new IllegalArgumentException(getAddLocatedItemError());
} }
item.setLocation(newItemsLocation); item.setLocation(newItemsLocation);
initItemsIfNull(); addItem(item);
items.add(item);
return true; return true;
} }
}; };
} }
private void addItem(@Nonnull T item) {
if (items == null) {
items = new ArrayList<>(1);
}
items.add(item);
}
protected abstract String getAddLocatedItemError(); protected abstract String getAddLocatedItemError();
public void mergeItemsIntoNext(@Nonnull MethodLocation nextLocation, LocatedItems<T> otherLocatedItems) { public void mergeItemsIntoNext(@Nonnull MethodLocation nextLocation, LocatedItems<T> otherLocatedItems) {