Formatted all files to the same standard

This commit is contained in:
matthew
2014-10-08 13:42:12 +01:00
parent 53a2a8b150
commit 3da9ad5469
151 changed files with 952 additions and 870 deletions

View File

@ -3,41 +3,36 @@ package com.iluwatar;
import com.iluwatar.Hero.HeroBuilder;
/**
*
*
* This is the Builder pattern variation as described by Joshua Bloch in
* Effective Java 2nd Edition.
*
*
* We want to build Hero objects, but its construction is complex because of the
* many parameters needed. To aid the user we introduce HeroBuilder class.
* HeroBuilder takes the minimum parameters to build Hero object in its
* constructor. After that additional configuration for the Hero object can be
* done using the fluent HeroBuilder interface. When configuration is ready the
* build method is called to receive the final Hero object.
*
*
*/
public class App {
public static void main(String[] args) {
public static void main(String[] args) {
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
.withHairColor(HairColor.BLACK)
.withWeapon(Weapon.DAGGER)
.build();
System.out.println(mage);
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
.withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER)
.build();
System.out.println(mage);
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
.withHairColor(HairColor.BLOND)
.withHairType(HairType.LONG_CURLY)
.withArmor(Armor.CHAIN_MAIL)
.withWeapon(Weapon.SWORD)
.build();
System.out.println(warrior);
Hero warrior = new HeroBuilder(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)
.withWeapon(Weapon.BOW)
.build();
System.out.println(thief);
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
.withHairType(HairType.BALD).withWeapon(Weapon.BOW).build();
System.out.println(thief);
}
}
}

View File

@ -3,17 +3,25 @@ package com.iluwatar;
public enum Armor {
CLOTHES, LEATHER, CHAIN_MAIL, PLATE_MAIL;
@Override
public String toString() {
String s = "";
switch(this) {
case CLOTHES: s = "clothes"; break;
case LEATHER: s = "leather armor"; break;
case CHAIN_MAIL: s = "chain mail"; break;
case PLATE_MAIL: s = "plate mail"; break;
switch (this) {
case CLOTHES:
s = "clothes";
break;
case LEATHER:
s = "leather armor";
break;
case CHAIN_MAIL:
s = "chain mail";
break;
case PLATE_MAIL:
s = "plate mail";
break;
}
return s;
}
}

View File

@ -3,18 +3,28 @@ package com.iluwatar;
public enum HairColor {
WHITE, BLOND, RED, BROWN, BLACK;
@Override
public String toString() {
String s = "";
switch(this) {
case WHITE: s = "white"; break;
case BLOND: s = "blond"; break;
case RED: s = "red"; break;
case BROWN: s = "brown"; break;
case BLACK: s = "black"; break;
switch (this) {
case WHITE:
s = "white";
break;
case BLOND:
s = "blond";
break;
case RED:
s = "red";
break;
case BROWN:
s = "brown";
break;
case BLACK:
s = "black";
break;
}
return s;
}
}

View File

@ -7,14 +7,24 @@ public enum HairType {
@Override
public String toString() {
String s = "";
switch(this) {
case BALD: s = "bold"; break;
case SHORT: s = "short"; break;
case CURLY: s = "curly"; break;
case LONG_STRAIGHT: s = "long straight"; break;
case LONG_CURLY: s = "long curly"; break;
switch (this) {
case BALD:
s = "bold";
break;
case SHORT:
s = "short";
break;
case CURLY:
s = "curly";
break;
case LONG_STRAIGHT:
s = "long straight";
break;
case LONG_CURLY:
s = "long curly";
break;
}
return s;
}
}

View File

@ -3,7 +3,7 @@ package com.iluwatar;
/**
*
* The class with many parameters.
*
*
*/
public class Hero {
@ -13,7 +13,7 @@ public class Hero {
private final HairColor hairColor;
private final Armor armor;
private final Weapon weapon;
public Profession getProfession() {
return profession;
}
@ -37,7 +37,7 @@ public class Hero {
public Weapon getWeapon() {
return weapon;
}
@Override
public String toString() {
@ -81,7 +81,7 @@ public class Hero {
/**
*
* The builder class.
*
*
*/
public static class HeroBuilder {
@ -91,20 +91,21 @@ public class Hero {
private HairColor hairColor;
private Armor armor;
private Weapon weapon;
public HeroBuilder(Profession profession, String name) {
if (profession == null || name == null) {
throw new NullPointerException("profession and name can not be null");
throw new NullPointerException(
"profession and name can not be null");
}
this.profession = profession;
this.name = name;
}
public HeroBuilder withHairType(HairType hairType) {
this.hairType = hairType;
return this;
}
public HeroBuilder withHairColor(HairColor hairColor) {
this.hairColor = hairColor;
return this;
@ -114,12 +115,12 @@ public class Hero {
this.armor = armor;
return this;
}
public HeroBuilder withWeapon(Weapon weapon) {
this.weapon = weapon;
return this;
}
public Hero build() {
return new Hero(this);
}

View File

@ -1,19 +1,27 @@
package com.iluwatar;
public enum Profession {
WARRIOR, THIEF, MAGE, PRIEST;
@Override
public String toString() {
String s = "";
switch(this) {
case WARRIOR: s = "Warrior"; break;
case THIEF: s = "Thief"; break;
case MAGE: s = "Mage"; break;
case PRIEST: s = "Priest"; break;
switch (this) {
case WARRIOR:
s = "Warrior";
break;
case THIEF:
s = "Thief";
break;
case MAGE:
s = "Mage";
break;
case PRIEST:
s = "Priest";
break;
}
return s;
}
}

View File

@ -3,18 +3,28 @@ package com.iluwatar;
public enum Weapon {
DAGGER, SWORD, AXE, WARHAMMER, BOW;
@Override
public String toString() {
String s = "";
switch(this) {
case DAGGER: s = "dagger"; break;
case SWORD: s = "sword"; break;
case AXE: s = "axe"; break;
case WARHAMMER: s = "warhammer"; break;
case BOW: s = "bow"; break;
switch (this) {
case DAGGER:
s = "dagger";
break;
case SWORD:
s = "sword";
break;
case AXE:
s = "axe";
break;
case WARHAMMER:
s = "warhammer";
break;
case BOW:
s = "bow";
break;
}
return s;
}
}