Skip to content

Commit 62b8717

Browse files
committed
Fixes HeroBuilder NullPointerException iluwatar#35.
1 parent dd855a3 commit 62b8717

File tree

1 file changed

+128
-128
lines changed

1 file changed

+128
-128
lines changed
Lines changed: 128 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,128 @@
1-
package com.iluwatar;
2-
3-
/**
4-
*
5-
* The class with many parameters.
6-
*
7-
*/
8-
public class Hero {
9-
10-
private final Profession profession;
11-
private final String name;
12-
private final HairType hairType;
13-
private final HairColor hairColor;
14-
private final Armor armor;
15-
private final Weapon weapon;
16-
17-
public Profession getProfession() {
18-
return profession;
19-
}
20-
21-
public String getName() {
22-
return name;
23-
}
24-
25-
public HairType getHairType() {
26-
return hairType;
27-
}
28-
29-
public HairColor getHairColor() {
30-
return hairColor;
31-
}
32-
33-
public Armor getArmor() {
34-
return armor;
35-
}
36-
37-
public Weapon getWeapon() {
38-
return weapon;
39-
}
40-
41-
@Override
42-
public String toString() {
43-
44-
StringBuilder sb = new StringBuilder();
45-
sb.append(profession);
46-
sb.append(" named ");
47-
sb.append(name);
48-
if (hairColor != null || hairType != null) {
49-
sb.append(" with ");
50-
if (hairColor != null) {
51-
sb.append(hairColor);
52-
sb.append(" ");
53-
}
54-
if (hairType != null) {
55-
sb.append(hairType);
56-
sb.append(" ");
57-
}
58-
sb.append(hairType != HairType.BALD ? "hair" : "head");
59-
}
60-
if (armor != null) {
61-
sb.append(" wearing ");
62-
sb.append(armor);
63-
}
64-
if (weapon != null) {
65-
sb.append(" and wielding ");
66-
sb.append(weapon);
67-
}
68-
sb.append(".");
69-
return sb.toString();
70-
}
71-
72-
private Hero(HeroBuilder builder) {
73-
this.profession = builder.profession;
74-
this.name = builder.name;
75-
this.hairColor = builder.hairColor;
76-
this.hairType = builder.hairType;
77-
this.weapon = builder.weapon;
78-
this.armor = builder.armor;
79-
}
80-
81-
/**
82-
*
83-
* The builder class.
84-
*
85-
*/
86-
public static class HeroBuilder {
87-
88-
private final Profession profession;
89-
private final String name;
90-
private HairType hairType;
91-
private HairColor hairColor;
92-
private Armor armor;
93-
private Weapon weapon;
94-
95-
public HeroBuilder(Profession profession, String name) {
96-
if (profession == null || name == null) {
97-
throw new NullPointerException(
98-
"profession and name can not be null");
99-
}
100-
this.profession = profession;
101-
this.name = name;
102-
}
103-
104-
public HeroBuilder withHairType(HairType hairType) {
105-
this.hairType = hairType;
106-
return this;
107-
}
108-
109-
public HeroBuilder withHairColor(HairColor hairColor) {
110-
this.hairColor = hairColor;
111-
return this;
112-
}
113-
114-
public HeroBuilder withArmor(Armor armor) {
115-
this.armor = armor;
116-
return this;
117-
}
118-
119-
public HeroBuilder withWeapon(Weapon weapon) {
120-
this.weapon = weapon;
121-
return this;
122-
}
123-
124-
public Hero build() {
125-
return new Hero(this);
126-
}
127-
}
128-
}
1+
package com.iluwatar;
2+
3+
/**
4+
*
5+
* The class with many parameters.
6+
*
7+
*/
8+
public class Hero {
9+
10+
private final Profession profession;
11+
private final String name;
12+
private final HairType hairType;
13+
private final HairColor hairColor;
14+
private final Armor armor;
15+
private final Weapon weapon;
16+
17+
public Profession getProfession() {
18+
return profession;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public HairType getHairType() {
26+
return hairType;
27+
}
28+
29+
public HairColor getHairColor() {
30+
return hairColor;
31+
}
32+
33+
public Armor getArmor() {
34+
return armor;
35+
}
36+
37+
public Weapon getWeapon() {
38+
return weapon;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
44+
StringBuilder sb = new StringBuilder();
45+
sb.append(profession);
46+
sb.append(" named ");
47+
sb.append(name);
48+
if (hairColor != null || hairType != null) {
49+
sb.append(" with ");
50+
if (hairColor != null) {
51+
sb.append(hairColor);
52+
sb.append(" ");
53+
}
54+
if (hairType != null) {
55+
sb.append(hairType);
56+
sb.append(" ");
57+
}
58+
sb.append(hairType != HairType.BALD ? "hair" : "head");
59+
}
60+
if (armor != null) {
61+
sb.append(" wearing ");
62+
sb.append(armor);
63+
}
64+
if (weapon != null) {
65+
sb.append(" and wielding ");
66+
sb.append(weapon);
67+
}
68+
sb.append(".");
69+
return sb.toString();
70+
}
71+
72+
private Hero(HeroBuilder builder) {
73+
this.profession = builder.profession;
74+
this.name = builder.name;
75+
this.hairColor = builder.hairColor;
76+
this.hairType = builder.hairType;
77+
this.weapon = builder.weapon;
78+
this.armor = builder.armor;
79+
}
80+
81+
/**
82+
*
83+
* The builder class.
84+
*
85+
*/
86+
public static class HeroBuilder {
87+
88+
private final Profession profession;
89+
private final String name;
90+
private HairType hairType;
91+
private HairColor hairColor;
92+
private Armor armor;
93+
private Weapon weapon;
94+
95+
public HeroBuilder(Profession profession, String name) {
96+
if (profession == null || name == null) {
97+
throw new IllegalArgumentException(
98+
"profession and name can not be null");
99+
}
100+
this.profession = profession;
101+
this.name = name;
102+
}
103+
104+
public HeroBuilder withHairType(HairType hairType) {
105+
this.hairType = hairType;
106+
return this;
107+
}
108+
109+
public HeroBuilder withHairColor(HairColor hairColor) {
110+
this.hairColor = hairColor;
111+
return this;
112+
}
113+
114+
public HeroBuilder withArmor(Armor armor) {
115+
this.armor = armor;
116+
return this;
117+
}
118+
119+
public HeroBuilder withWeapon(Weapon weapon) {
120+
this.weapon = weapon;
121+
return this;
122+
}
123+
124+
public Hero build() {
125+
return new Hero(this);
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)