From d3eb8a2ef257bae97a49d9c91f48e96eca6a1cb9 Mon Sep 17 00:00:00 2001 From: JuhoKang Date: Fri, 29 Jan 2016 00:53:27 +0900 Subject: [PATCH] Added comments in the code. modified index.md --- value-object/index.md | 4 ++-- .../java/com/iluwatar/value/object/App.java | 13 ++++++++--- .../com/iluwatar/value/object/HeroStat.java | 18 +++++++++++---- .../iluwatar/value/object/HeroStatTest.java | 23 +++++++++++++++++++ 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/value-object/index.md b/value-object/index.md index 8645a2384..83223d8a2 100644 --- a/value-object/index.md +++ b/value-object/index.md @@ -23,9 +23,9 @@ Use the Value Object when ## Real world examples -* [java.util.Date](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) +* [java.util.Optional](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html) +* [java.time.LocalDate](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html) * [joda-time, money, beans](http://www.joda.org/) -* [JSR-310 / ThreeTen project LocalDate](http://www.threeten.org/articles/local-date.html) ## Credits diff --git a/value-object/src/main/java/com/iluwatar/value/object/App.java b/value-object/src/main/java/com/iluwatar/value/object/App.java index 8c3c56db2..da4c81812 100644 --- a/value-object/src/main/java/com/iluwatar/value/object/App.java +++ b/value-object/src/main/java/com/iluwatar/value/object/App.java @@ -1,12 +1,19 @@ package com.iluwatar.value.object; /** - * Hello world!. + * App Class. * */ public class App { + /** + * main method. + */ public static void main(String[] args) { - HeroStat stat = HeroStat.valueOf(10, 5, 0); - System.out.println(stat.toString()); + HeroStat statA = HeroStat.valueOf(10, 5, 0); + HeroStat statB = HeroStat.valueOf(5, 1, 8); + + System.out.println(statA.toString()); + // When using Value Objects do not use ==, only compare using equals(). + System.out.println("is statA and statB equal : " + statA.equals(statB)); } } diff --git a/value-object/src/main/java/com/iluwatar/value/object/HeroStat.java b/value-object/src/main/java/com/iluwatar/value/object/HeroStat.java index b53b5c333..efcbce7ea 100644 --- a/value-object/src/main/java/com/iluwatar/value/object/HeroStat.java +++ b/value-object/src/main/java/com/iluwatar/value/object/HeroStat.java @@ -1,13 +1,20 @@ package com.iluwatar.value.object; /** - * The Discount Coupon only discounts by percentage. - * + * HeroStat is a Value Object. following rules are from Stephen Colebourne's term VALJO(not the + * entire rule set) from : http://blog.joda.org/2014/03/valjos-value-java-objects.html
+ * Value Objects must override equals(), hashCode() to check the equality with values.
+ * Value Objects should be immutable so declare members final. Obtain instances by static factory + * methods.
+ * The elements of the state must be other values, including primitive types.
+ * Provide methods, typically simple getters, to get the elements of the state.
+ * + * {@link http://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html} */ public class HeroStat { - // stats for a hero + // Stats for a hero private final int strength; private final int intelligence; @@ -22,6 +29,7 @@ public class HeroStat { this.luck = luck; } + // Static factory method to create new instances. public static HeroStat valueOf(int strength, int intelligence, int luck) { return new HeroStat(strength, intelligence, luck); } @@ -39,7 +47,7 @@ public class HeroStat { } /* - * recommended to provide a static factory method capable of creating an instance from the formal + * Recommended to provide a static factory method capable of creating an instance from the formal * string representation declared like this. public static Juice parse(String string) {} */ @@ -86,6 +94,6 @@ public class HeroStat { } - // the clone() method should not be public + // The clone() method should not be public } diff --git a/value-object/src/test/java/com/iluwatar/value/object/HeroStatTest.java b/value-object/src/test/java/com/iluwatar/value/object/HeroStatTest.java index a7c07d226..f8785e538 100644 --- a/value-object/src/test/java/com/iluwatar/value/object/HeroStatTest.java +++ b/value-object/src/test/java/com/iluwatar/value/object/HeroStatTest.java @@ -1,5 +1,10 @@ package com.iluwatar.value.object; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; + +import static org.junit.Assert.assertThat; + import com.google.common.testing.EqualsTester; import org.junit.Test; @@ -11,6 +16,7 @@ public class HeroStatTest { /** * Tester for equals() and hashCode() methods of a class. + * * @see http://www.javadoc.io/doc/com.google.guava/guava-testlib/19.0 */ @Test @@ -20,4 +26,21 @@ public class HeroStatTest { new EqualsTester().addEqualityGroup(heroStatA, heroStatB).testEquals(); } + /** + * The toString() for two equal values must be the same. For two non-equal values it must be + * different. + */ + @Test + public void testToString() { + + HeroStat heroStatA = HeroStat.valueOf(3, 9, 2); + HeroStat heroStatB = HeroStat.valueOf(3, 9, 2); + HeroStat heroStatC = HeroStat.valueOf(3, 9, 8); + + assertThat(heroStatA.toString(), is(heroStatB.toString())); + assertThat(heroStatA.toString(), is(not(heroStatC.toString()))); + + + } + }