📍Use lombok, reformat, and optimize the code (#1560)
* Use lombok, reformat, and optimize the code * Fix merge conflicts and some sonar issues Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
@ -23,8 +23,7 @@
|
||||
|
||||
package com.iluwatar.value.object;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* A Value Object are objects which follow value semantics rather than reference semantics. This
|
||||
@ -40,10 +39,9 @@ import org.slf4j.LoggerFactory;
|
||||
* <p>For more specific and strict rules to implement value objects check the rules from Stephen
|
||||
* Colebourne's term VALJO : http://blog.joda.org/2014/03/valjos-value-java-objects.html
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* This practice creates three HeroStats(Value object) and checks equality between those.
|
||||
*/
|
||||
|
@ -23,6 +23,11 @@
|
||||
|
||||
package com.iluwatar.value.object;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* HeroStat is a value object.
|
||||
*
|
||||
@ -30,6 +35,10 @@ package com.iluwatar.value.object;
|
||||
* http://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html
|
||||
* </a>
|
||||
*/
|
||||
@Getter
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
@RequiredArgsConstructor
|
||||
public class HeroStat {
|
||||
|
||||
// Stats for a hero
|
||||
@ -38,74 +47,11 @@ public class HeroStat {
|
||||
private final int intelligence;
|
||||
private final int luck;
|
||||
|
||||
// All constructors must be private.
|
||||
private HeroStat(int strength, int intelligence, int luck) {
|
||||
this.strength = strength;
|
||||
this.intelligence = intelligence;
|
||||
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);
|
||||
}
|
||||
|
||||
public int getStrength() {
|
||||
return strength;
|
||||
}
|
||||
|
||||
public int getIntelligence() {
|
||||
return intelligence;
|
||||
}
|
||||
|
||||
public int getLuck() {
|
||||
return luck;
|
||||
}
|
||||
|
||||
/*
|
||||
* Recommended to provide a static factory method capable of creating an instance from the formal
|
||||
* string representation declared like this. public static HeroStat parse(String string) {}
|
||||
*/
|
||||
|
||||
// toString, hashCode, equals
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "HeroStat [strength=" + strength + ", intelligence=" + intelligence
|
||||
+ ", luck=" + luck + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final var prime = 31;
|
||||
var result = 1;
|
||||
result = prime * result + intelligence;
|
||||
result = prime * result + luck;
|
||||
result = prime * result + strength;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
var other = (HeroStat) obj;
|
||||
if (intelligence != other.intelligence) {
|
||||
return false;
|
||||
}
|
||||
if (luck != other.luck) {
|
||||
return false;
|
||||
}
|
||||
return strength == other.strength;
|
||||
}
|
||||
|
||||
// The clone() method should not be public. Just don't override it.
|
||||
|
||||
}
|
||||
|
@ -33,17 +33,17 @@ import org.junit.jupiter.api.Test;
|
||||
/**
|
||||
* Unit test for HeroStat.
|
||||
*/
|
||||
public class HeroStatTest {
|
||||
class HeroStatTest {
|
||||
|
||||
/**
|
||||
* Tester for equals() and hashCode() methods of a class. Using guava's EqualsTester.
|
||||
*
|
||||
* @see <a href="http://static.javadoc.io/com.google.guava/guava-testlib/19.0/com/google/common/testing/EqualsTester.html">
|
||||
* http://static.javadoc.io/com.google.guava/guava-testlib/19.0/com/google/common/testing/EqualsTester.html
|
||||
* </a>
|
||||
* http://static.javadoc.io/com.google.guava/guava-testlib/19.0/com/google/common/testing/EqualsTester.html
|
||||
* </a>
|
||||
*/
|
||||
@Test
|
||||
public void testEquals() {
|
||||
void testEquals() {
|
||||
var heroStatA = HeroStat.valueOf(3, 9, 2);
|
||||
var heroStatB = HeroStat.valueOf(3, 9, 2);
|
||||
new EqualsTester().addEqualityGroup(heroStatA, heroStatB).testEquals();
|
||||
@ -54,7 +54,7 @@ public class HeroStatTest {
|
||||
* different.
|
||||
*/
|
||||
@Test
|
||||
public void testToString() {
|
||||
void testToString() {
|
||||
var heroStatA = HeroStat.valueOf(3, 9, 2);
|
||||
var heroStatB = HeroStat.valueOf(3, 9, 2);
|
||||
var heroStatC = HeroStat.valueOf(3, 9, 8);
|
||||
|
Reference in New Issue
Block a user