Simplify getLabels() by using two separate methods.

This commit is contained in:
Assaf 2018-06-20 14:06:04 +03:00 committed by Ben Gruver
parent 86963ccb43
commit 7542a6b531

View File

@ -75,22 +75,42 @@ public class MethodLocation {
} }
@Nonnull @Nonnull
private List<Label> getLabels(boolean mutable) { private List<Label> getMutableLabels() {
if (labels == null) {
labels = new ArrayList<>(1);
}
return labels;
}
@Nonnull
private List<Label> getImmutableLabels() {
if (labels == null) { if (labels == null) {
if (mutable) {
labels = new ArrayList<Label>(1);
return labels;
}
return ImmutableList.of(); return ImmutableList.of();
} }
return labels; return labels;
} }
@Nonnull
private List<BuilderDebugItem> getMutableDebugItems() {
if (debugItems == null) {
debugItems = new ArrayList<>(1);
}
return debugItems;
}
@Nonnull
private List<BuilderDebugItem> getImmutableDebugItems() {
if (debugItems == null) {
return ImmutableList.of();
}
return debugItems;
}
@Nonnull @Nonnull
private List<BuilderDebugItem> getDebugItems(boolean mutable) { private List<BuilderDebugItem> getDebugItems(boolean mutable) {
if (debugItems == null) { if (debugItems == null) {
if (mutable) { if (mutable) {
debugItems = new ArrayList<BuilderDebugItem>(1); debugItems = new ArrayList<>(1);
return debugItems; return debugItems;
} }
return ImmutableList.of(); return ImmutableList.of();
@ -100,8 +120,8 @@ public class MethodLocation {
void mergeInto(@Nonnull MethodLocation other) { void mergeInto(@Nonnull MethodLocation other) {
if (this.labels != null || other.labels != null) { if (this.labels != null || other.labels != null) {
List<Label> otherLabels = other.getLabels(true); List<Label> otherLabels = other.getMutableLabels();
for (Label label: this.getLabels(false)) { for (Label label: this.getImmutableLabels()) {
label.location = other; label.location = other;
otherLabels.add(label); otherLabels.add(label);
} }
@ -111,11 +131,11 @@ public class MethodLocation {
if (this.debugItems != null || other.labels != null) { if (this.debugItems != null || other.labels != null) {
// We need to keep the debug items in the same order. We add the other debug items to this list, then reassign // We need to keep the debug items in the same order. We add the other debug items to this list, then reassign
// the list. // the list.
List<BuilderDebugItem> debugItems = getDebugItems(true); List<BuilderDebugItem> debugItems = getMutableDebugItems();
for (BuilderDebugItem debugItem: debugItems) { for (BuilderDebugItem debugItem: debugItems) {
debugItem.location = other; debugItem.location = other;
} }
debugItems.addAll(other.getDebugItems(false)); debugItems.addAll(other.getImmutableDebugItems());
other.debugItems = debugItems; other.debugItems = debugItems;
this.debugItems = null; this.debugItems = null;
} }
@ -126,7 +146,7 @@ public class MethodLocation {
return new AbstractSet<Label>() { return new AbstractSet<Label>() {
@Nonnull @Nonnull
@Override public Iterator<Label> iterator() { @Override public Iterator<Label> iterator() {
final Iterator<Label> it = getLabels(false).iterator(); final Iterator<Label> it = getImmutableLabels().iterator();
return new Iterator<Label>() { return new Iterator<Label>() {
private @Nullable Label currentLabel = null; private @Nullable Label currentLabel = null;
@ -150,7 +170,7 @@ public class MethodLocation {
} }
@Override public int size() { @Override public int size() {
return getLabels(false).size(); return getImmutableLabels().size();
} }
@Override public boolean add(@Nonnull Label label) { @Override public boolean add(@Nonnull Label label) {
@ -159,7 +179,7 @@ public class MethodLocation {
"it from its current location first."); "it from its current location first.");
} }
label.location = MethodLocation.this; label.location = MethodLocation.this;
getLabels(true).add(label); getMutableLabels().add(label);
return true; return true;
} }
}; };
@ -168,7 +188,7 @@ public class MethodLocation {
@Nonnull @Nonnull
public Label addNewLabel() { public Label addNewLabel() {
Label label = new Label(this); Label label = new Label(this);
getLabels(true).add(label); getMutableLabels().add(label);
return label; return label;
} }
@ -177,7 +197,7 @@ public class MethodLocation {
return new AbstractSet<BuilderDebugItem>() { return new AbstractSet<BuilderDebugItem>() {
@Nonnull @Nonnull
@Override public Iterator<BuilderDebugItem> iterator() { @Override public Iterator<BuilderDebugItem> iterator() {
final Iterator<BuilderDebugItem> it = getDebugItems(false).iterator(); final Iterator<BuilderDebugItem> it = getImmutableDebugItems().iterator();
return new Iterator<BuilderDebugItem>() { return new Iterator<BuilderDebugItem>() {
private @Nullable BuilderDebugItem currentDebugItem = null; private @Nullable BuilderDebugItem currentDebugItem = null;
@ -201,7 +221,7 @@ public class MethodLocation {
} }
@Override public int size() { @Override public int size() {
return getDebugItems(false).size(); return getImmutableDebugItems().size();
} }
@Override public boolean add(@Nonnull BuilderDebugItem debugItem) { @Override public boolean add(@Nonnull BuilderDebugItem debugItem) {
@ -210,7 +230,7 @@ public class MethodLocation {
"method. You must remove it from its current location first."); "method. You must remove it from its current location first.");
} }
debugItem.location = MethodLocation.this; debugItem.location = MethodLocation.this;
getDebugItems(true).add(debugItem); getMutableDebugItems().add(debugItem);
return true; return true;
} }
}; };