io.sundr.examples.arrays.GameFluentImpl Maven / Gradle / Ivy
package io.sundr.examples.arrays;
import io.sundr.builder.VisitableBuilder;
import io.sundr.builder.Nested;
import java.util.ArrayList;
import java.lang.String;
import io.sundr.builder.Predicate;
import io.sundr.builder.BaseFluent;
import java.util.Collection;
import java.lang.Object;
import java.util.List;
import java.lang.Boolean;
public class GameFluentImpl> extends BaseFluent implements GameFluent{
private List players;
public GameFluentImpl(){
}
public GameFluentImpl(Game instance){
this.withPlayers(instance.getPlayers());
}
public A withPlayers(Person... players){
if (this.players != null) {this.players.clear();}
if (players != null) {for (Person item :players){ this.addToPlayers(item);}} return (A) this;
}
/**
* This method has been deprecated, please use method buildPlayers instead.
*/
public Person[] getPlayers(){
int size = players != null ? players.size() : 0;
Person[] result = new Person[size];
if (size == 0) {
return result;
}
int index = 0;
for (VisitableBuilder extends Person,?> builder : players) {
result[index++]=builder.build();
}
return result;
}
public Person[] buildPlayers(){
int size = players != null ? players.size() : 0;
Person[] result = new Person[size];
if (size == 0) {
return result;
}
int index = 0;
for (VisitableBuilder extends Person,?> builder : players) {
result[index++]=builder.build();
}
return result;
}
public Person buildPlayer(int index){
return this.players.get(index).build();
}
public Person buildFirstPlayer(){
return this.players.get(0).build();
}
public Person buildLastPlayer(){
return this.players.get(players.size() - 1).build();
}
public Person buildMatchingPlayer(Predicate predicate){
for (PersonBuilder item: players) { if(predicate.apply(item)){return item.build();} } return null;
}
public Boolean hasMatchingPlayer(Predicate predicate){
for (PersonBuilder item: players) { if(predicate.apply(item)){return true;} } return false;
}
public A addToPlayers(int index,Person item){
if (this.players == null) {this.players = new ArrayList();}
PersonBuilder builder = new PersonBuilder(item);_visitables.get("players").add(index >= 0 ? index : _visitables.get("players").size(), builder);this.players.add(index >= 0 ? index : players.size(), builder); return (A)this;
}
public A setToPlayers(int index,Person item){
if (this.players == null) {this.players = new ArrayList();}
PersonBuilder builder = new PersonBuilder(item);
if (index < 0 || index >= _visitables.get("players").size()) { _visitables.get("players").add(builder); } else { _visitables.get("players").set(index, builder);}
if (index < 0 || index >= players.size()) { players.add(builder); } else { players.set(index, builder);}
return (A)this;
}
public A addToPlayers(Person... items){
if (this.players == null) {this.players = new ArrayList();}
for (Person item : items) {PersonBuilder builder = new PersonBuilder(item);_visitables.get("players").add(builder);this.players.add(builder);} return (A)this;
}
public A addAllToPlayers(Collection items){
if (this.players == null) {this.players = new ArrayList();}
for (Person item : items) {PersonBuilder builder = new PersonBuilder(item);_visitables.get("players").add(builder);this.players.add(builder);} return (A)this;
}
public A removeFromPlayers(Person... items){
for (Person item : items) {PersonBuilder builder = new PersonBuilder(item);_visitables.get("players").remove(builder);if (this.players != null) {this.players.remove(builder);}} return (A)this;
}
public A removeAllFromPlayers(Collection items){
for (Person item : items) {PersonBuilder builder = new PersonBuilder(item);_visitables.get("players").remove(builder);if (this.players != null) {this.players.remove(builder);}} return (A)this;
}
public Boolean hasPlayers(){
return players != null && !players.isEmpty();
}
public A addNewPlayer(String firstName,String lastName,int[] numbers){
return (A)addToPlayers(new Person(firstName, lastName, numbers));
}
public GameFluent.PlayersNested addNewPlayer(){
return new PlayersNestedImpl();
}
public GameFluent.PlayersNested addNewPlayerLike(Person item){
return new PlayersNestedImpl(-1, item);
}
public GameFluent.PlayersNested setNewPlayerLike(int index,Person item){
return new PlayersNestedImpl(index, item);
}
public GameFluent.PlayersNested editPlayer(int index){
if (players.size() <= index) throw new RuntimeException("Can't edit players. Index exceeds size.");
return setNewPlayerLike(index, buildPlayer(index));
}
public GameFluent.PlayersNested editFirstPlayer(){
if (players.size() == 0) throw new RuntimeException("Can't edit first players. The list is empty.");
return setNewPlayerLike(0, buildPlayer(0));
}
public GameFluent.PlayersNested editLastPlayer(){
int index = players.size() - 1;
if (index < 0) throw new RuntimeException("Can't edit last players. The list is empty.");
return setNewPlayerLike(index, buildPlayer(index));
}
public GameFluent.PlayersNested editMatchingPlayer(Predicate predicate){
int index = -1;
for (int i=0;i extends PersonFluentImpl> implements GameFluent.PlayersNested,Nested{
private final PersonBuilder builder;
private final int index;
PlayersNestedImpl(int index,Person item){
this.index = index;
this.builder = new PersonBuilder(this, item);
}
PlayersNestedImpl(){
this.index = -1;
this.builder = new PersonBuilder(this);
}
public N and(){
return (N) GameFluentImpl.this.setToPlayers(index, builder.build());
}
public N endPlayer(){
return and();
}
}
}