diff --git a/autovalue-tutorial/pom.xml b/autovalue-tutorial/pom.xml new file mode 100644 index 0000000000..37d595dce1 --- /dev/null +++ b/autovalue-tutorial/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + com.baeldung + autovalue-tutorial + 1.0 + AutoValue + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 7 + 7 + + + + + + + com.google.auto.value + auto-value + 1.2 + + + + junit + junit + 4.3 + test + + + + diff --git a/autovalue-tutorial/src/main/java/com/baeldung/autovalue/AutoValueMoney.java b/autovalue-tutorial/src/main/java/com/baeldung/autovalue/AutoValueMoney.java new file mode 100644 index 0000000000..ef39f499d1 --- /dev/null +++ b/autovalue-tutorial/src/main/java/com/baeldung/autovalue/AutoValueMoney.java @@ -0,0 +1,15 @@ +package com.baeldung.autovalue; + +import com.google.auto.value.AutoValue; + +@AutoValue +public abstract class AutoValueMoney { + public abstract String getCurrency(); + + public abstract long getAmount(); + + public static AutoValueMoney create(String currency, long amount) { + return new AutoValue_AutoValueMoney(currency, amount); + + } +} diff --git a/autovalue-tutorial/src/main/java/com/baeldung/autovalue/AutoValueMoneyWithBuilder.java b/autovalue-tutorial/src/main/java/com/baeldung/autovalue/AutoValueMoneyWithBuilder.java new file mode 100644 index 0000000000..a7ac93e45b --- /dev/null +++ b/autovalue-tutorial/src/main/java/com/baeldung/autovalue/AutoValueMoneyWithBuilder.java @@ -0,0 +1,23 @@ +package com.baeldung.autovalue; + +import com.google.auto.value.AutoValue; + +@AutoValue +public abstract class AutoValueMoneyWithBuilder { + public abstract String getCurrency(); + + public abstract long getAmount(); + + static Builder builder() { + return new AutoValue_AutoValueMoneyWithBuilder.Builder(); + } + + @AutoValue.Builder + abstract static class Builder { + abstract Builder setCurrency(String currency); + + abstract Builder setAmount(long amount); + + abstract AutoValueMoneyWithBuilder build(); + } +} diff --git a/autovalue-tutorial/src/main/java/com/baeldung/autovalue/Foo.java b/autovalue-tutorial/src/main/java/com/baeldung/autovalue/Foo.java new file mode 100644 index 0000000000..bb90070f6d --- /dev/null +++ b/autovalue-tutorial/src/main/java/com/baeldung/autovalue/Foo.java @@ -0,0 +1,51 @@ +package com.baeldung.autovalue; + +import java.util.Objects; + +public final class Foo { + private final String text; + private final int number; + + public Foo(String text, int number) { + this.text = text; + this.number = number; + } + + public String getText() { + return text; + } + + public int getNumber() { + return number; + } + + @Override + public int hashCode() { + return Objects.hash(text, number); + } + + @Override + public String toString() { + return "Foo [text=" + text + ", number=" + number + "]"; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Foo other = (Foo) obj; + if (number != other.number) + return false; + if (text == null) { + if (other.text != null) + return false; + } else if (!text.equals(other.text)) + return false; + return true; + } + +} diff --git a/autovalue-tutorial/src/main/java/com/baeldung/autovalue/ImmutableMoney.java b/autovalue-tutorial/src/main/java/com/baeldung/autovalue/ImmutableMoney.java new file mode 100644 index 0000000000..04d29b6b09 --- /dev/null +++ b/autovalue-tutorial/src/main/java/com/baeldung/autovalue/ImmutableMoney.java @@ -0,0 +1,52 @@ +package com.baeldung.autovalue; +public final class ImmutableMoney { + private final long amount; + private final String currency; + public ImmutableMoney(long amount, String currency) { + this.amount = amount; + this.currency = currency; + } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (int) (amount ^ (amount >>> 32)); + result = prime * result + + ((currency == null) ? 0 : currency.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ImmutableMoney other = (ImmutableMoney) obj; + if (amount != other.amount) + return false; + if (currency == null) { + if (other.currency != null) + return false; + } else if (!currency.equals(other.currency)) + return false; + return true; + } + + public long getAmount() { + return amount; + } + + public String getCurrency() { + return currency; + } + + @Override + public String toString() { + return "ImmutableMoney [amount=" + amount + ", currency=" + currency + + "]"; + } + +} diff --git a/autovalue-tutorial/src/main/java/com/baeldung/autovalue/MutableMoney.java b/autovalue-tutorial/src/main/java/com/baeldung/autovalue/MutableMoney.java new file mode 100644 index 0000000000..6cf8b75f7d --- /dev/null +++ b/autovalue-tutorial/src/main/java/com/baeldung/autovalue/MutableMoney.java @@ -0,0 +1,35 @@ +package com.baeldung.autovalue; + +public class MutableMoney { + @Override + public String toString() { + return "MutableMoney [amount=" + amount + ", currency=" + currency + + "]"; + } + + public long getAmount() { + return amount; + } + + public void setAmount(long amount) { + this.amount = amount; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + private long amount; + private String currency; + + public MutableMoney(long amount, String currency) { + super(); + this.amount = amount; + this.currency = currency; + } + +} diff --git a/autovalue-tutorial/src/test/java/com/baeldung/autovalue/MoneyTest.java b/autovalue-tutorial/src/test/java/com/baeldung/autovalue/MoneyTest.java new file mode 100644 index 0000000000..af3afe84fb --- /dev/null +++ b/autovalue-tutorial/src/test/java/com/baeldung/autovalue/MoneyTest.java @@ -0,0 +1,59 @@ +package com.baeldung.autovalue; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class MoneyTest { + @Test + public void givenTwoSameValueMoneyObjects_whenEqualityTestFails_thenCorrect() { + MutableMoney m1 = new MutableMoney(10000, "USD"); + MutableMoney m2 = new MutableMoney(10000, "USD"); + assertFalse(m1.equals(m2)); + } + + @Test + public void givenTwoSameValueMoneyValueObjects_whenEqualityTestPasses_thenCorrect() { + ImmutableMoney m1 = new ImmutableMoney(10000, "USD"); + ImmutableMoney m2 = new ImmutableMoney(10000, "USD"); + assertTrue(m1.equals(m2)); + } + + @Test + public void givenValueTypeWithAutoValue_whenFieldsCorrectlySet_thenCorrect() { + AutoValueMoney m = AutoValueMoney.create("USD", 10000); + assertEquals(m.getAmount(), 10000); + assertEquals(m.getCurrency(), "USD"); + } + + @Test + public void given2EqualValueTypesWithAutoValue_whenEqual_thenCorrect() { + AutoValueMoney m1 = AutoValueMoney.create("USD", 5000); + AutoValueMoney m2 = AutoValueMoney.create("USD", 5000); + assertTrue(m1.equals(m2)); + } + @Test + public void given2DifferentValueTypesWithAutoValue_whenNotEqual_thenCorrect() { + AutoValueMoney m1 = AutoValueMoney.create("GBP", 5000); + AutoValueMoney m2 = AutoValueMoney.create("USD", 5000); + assertFalse(m1.equals(m2)); + } + @Test + public void given2EqualValueTypesWithBuilder_whenEqual_thenCorrect() { + AutoValueMoneyWithBuilder m1 = AutoValueMoneyWithBuilder.builder().setAmount(5000).setCurrency("USD").build(); + AutoValueMoneyWithBuilder m2 = AutoValueMoneyWithBuilder.builder().setAmount(5000).setCurrency("USD").build(); + assertTrue(m1.equals(m2)); + } + @Test + public void given2DifferentValueTypesBuilder_whenNotEqual_thenCorrect() { + AutoValueMoneyWithBuilder m1 = AutoValueMoneyWithBuilder.builder().setAmount(5000).setCurrency("USD").build(); + AutoValueMoneyWithBuilder m2 = AutoValueMoneyWithBuilder.builder().setAmount(5000).setCurrency("GBP").build(); + assertFalse(m1.equals(m2)); + } + @Test + public void givenValueTypeWithBuilder_whenFieldsCorrectlySet_thenCorrect() { + AutoValueMoneyWithBuilder m = AutoValueMoneyWithBuilder.builder().setAmount(5000).setCurrency("USD").build(); + assertEquals(m.getAmount(), 5000); + assertEquals(m.getCurrency(), "USD"); + } +}