Skip to content

Commit c7a4a85

Browse files
committed
Merge pull request iluwatar#29 from ruslanpa/master
[refactor] Makes enums more readable.
2 parents 67c1125 + 6da9686 commit c7a4a85

File tree

9 files changed

+57
-98
lines changed

9 files changed

+57
-98
lines changed

builder/src/main/java/com/iluwatar/Armor.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
public enum Armor {
44

5-
CLOTHES, LEATHER, CHAIN_MAIL, PLATE_MAIL;
5+
CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");
66

7-
@Override
7+
private String title;
8+
9+
Armor(String title) {
10+
this.title = title;
11+
}
12+
13+
@Override
814
public String toString() {
9-
return name().toLowerCase().replaceAll("_", " ");
15+
return title;
1016
}
11-
1217
}
Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
package com.iluwatar;
22

33
/**
4-
*
4+
*
55
* Enumeration for target size.
66
*
77
*/
88
public enum Size {
99

10-
SMALL, NORMAL, LARGE;
11-
12-
@Override
13-
public String toString() {
10+
SMALL("small"), NORMAL("normal"), LARGE("large"), UNDEFINED("");
11+
12+
private String title;
1413

15-
String s = "";
14+
Size(String title) {
15+
this.title = title;
16+
}
1617

17-
switch (this) {
18-
case LARGE:
19-
s = "large";
20-
break;
21-
case NORMAL:
22-
s = "normal";
23-
break;
24-
case SMALL:
25-
s = "small";
26-
break;
27-
default:
28-
break;
29-
}
30-
return s;
18+
@Override
19+
public String toString() {
20+
return title;
3121
}
3222
}

command/src/main/java/com/iluwatar/Visibility.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,16 @@
77
*/
88
public enum Visibility {
99

10-
VISIBLE, INVISIBLE;
10+
VISIBLE("visible"), INVISIBLE("invisible"), UNDEFINED("");
1111

12-
@Override
13-
public String toString() {
14-
15-
String s = "";
12+
private String title;
1613

17-
switch (this) {
18-
case INVISIBLE:
19-
s = "invisible";
20-
break;
21-
case VISIBLE:
22-
s = "visible";
23-
break;
24-
default:
25-
break;
14+
Visibility(String title) {
15+
this.title = title;
16+
}
2617

27-
}
28-
return s;
18+
@Override
19+
public String toString() {
20+
return title;
2921
}
3022
}

factory-method/src/main/java/com/iluwatar/WeaponType.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,16 @@
22

33
public enum WeaponType {
44

5-
SHORT_SWORD, SPEAR, AXE;
5+
SHORT_SWORD("short sword"), SPEAR("spear"), AXE("axe"), UNDEFINED("");
66

7-
@Override
7+
private String title;
8+
9+
WeaponType(String title) {
10+
this.title = title;
11+
}
12+
13+
@Override
814
public String toString() {
9-
String s = "";
10-
switch (this) {
11-
case SHORT_SWORD:
12-
s = "short sword";
13-
break;
14-
case SPEAR:
15-
s = "spear";
16-
break;
17-
case AXE:
18-
s = "axe";
19-
break;
20-
}
21-
return s;
15+
return title;
2216
}
23-
2417
}

flyweight/src/main/java/com/iluwatar/Potion.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
*/
88
public interface Potion {
99

10-
public void drink();
11-
10+
void drink();
1211
}

flyweight/src/main/java/com/iluwatar/PotionFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.iluwatar;
22

33
import java.util.EnumMap;
4+
import java.util.Map;
45

56
/**
67
*
@@ -12,7 +13,7 @@
1213
*/
1314
public class PotionFactory {
1415

15-
private EnumMap<PotionType, Potion> potions;
16+
private final Map<PotionType, Potion> potions;
1617

1718
public PotionFactory() {
1819
potions = new EnumMap<>(PotionType.class);

flyweight/src/main/java/com/iluwatar/PotionType.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
*/
88
public enum PotionType {
99

10-
HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON;
11-
10+
HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON
1211
}

mediator/src/main/java/com/iluwatar/Action.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@
77
*/
88
public enum Action {
99

10-
HUNT, TALE, GOLD, ENEMY;
10+
HUNT("hunted a rabbit"), TALE("tells a tale"), GOLD("found gold"), ENEMY("spotted enemies"), NONE("");
1111

12-
public String toString() {
12+
private String title;
1313

14-
switch (this) {
15-
case ENEMY:
16-
return "spotted enemies";
17-
case GOLD:
18-
return "found gold";
19-
case HUNT:
20-
return "hunted a rabbit";
21-
case TALE:
22-
return "tells a tale";
23-
}
24-
return "";
14+
Action(String title) {
15+
this.title = title;
16+
}
17+
18+
public String toString() {
19+
return title;
2520
}
2621
}

memento/src/main/java/com/iluwatar/StarType.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,16 @@
22

33
public enum StarType {
44

5-
SUN, RED_GIANT, WHITE_DWARF, SUPERNOVA, DEAD;
5+
SUN("sun"), RED_GIANT("red giant"), WHITE_DWARF("white dwarf"), SUPERNOVA("supernova"), DEAD("dead star"), UNDEFINED("");
66

7-
@Override
7+
private String title;
8+
9+
StarType(String title) {
10+
this.title = title;
11+
}
12+
13+
@Override
814
public String toString() {
9-
String s = "";
10-
switch (this) {
11-
case RED_GIANT:
12-
s = "red giant";
13-
break;
14-
case SUN:
15-
s = "sun";
16-
break;
17-
case SUPERNOVA:
18-
s = "supernova";
19-
break;
20-
case WHITE_DWARF:
21-
s = "white dwarf";
22-
break;
23-
case DEAD:
24-
s = "dead star";
25-
break;
26-
default:
27-
break;
28-
}
29-
return s;
15+
return title;
3016
}
31-
3217
}

0 commit comments

Comments
 (0)