Skip to content

Commit

Permalink
Merge pull request #431 from VarunVats9/master
Browse files Browse the repository at this point in the history
Renamed HeroBuilder to Builder
  • Loading branch information
iluwatar committed Jun 5, 2016
2 parents f6649a4 + 509de97 commit 34431d7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion builder/etc/builder.ucls
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</enumeration>
<class id="4" language="java" name="com.iluwatar.builder.Hero.HeroBuilder" project="builder"
<class id="4" language="java" name="com.iluwatar.builder.Hero.Builder" project="builder"
file="/builder/src/main/java/com/iluwatar/builder/Hero.java" binary="false" corner="BOTTOM_RIGHT">
<position height="196" width="234" x="20" y="183"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
Expand Down
12 changes: 6 additions & 6 deletions builder/src/main/java/com/iluwatar/builder/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
package com.iluwatar.builder;

import com.iluwatar.builder.Hero.HeroBuilder;
import com.iluwatar.builder.Hero.Builder;

/**
*
Expand All @@ -41,10 +41,10 @@
* Java 2nd Edition.
* <p>
* We want to build {@link Hero} objects, but its construction is complex because of the many
* parameters needed. To aid the user we introduce {@link HeroBuilder} class. {@link HeroBuilder}
* parameters needed. To aid the user we introduce {@link Builder} class. {@link Hero.Builder}
* takes the minimum parameters to build {@link Hero} object in its constructor. After that
* additional configuration for the {@link Hero} object can be done using the fluent
* {@link HeroBuilder} interface. When configuration is ready the build method is called to receive
* {@link Builder} interface. When configuration is ready the build method is called to receive
* the final {@link Hero} object.
*
*/
Expand All @@ -58,18 +58,18 @@ public class App {
public static void main(String[] args) {

Hero mage =
new HeroBuilder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK)
new Hero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK)
.withWeapon(Weapon.DAGGER).build();
System.out.println(mage);

Hero warrior =
new HeroBuilder(Profession.WARRIOR, "Amberjill").withHairColor(HairColor.BLOND)
new Hero.Builder(Profession.WARRIOR, "Amberjill").withHairColor(HairColor.BLOND)
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL).withWeapon(Weapon.SWORD)
.build();
System.out.println(warrior);

Hero thief =
new HeroBuilder(Profession.THIEF, "Desmond").withHairType(HairType.BALD)
new Hero.Builder(Profession.THIEF, "Desmond").withHairType(HairType.BALD)
.withWeapon(Weapon.BOW).build();
System.out.println(thief);

Expand Down
14 changes: 7 additions & 7 deletions builder/src/main/java/com/iluwatar/builder/Hero.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class Hero {
private final Armor armor;
private final Weapon weapon;

private Hero(HeroBuilder builder) {
private Hero(Builder builder) {
this.profession = builder.profession;
this.name = builder.name;
this.hairColor = builder.hairColor;
Expand Down Expand Up @@ -102,7 +102,7 @@ public String toString() {
* The builder class.
*
*/
public static class HeroBuilder {
public static class Builder {

private final Profession profession;
private final String name;
Expand All @@ -114,30 +114,30 @@ public static class HeroBuilder {
/**
* Constructor
*/
public HeroBuilder(Profession profession, String name) {
public Builder(Profession profession, String name) {
if (profession == null || name == null) {
throw new IllegalArgumentException("profession and name can not be null");
}
this.profession = profession;
this.name = name;
}

public HeroBuilder withHairType(HairType hairType) {
public Builder withHairType(HairType hairType) {
this.hairType = hairType;
return this;
}

public HeroBuilder withHairColor(HairColor hairColor) {
public Builder withHairColor(HairColor hairColor) {
this.hairColor = hairColor;
return this;
}

public HeroBuilder withArmor(Armor armor) {
public Builder withArmor(Armor armor) {
this.armor = armor;
return this;
}

public HeroBuilder withWeapon(Weapon weapon) {
public Builder withWeapon(Weapon weapon) {
this.weapon = weapon;
return this;
}
Expand Down
6 changes: 3 additions & 3 deletions builder/src/test/java/com/iluwatar/builder/HeroTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public class HeroTest {
*/
@Test(expected = IllegalArgumentException.class)
public void testMissingProfession() throws Exception {
new Hero.HeroBuilder(null, "Sir without a job");
new Hero.Builder(null, "Sir without a job");
}

/**
* Test if we get the expected exception when trying to create a hero without a name
*/
@Test(expected = IllegalArgumentException.class)
public void testMissingName() throws Exception {
new Hero.HeroBuilder(Profession.THIEF, null);
new Hero.Builder(Profession.THIEF, null);
}

/**
Expand All @@ -57,7 +57,7 @@ public void testMissingName() throws Exception {
public void testBuildHero() throws Exception {
final String heroName = "Sir Lancelot";

final Hero hero = new Hero.HeroBuilder(Profession.WARRIOR, heroName)
final Hero hero = new Hero.Builder(Profession.WARRIOR, heroName)
.withArmor(Armor.CHAIN_MAIL)
.withWeapon(Weapon.SWORD)
.withHairType(HairType.LONG_CURLY)
Expand Down

0 comments on commit 34431d7

Please sign in to comment.