diff --git a/prototype/src/main/java/com/iluwatar/prototype/Beast.java b/prototype/src/main/java/com/iluwatar/prototype/Beast.java index e3b25b850..c6797fd29 100644 --- a/prototype/src/main/java/com/iluwatar/prototype/Beast.java +++ b/prototype/src/main/java/com/iluwatar/prototype/Beast.java @@ -26,9 +26,24 @@ package com.iluwatar.prototype; /** * Beast. */ -public abstract class Beast extends Prototype { +public abstract class Beast implements Prototype { + + public Beast() { } + + public Beast(Beast source) { } @Override - public abstract Beast copy() throws CloneNotSupportedException; + public abstract Beast copy(); + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + return getClass() == obj.getClass(); + } } diff --git a/prototype/src/main/java/com/iluwatar/prototype/ElfBeast.java b/prototype/src/main/java/com/iluwatar/prototype/ElfBeast.java index 50761964a..e88f11699 100644 --- a/prototype/src/main/java/com/iluwatar/prototype/ElfBeast.java +++ b/prototype/src/main/java/com/iluwatar/prototype/ElfBeast.java @@ -35,11 +35,12 @@ public class ElfBeast extends Beast { } public ElfBeast(ElfBeast elfBeast) { + super(elfBeast); this.helpType = elfBeast.helpType; } @Override - public Beast copy() { + public ElfBeast copy() { return new ElfBeast(this); } @@ -48,4 +49,26 @@ public class ElfBeast extends Beast { return "Elven eagle helps in " + helpType; } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + ElfBeast other = (ElfBeast) obj; + if (helpType == null) { + if (other.helpType != null) { + return false; + } + } else if (!helpType.equals(other.helpType)) { + return false; + } + return true; + } + } diff --git a/prototype/src/main/java/com/iluwatar/prototype/ElfMage.java b/prototype/src/main/java/com/iluwatar/prototype/ElfMage.java index 014c39bac..d569a18c2 100644 --- a/prototype/src/main/java/com/iluwatar/prototype/ElfMage.java +++ b/prototype/src/main/java/com/iluwatar/prototype/ElfMage.java @@ -28,7 +28,6 @@ package com.iluwatar.prototype; */ public class ElfMage extends Mage { - private String helpType; public ElfMage(String helpType) { @@ -36,6 +35,7 @@ public class ElfMage extends Mage { } public ElfMage(ElfMage elfMage) { + super(elfMage); this.helpType = elfMage.helpType; } @@ -49,4 +49,25 @@ public class ElfMage extends Mage { return "Elven mage helps in " + helpType; } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + ElfMage other = (ElfMage) obj; + if (helpType == null) { + if (other.helpType != null) { + return false; + } + } else if (!helpType.equals(other.helpType)) { + return false; + } + return true; + } } diff --git a/prototype/src/main/java/com/iluwatar/prototype/ElfWarlord.java b/prototype/src/main/java/com/iluwatar/prototype/ElfWarlord.java index 43cdac7e5..3484198d5 100644 --- a/prototype/src/main/java/com/iluwatar/prototype/ElfWarlord.java +++ b/prototype/src/main/java/com/iluwatar/prototype/ElfWarlord.java @@ -35,6 +35,7 @@ public class ElfWarlord extends Warlord { } public ElfWarlord(ElfWarlord elfWarlord) { + super(elfWarlord); this.helpType = elfWarlord.helpType; } @@ -48,4 +49,25 @@ public class ElfWarlord extends Warlord { return "Elven warlord helps in " + helpType; } -} + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + ElfWarlord other = (ElfWarlord) obj; + if (helpType == null) { + if (other.helpType != null) { + return false; + } + } else if (!helpType.equals(other.helpType)) { + return false; + } + return true; + } +} \ No newline at end of file diff --git a/prototype/src/main/java/com/iluwatar/prototype/HeroFactoryImpl.java b/prototype/src/main/java/com/iluwatar/prototype/HeroFactoryImpl.java index b9348a3b2..eb84b2982 100644 --- a/prototype/src/main/java/com/iluwatar/prototype/HeroFactoryImpl.java +++ b/prototype/src/main/java/com/iluwatar/prototype/HeroFactoryImpl.java @@ -45,33 +45,21 @@ public class HeroFactoryImpl implements HeroFactory { * Create mage. */ public Mage createMage() { - try { - return mage.copy(); - } catch (CloneNotSupportedException e) { - return null; - } + return mage.copy(); } /** * Create warlord. */ public Warlord createWarlord() { - try { - return warlord.copy(); - } catch (CloneNotSupportedException e) { - return null; - } + return warlord.copy(); } /** * Create beast. */ public Beast createBeast() { - try { - return beast.copy(); - } catch (CloneNotSupportedException e) { - return null; - } + return beast.copy(); } } diff --git a/prototype/src/main/java/com/iluwatar/prototype/Mage.java b/prototype/src/main/java/com/iluwatar/prototype/Mage.java index 27c3bae7a..f8a597805 100644 --- a/prototype/src/main/java/com/iluwatar/prototype/Mage.java +++ b/prototype/src/main/java/com/iluwatar/prototype/Mage.java @@ -26,9 +26,24 @@ package com.iluwatar.prototype; /** * Mage. */ -public abstract class Mage extends Prototype { +public abstract class Mage implements Prototype { + + public Mage() { } + + public Mage(Mage source) { } @Override - public abstract Mage copy() throws CloneNotSupportedException; + public abstract Mage copy(); + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + return getClass() == obj.getClass(); + } } diff --git a/prototype/src/main/java/com/iluwatar/prototype/OrcBeast.java b/prototype/src/main/java/com/iluwatar/prototype/OrcBeast.java index c800b3ac5..020063616 100644 --- a/prototype/src/main/java/com/iluwatar/prototype/OrcBeast.java +++ b/prototype/src/main/java/com/iluwatar/prototype/OrcBeast.java @@ -35,11 +35,12 @@ public class OrcBeast extends Beast { } public OrcBeast(OrcBeast orcBeast) { + super(orcBeast); this.weapon = orcBeast.weapon; } @Override - public Beast copy() { + public OrcBeast copy() { return new OrcBeast(this); } @@ -48,5 +49,27 @@ public class OrcBeast extends Beast { return "Orcish wolf attacks with " + weapon; } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + OrcBeast other = (OrcBeast) obj; + if (weapon == null) { + if (other.weapon != null) { + return false; + } + } else if (!weapon.equals(other.weapon)) { + return false; + } + return true; + } + } diff --git a/prototype/src/main/java/com/iluwatar/prototype/OrcMage.java b/prototype/src/main/java/com/iluwatar/prototype/OrcMage.java index c71b59d06..e14145434 100644 --- a/prototype/src/main/java/com/iluwatar/prototype/OrcMage.java +++ b/prototype/src/main/java/com/iluwatar/prototype/OrcMage.java @@ -35,6 +35,7 @@ public class OrcMage extends Mage { } public OrcMage(OrcMage orcMage) { + super(orcMage); this.weapon = orcMage.weapon; } @@ -48,4 +49,25 @@ public class OrcMage extends Mage { return "Orcish mage attacks with " + weapon; } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + OrcMage other = (OrcMage) obj; + if (weapon == null) { + if (other.weapon != null) { + return false; + } + } else if (!weapon.equals(other.weapon)) { + return false; + } + return true; + } } diff --git a/prototype/src/main/java/com/iluwatar/prototype/OrcWarlord.java b/prototype/src/main/java/com/iluwatar/prototype/OrcWarlord.java index 096fd49dc..cc5e17d8e 100644 --- a/prototype/src/main/java/com/iluwatar/prototype/OrcWarlord.java +++ b/prototype/src/main/java/com/iluwatar/prototype/OrcWarlord.java @@ -35,6 +35,7 @@ public class OrcWarlord extends Warlord { } public OrcWarlord(OrcWarlord orcWarlord) { + super(orcWarlord); this.weapon = orcWarlord.weapon; } @@ -48,4 +49,25 @@ public class OrcWarlord extends Warlord { return "Orcish warlord attacks with " + weapon; } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + OrcWarlord other = (OrcWarlord) obj; + if (weapon == null) { + if (other.weapon != null) { + return false; + } + } else if (!weapon.equals(other.weapon)) { + return false; + } + return true; + } } diff --git a/prototype/src/main/java/com/iluwatar/prototype/Prototype.java b/prototype/src/main/java/com/iluwatar/prototype/Prototype.java index c74c82f4a..d4c518ec2 100644 --- a/prototype/src/main/java/com/iluwatar/prototype/Prototype.java +++ b/prototype/src/main/java/com/iluwatar/prototype/Prototype.java @@ -26,8 +26,8 @@ package com.iluwatar.prototype; /** * Prototype. */ -public abstract class Prototype implements Cloneable { +public interface Prototype { - public abstract Object copy() throws CloneNotSupportedException; + Object copy(); } diff --git a/prototype/src/main/java/com/iluwatar/prototype/Warlord.java b/prototype/src/main/java/com/iluwatar/prototype/Warlord.java index c270ab80b..30a3cd75c 100644 --- a/prototype/src/main/java/com/iluwatar/prototype/Warlord.java +++ b/prototype/src/main/java/com/iluwatar/prototype/Warlord.java @@ -26,9 +26,24 @@ package com.iluwatar.prototype; /** * Warlord. */ -public abstract class Warlord extends Prototype { +public abstract class Warlord implements Prototype { + + public Warlord() { } + + public Warlord(Warlord source) { } @Override - public abstract Warlord copy() throws CloneNotSupportedException; + public abstract Warlord copy(); + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + return getClass() == obj.getClass(); + } } diff --git a/prototype/src/test/java/com/iluwatar/prototype/HeroFactoryImplTest.java b/prototype/src/test/java/com/iluwatar/prototype/HeroFactoryImplTest.java deleted file mode 100644 index 0d33b67c3..000000000 --- a/prototype/src/test/java/com/iluwatar/prototype/HeroFactoryImplTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package com.iluwatar.prototype; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -/** - * Date: 12/28/15 - 8:34 PM - * - * @author Jeroen Meulemeester - */ -public class HeroFactoryImplTest { - - @Test - public void testFactory() throws Exception { - final Mage mage = mock(Mage.class); - final Warlord warlord = mock(Warlord.class); - final Beast beast = mock(Beast.class); - - when(mage.copy()).thenThrow(CloneNotSupportedException.class); - when(warlord.copy()).thenThrow(CloneNotSupportedException.class); - when(beast.copy()).thenThrow(CloneNotSupportedException.class); - - final HeroFactoryImpl factory = new HeroFactoryImpl(mage, warlord, beast); - assertNull(factory.createMage()); - assertNull(factory.createWarlord()); - assertNull(factory.createBeast()); - - verify(mage).copy(); - verify(warlord).copy(); - verify(beast).copy(); - verifyNoMoreInteractions(mage, warlord, beast); - } - -} \ No newline at end of file diff --git a/prototype/src/test/java/com/iluwatar/prototype/PrototypeTest.java b/prototype/src/test/java/com/iluwatar/prototype/PrototypeTest.java index e4dd0658a..579746ecc 100644 --- a/prototype/src/test/java/com/iluwatar/prototype/PrototypeTest.java +++ b/prototype/src/test/java/com/iluwatar/prototype/PrototypeTest.java @@ -57,6 +57,7 @@ public class PrototypeTest

{ assertNotNull(clone); assertNotSame(clone, testedPrototype); assertSame(testedPrototype.getClass(), clone.getClass()); + assertEquals(clone, testedPrototype); } }