I have a Java AutoValue class with a List attribute. I'd like to allow the builder to append to the List rather than having to pass the entire constructed list.
Example:
import com.google.auto.value.AutoValue;
@AutoValue
public abstract class Deck {
public abstract List<Card> cards();
public static Builder builder() {
return new AutoValue_Card.Builder()
.cards(new ArrayList<Card>());
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder cards(List<Card> cards);
/**
* Append card to cards in the constructed Deck.
*/
public Builder addCard(Card card) {
// Is it possible to write this function?
}
}
}
What's the best solution for writing the addCard function? Does AutoValue somehow support this already? The intermediate cards property in the constructed class not visible to the Builder so I can't access it directly. I could try to bypass the Builder directly by keeping my own copy of cards in the Builder, is that the only option here?