Fixes HeroBuilder NullPointerException #35.
This commit is contained in:
parent
dd855a376b
commit
62b871774a
@ -1,128 +1,128 @@
|
|||||||
package com.iluwatar;
|
package com.iluwatar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The class with many parameters.
|
* The class with many parameters.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Hero {
|
public class Hero {
|
||||||
|
|
||||||
private final Profession profession;
|
private final Profession profession;
|
||||||
private final String name;
|
private final String name;
|
||||||
private final HairType hairType;
|
private final HairType hairType;
|
||||||
private final HairColor hairColor;
|
private final HairColor hairColor;
|
||||||
private final Armor armor;
|
private final Armor armor;
|
||||||
private final Weapon weapon;
|
private final Weapon weapon;
|
||||||
|
|
||||||
public Profession getProfession() {
|
public Profession getProfession() {
|
||||||
return profession;
|
return profession;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HairType getHairType() {
|
public HairType getHairType() {
|
||||||
return hairType;
|
return hairType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HairColor getHairColor() {
|
public HairColor getHairColor() {
|
||||||
return hairColor;
|
return hairColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Armor getArmor() {
|
public Armor getArmor() {
|
||||||
return armor;
|
return armor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Weapon getWeapon() {
|
public Weapon getWeapon() {
|
||||||
return weapon;
|
return weapon;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(profession);
|
sb.append(profession);
|
||||||
sb.append(" named ");
|
sb.append(" named ");
|
||||||
sb.append(name);
|
sb.append(name);
|
||||||
if (hairColor != null || hairType != null) {
|
if (hairColor != null || hairType != null) {
|
||||||
sb.append(" with ");
|
sb.append(" with ");
|
||||||
if (hairColor != null) {
|
if (hairColor != null) {
|
||||||
sb.append(hairColor);
|
sb.append(hairColor);
|
||||||
sb.append(" ");
|
sb.append(" ");
|
||||||
}
|
}
|
||||||
if (hairType != null) {
|
if (hairType != null) {
|
||||||
sb.append(hairType);
|
sb.append(hairType);
|
||||||
sb.append(" ");
|
sb.append(" ");
|
||||||
}
|
}
|
||||||
sb.append(hairType != HairType.BALD ? "hair" : "head");
|
sb.append(hairType != HairType.BALD ? "hair" : "head");
|
||||||
}
|
}
|
||||||
if (armor != null) {
|
if (armor != null) {
|
||||||
sb.append(" wearing ");
|
sb.append(" wearing ");
|
||||||
sb.append(armor);
|
sb.append(armor);
|
||||||
}
|
}
|
||||||
if (weapon != null) {
|
if (weapon != null) {
|
||||||
sb.append(" and wielding ");
|
sb.append(" and wielding ");
|
||||||
sb.append(weapon);
|
sb.append(weapon);
|
||||||
}
|
}
|
||||||
sb.append(".");
|
sb.append(".");
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Hero(HeroBuilder builder) {
|
private Hero(HeroBuilder builder) {
|
||||||
this.profession = builder.profession;
|
this.profession = builder.profession;
|
||||||
this.name = builder.name;
|
this.name = builder.name;
|
||||||
this.hairColor = builder.hairColor;
|
this.hairColor = builder.hairColor;
|
||||||
this.hairType = builder.hairType;
|
this.hairType = builder.hairType;
|
||||||
this.weapon = builder.weapon;
|
this.weapon = builder.weapon;
|
||||||
this.armor = builder.armor;
|
this.armor = builder.armor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The builder class.
|
* The builder class.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static class HeroBuilder {
|
public static class HeroBuilder {
|
||||||
|
|
||||||
private final Profession profession;
|
private final Profession profession;
|
||||||
private final String name;
|
private final String name;
|
||||||
private HairType hairType;
|
private HairType hairType;
|
||||||
private HairColor hairColor;
|
private HairColor hairColor;
|
||||||
private Armor armor;
|
private Armor armor;
|
||||||
private Weapon weapon;
|
private Weapon weapon;
|
||||||
|
|
||||||
public HeroBuilder(Profession profession, String name) {
|
public HeroBuilder(Profession profession, String name) {
|
||||||
if (profession == null || name == null) {
|
if (profession == null || name == null) {
|
||||||
throw new NullPointerException(
|
throw new IllegalArgumentException(
|
||||||
"profession and name can not be null");
|
"profession and name can not be null");
|
||||||
}
|
}
|
||||||
this.profession = profession;
|
this.profession = profession;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroBuilder withHairType(HairType hairType) {
|
public HeroBuilder withHairType(HairType hairType) {
|
||||||
this.hairType = hairType;
|
this.hairType = hairType;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroBuilder withHairColor(HairColor hairColor) {
|
public HeroBuilder withHairColor(HairColor hairColor) {
|
||||||
this.hairColor = hairColor;
|
this.hairColor = hairColor;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroBuilder withArmor(Armor armor) {
|
public HeroBuilder withArmor(Armor armor) {
|
||||||
this.armor = armor;
|
this.armor = armor;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeroBuilder withWeapon(Weapon weapon) {
|
public HeroBuilder withWeapon(Weapon weapon) {
|
||||||
this.weapon = weapon;
|
this.weapon = weapon;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Hero build() {
|
public Hero build() {
|
||||||
return new Hero(this);
|
return new Hero(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user