Skip to content

Commit d3eb8a2

Browse files
committed
Added comments in the code. modified index.md
1 parent 083065b commit d3eb8a2

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

value-object/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ Use the Value Object when
2323

2424
## Real world examples
2525

26-
* [java.util.Date](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html)
26+
* [java.util.Optional](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html)
27+
* [java.time.LocalDate](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html)
2728
* [joda-time, money, beans](http://www.joda.org/)
28-
* [JSR-310 / ThreeTen project LocalDate](http://www.threeten.org/articles/local-date.html)
2929

3030
## Credits
3131

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package com.iluwatar.value.object;
22

33
/**
4-
* Hello world!.
4+
* App Class.
55
*
66
*/
77
public class App {
8+
/**
9+
* main method.
10+
*/
811
public static void main(String[] args) {
9-
HeroStat stat = HeroStat.valueOf(10, 5, 0);
10-
System.out.println(stat.toString());
12+
HeroStat statA = HeroStat.valueOf(10, 5, 0);
13+
HeroStat statB = HeroStat.valueOf(5, 1, 8);
14+
15+
System.out.println(statA.toString());
16+
// When using Value Objects do not use ==, only compare using equals().
17+
System.out.println("is statA and statB equal : " + statA.equals(statB));
1118
}
1219
}

value-object/src/main/java/com/iluwatar/value/object/HeroStat.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package com.iluwatar.value.object;
22

33
/**
4-
* The Discount Coupon only discounts by percentage.
5-
*
4+
* HeroStat is a Value Object. following rules are from Stephen Colebourne's term VALJO(not the
5+
* entire rule set) from : http://blog.joda.org/2014/03/valjos-value-java-objects.html<br>
6+
* Value Objects must override equals(), hashCode() to check the equality with values. <br>
7+
* Value Objects should be immutable so declare members final. Obtain instances by static factory
8+
* methods. <br>
9+
* The elements of the state must be other values, including primitive types.<br>
10+
* Provide methods, typically simple getters, to get the elements of the state.<br>
11+
*
12+
* {@link http://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html}
613
*/
714
public class HeroStat {
815

916

10-
// stats for a hero
17+
// Stats for a hero
1118

1219
private final int strength;
1320
private final int intelligence;
@@ -22,6 +29,7 @@ private HeroStat(int strength, int intelligence, int luck) {
2229
this.luck = luck;
2330
}
2431

32+
// Static factory method to create new instances.
2533
public static HeroStat valueOf(int strength, int intelligence, int luck) {
2634
return new HeroStat(strength, intelligence, luck);
2735
}
@@ -39,7 +47,7 @@ public int getLuck() {
3947
}
4048

4149
/*
42-
* recommended to provide a static factory method capable of creating an instance from the formal
50+
* Recommended to provide a static factory method capable of creating an instance from the formal
4351
* string representation declared like this. public static Juice parse(String string) {}
4452
*/
4553

@@ -86,6 +94,6 @@ public boolean equals(Object obj) {
8694
}
8795

8896

89-
// the clone() method should not be public
97+
// The clone() method should not be public
9098

9199
}

value-object/src/test/java/com/iluwatar/value/object/HeroStatTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar.value.object;
22

3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.hamcrest.CoreMatchers.not;
5+
6+
import static org.junit.Assert.assertThat;
7+
38
import com.google.common.testing.EqualsTester;
49

510
import org.junit.Test;
@@ -11,6 +16,7 @@ public class HeroStatTest {
1116

1217
/**
1318
* Tester for equals() and hashCode() methods of a class.
19+
*
1420
* @see http://www.javadoc.io/doc/com.google.guava/guava-testlib/19.0
1521
*/
1622
@Test
@@ -20,4 +26,21 @@ public void testEquals() {
2026
new EqualsTester().addEqualityGroup(heroStatA, heroStatB).testEquals();
2127
}
2228

29+
/**
30+
* The toString() for two equal values must be the same. For two non-equal values it must be
31+
* different.
32+
*/
33+
@Test
34+
public void testToString() {
35+
36+
HeroStat heroStatA = HeroStat.valueOf(3, 9, 2);
37+
HeroStat heroStatB = HeroStat.valueOf(3, 9, 2);
38+
HeroStat heroStatC = HeroStat.valueOf(3, 9, 8);
39+
40+
assertThat(heroStatA.toString(), is(heroStatB.toString()));
41+
assertThat(heroStatA.toString(), is(not(heroStatC.toString())));
42+
43+
44+
}
45+
2346
}

0 commit comments

Comments
 (0)