com.danielflower.crickam.scorer.Player Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cricket-scorer Show documentation
Show all versions of cricket-scorer Show documentation
An immutable java library for keeping score of cricket matches
package com.danielflower.crickam.scorer;
import java.util.Objects;
import java.util.stream.Collectors;
import static java.util.Objects.requireNonNull;
/**
* A cricket player
* To create players, use {@link #player()} to get a new {@link Builder}.
* This class is designed to be inherited if you wish to add custom data to the model.
*/
public class Player {
private final String familyName;
private final String givenName;
private final ImmutableList formalGivenNames;
private final String fullName;
private final String initials;
protected Player(Builder builder) {
Objects.requireNonNullElse(builder.givenName, builder.formalGivenNames);
this.formalGivenNames = builder.formalGivenNames != null ? builder.formalGivenNames : ImmutableList.of(builder.givenName);
this.givenName = builder.givenName != null ? builder.givenName : builder.formalGivenNames.get(0);
this.fullName = builder.fullName != null ? builder.fullName : givenName + " " + builder.familyName;
this.initials = builder.initials != null ? builder.initials : builder.formalGivenNames.stream().map(s -> String.valueOf(s.charAt(0))).collect(Collectors.joining());
this.familyName = requireNonNull(builder.familyName);
}
public String toString() {
return initialsWithSurname();
}
public String givenName() {
return givenName;
}
public ImmutableList formalGivenNames() {
return formalGivenNames;
}
public String formalName() {
return formalGivenNames.stream().collect(Collectors.joining(" ")) + " " + familyName();
}
public String familyName() {
return familyName;
}
public String fullName() {
return fullName;
}
public String initialsWithSurname() {
return initials + " " + familyName;
}
public String initials() {
return initials;
}
/**
* Creates a player builder. Consider using {@link #player(String)} instead.
* @return A player builder.
*/
public static Builder player() {
return new Builder();
}
/**
* @param name The full name of the player, such as "Luteru Ross Poutoa Lote Taylor"
* @return A player builder formal given names and family name set.
*/
public static Builder player(String name) {
return player(name.split("\\s+"));
}
/**
* @param names The full name of the player, such as "Luteru", "Ross", "Poutoa", "Lote", "Taylor"
* @return A player builder formal given names and family name set.
*/
public static Builder player(String... names) {
if (names.length < 2) {
throw new IllegalArgumentException("Names should have at least two items");
}
ImmutableList list = ImmutableList.of(names);
Builder builder = player();
int size = list.size();
if (size >= 4 && list.get(size - 3).equals("van") && list.get(size - 2).equals("der")) {
list = list.subList(0, size - 4).add(list.get(size - 3) + " " + list.get(size - 2) + " " + list.get(size - 1));
} else if (size >= 3 && list.get(size - 2).matches("d[aeiou]")) {
list = list.subList(0, size - 3).add(list.get(size - 2) + " " + list.get(size - 1));
}
return builder
.withFormalGivenNames(list.removeLast())
.withFamilyName(list.last().orElseThrow());
}
/**
* A builder of {@link Player} objects.
*/
public static class Builder {
private ImmutableList formalGivenNames;
private String givenName;
private String familyName;
private String fullName;
private String initials;
/**
* @param formalGivenNames The formal given names of the player, such as "Luteru", "Ross", "Poutoa", "Lote"
for Ross Taylor.
* @return This builder
*/
public Builder withFormalGivenNames(ImmutableList formalGivenNames) {
this.formalGivenNames = formalGivenNames;
return this;
}
/**
* @param familyName The family name of the player, for example "Taylor" for Ross Taylor.
* @return This builder
*/
public Builder withFamilyName(String familyName) {
this.familyName = familyName;
return this;
}
/**
* Leave unset to infer this from the given name and full name.
* @param fullName The name this player is known as, for example "Ross Taylor"
* @return This builder
*/
public Builder withFullName(String fullName) {
this.fullName = fullName;
return this;
}
/**
* @param givenName The given name that this person is generally known as, for example "Ross" for Luteru Ross Poutoa Lote Taylor
* @return This builder
*/
public Builder withGivenName(String givenName) {
this.givenName = givenName;
return this;
}
/**
* @param initials The initials of the player's formal given names. This is inferred if unset.
* @return This builder
*/
public Builder withInitials(String initials) {
this.initials = initials;
return this;
}
public Player build() {
return new Player(this);
}
}
}