From e872f156be613dc5765a0d621c97201ec49f7734 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 27 Jul 2018 21:27:38 +0200 Subject: [PATCH 1/7] BAEL-1997 state design pattern in Java --- .../com/baeldung/state/ExplicitStateDemo.java | 21 ++++++++++++++ .../com/baeldung/state/InternalStateDemo.java | 15 ++++++++++ .../baeldung/state/context/TradeContext.java | 29 +++++++++++++++++++ .../state/states/DayTradeBehavior.java | 11 +++++++ .../states/FallbackDayTradeBehavior.java | 16 ++++++++++ .../state/states/PositionTradeBehavior.java | 11 +++++++ .../state/states/SwingTradeBehavior.java | 11 +++++++ .../baeldung/state/states/TradeBehavior.java | 7 +++++ 8 files changed, 121 insertions(+) create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java new file mode 100644 index 0000000000..de7b54d70e --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java @@ -0,0 +1,21 @@ +package com.baeldung.state; + +import com.baeldung.state.context.TradeContext; +import com.baeldung.state.states.DayTradeBehavior; +import com.baeldung.state.states.PositionTradeBehavior; +import com.baeldung.state.states.SwingTradeBehavior; + +public class ExplicitStateDemo { + + public static void main(String[] args) { + + TradeContext tradeContext = new TradeContext(new SwingTradeBehavior()); + tradeContext.trade(); + + tradeContext.setBehavior(new PositionTradeBehavior()); + tradeContext.trade(); + + tradeContext.setBehavior(new DayTradeBehavior()); + tradeContext.trade(); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java new file mode 100644 index 0000000000..3148940f17 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java @@ -0,0 +1,15 @@ +package com.baeldung.state; + +import com.baeldung.state.context.TradeContext; +import com.baeldung.state.states.FallbackDayTradeBehavior; + +public class InternalStateDemo { + + public static void main(String[] args) { + + TradeContext tradeContext = new TradeContext(new FallbackDayTradeBehavior()); + tradeContext.trade(); + tradeContext.trade(); + tradeContext.trade(); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java b/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java new file mode 100644 index 0000000000..0f12d050db --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java @@ -0,0 +1,29 @@ +package com.baeldung.state.context; + +import com.baeldung.state.states.TradeBehavior; + +public class TradeContext { + + private TradeBehavior behavior; + + public TradeContext(TradeBehavior behavior) { + this.behavior = behavior; + } + + public TradeBehavior getBehavior() { + return behavior; + } + + public void setBehavior(TradeBehavior behavior) { + this.behavior = behavior; + } + + public void trade() { + behavior.trade(this); + } + + @Override + public String toString() { + return "TradeContext{" + "behavior=" + behavior + '}'; + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java new file mode 100644 index 0000000000..540530e50b --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java @@ -0,0 +1,11 @@ +package com.baeldung.state.states; + +import com.baeldung.state.context.TradeContext; + +public class DayTradeBehavior implements TradeBehavior { + + @Override + public void trade(TradeContext ctx) { + System.out.println("Trading method: Day Trade"); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java new file mode 100644 index 0000000000..cf6e3ea695 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java @@ -0,0 +1,16 @@ +package com.baeldung.state.states; + +import com.baeldung.state.context.TradeContext; + +public class FallbackDayTradeBehavior implements TradeBehavior { + + @Override + public void trade(TradeContext ctx) { + System.out.println("Trading method: Day Trade"); + + if (Math.random() > 0.4) { + ctx.setBehavior(new SwingTradeBehavior()); + System.out.println("Day trading would not go well, falling back to Swing Trade"); + } + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java new file mode 100644 index 0000000000..0b1facb143 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java @@ -0,0 +1,11 @@ +package com.baeldung.state.states; + +import com.baeldung.state.context.TradeContext; + +public class PositionTradeBehavior implements TradeBehavior { + + @Override + public void trade(TradeContext ctx) { + System.out.println("Trading method: Position Trade"); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java new file mode 100644 index 0000000000..bbe8091bed --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java @@ -0,0 +1,11 @@ +package com.baeldung.state.states; + +import com.baeldung.state.context.TradeContext; + +public class SwingTradeBehavior implements TradeBehavior { + + @Override + public void trade(TradeContext ctx) { + System.out.println("Trading method: Swing Trade"); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java new file mode 100644 index 0000000000..ef5554ab50 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java @@ -0,0 +1,7 @@ +package com.baeldung.state.states; + +import com.baeldung.state.context.TradeContext; + +public interface TradeBehavior { + void trade(TradeContext ctx); +} From 26a28dd663a1dd8ca1a692c8973d3f4427ab3d36 Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 7 Aug 2018 21:28:06 +0200 Subject: [PATCH 2/7] BAEL-1997 different example code --- .../com/baeldung/state/DeliveredState.java | 19 +++++++++++ .../com/baeldung/state/ExplicitStateDemo.java | 21 ------------ .../com/baeldung/state/InternalStateDemo.java | 15 --------- .../java/com/baeldung/state/OrderedState.java | 19 +++++++++++ .../main/java/com/baeldung/state/Package.java | 22 +++++++++++++ .../java/com/baeldung/state/PackageState.java | 8 +++++ .../com/baeldung/state/ReceivedState.java | 23 +++++++++++++ .../baeldung/state/context/TradeContext.java | 29 ---------------- .../state/states/DayTradeBehavior.java | 11 ------- .../states/FallbackDayTradeBehavior.java | 16 --------- .../state/states/PositionTradeBehavior.java | 11 ------- .../state/states/SwingTradeBehavior.java | 11 ------- .../baeldung/state/states/TradeBehavior.java | 7 ---- .../baeldung/state/StatePatternUnitTest.java | 33 +++++++++++++++++++ 14 files changed, 124 insertions(+), 121 deletions(-) create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/Package.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java create mode 100644 patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java new file mode 100644 index 0000000000..adb0fd3f5a --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java @@ -0,0 +1,19 @@ +package com.baeldung.state; + +public class DeliveredState implements PackageState { + + @Override + public void next(Package pkg) { + pkg.setState(new ReceivedState()); + } + + @Override + public void prev(Package pkg) { + pkg.setState(new OrderedState()); + } + + @Override + public String toString() { + return "DeliveredState{}"; + } +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java deleted file mode 100644 index de7b54d70e..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.state; - -import com.baeldung.state.context.TradeContext; -import com.baeldung.state.states.DayTradeBehavior; -import com.baeldung.state.states.PositionTradeBehavior; -import com.baeldung.state.states.SwingTradeBehavior; - -public class ExplicitStateDemo { - - public static void main(String[] args) { - - TradeContext tradeContext = new TradeContext(new SwingTradeBehavior()); - tradeContext.trade(); - - tradeContext.setBehavior(new PositionTradeBehavior()); - tradeContext.trade(); - - tradeContext.setBehavior(new DayTradeBehavior()); - tradeContext.trade(); - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java deleted file mode 100644 index 3148940f17..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.state; - -import com.baeldung.state.context.TradeContext; -import com.baeldung.state.states.FallbackDayTradeBehavior; - -public class InternalStateDemo { - - public static void main(String[] args) { - - TradeContext tradeContext = new TradeContext(new FallbackDayTradeBehavior()); - tradeContext.trade(); - tradeContext.trade(); - tradeContext.trade(); - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java new file mode 100644 index 0000000000..56a267f160 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java @@ -0,0 +1,19 @@ +package com.baeldung.state; + +public class OrderedState implements PackageState { + + @Override + public void next(Package pkg) { + pkg.setState(new DeliveredState()); + } + + @Override + public void prev(Package pkg) { + System.out.println("The package is in it's root state."); + } + + @Override + public String toString() { + return "OrderedState{}"; + } +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java new file mode 100644 index 0000000000..ff04d543b8 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java @@ -0,0 +1,22 @@ +package com.baeldung.state; + +public class Package { + + private PackageState state = new OrderedState(); + + public PackageState getState() { + return state; + } + + public void setState(PackageState state) { + this.state = state; + } + + public void previousState() { + state.prev(this); + } + + public void nextState() { + state.next(this); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java new file mode 100644 index 0000000000..d7f7a5c110 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java @@ -0,0 +1,8 @@ +package com.baeldung.state; + +public interface PackageState { + + void next(Package pkg); + + void prev(Package pkg); +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java new file mode 100644 index 0000000000..be76937159 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java @@ -0,0 +1,23 @@ +package com.baeldung.state; + +public class ReceivedState implements PackageState { + + @Override + public void next(Package pkg) { + if (pkg.getState() instanceof ReceivedState) { + System.out.println("This package is already received by a client."); + return; + } + pkg.setState(this); + } + + @Override + public void prev(Package pkg) { + pkg.setState(new DeliveredState()); + } + + @Override + public String toString() { + return "ReceivedState{}"; + } +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java b/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java deleted file mode 100644 index 0f12d050db..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.state.context; - -import com.baeldung.state.states.TradeBehavior; - -public class TradeContext { - - private TradeBehavior behavior; - - public TradeContext(TradeBehavior behavior) { - this.behavior = behavior; - } - - public TradeBehavior getBehavior() { - return behavior; - } - - public void setBehavior(TradeBehavior behavior) { - this.behavior = behavior; - } - - public void trade() { - behavior.trade(this); - } - - @Override - public String toString() { - return "TradeContext{" + "behavior=" + behavior + '}'; - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java deleted file mode 100644 index 540530e50b..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.state.states; - -import com.baeldung.state.context.TradeContext; - -public class DayTradeBehavior implements TradeBehavior { - - @Override - public void trade(TradeContext ctx) { - System.out.println("Trading method: Day Trade"); - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java deleted file mode 100644 index cf6e3ea695..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.state.states; - -import com.baeldung.state.context.TradeContext; - -public class FallbackDayTradeBehavior implements TradeBehavior { - - @Override - public void trade(TradeContext ctx) { - System.out.println("Trading method: Day Trade"); - - if (Math.random() > 0.4) { - ctx.setBehavior(new SwingTradeBehavior()); - System.out.println("Day trading would not go well, falling back to Swing Trade"); - } - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java deleted file mode 100644 index 0b1facb143..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.state.states; - -import com.baeldung.state.context.TradeContext; - -public class PositionTradeBehavior implements TradeBehavior { - - @Override - public void trade(TradeContext ctx) { - System.out.println("Trading method: Position Trade"); - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java deleted file mode 100644 index bbe8091bed..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.state.states; - -import com.baeldung.state.context.TradeContext; - -public class SwingTradeBehavior implements TradeBehavior { - - @Override - public void trade(TradeContext ctx) { - System.out.println("Trading method: Swing Trade"); - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java deleted file mode 100644 index ef5554ab50..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.baeldung.state.states; - -import com.baeldung.state.context.TradeContext; - -public interface TradeBehavior { - void trade(TradeContext ctx); -} diff --git a/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java b/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java new file mode 100644 index 0000000000..731974f92b --- /dev/null +++ b/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.state; + +import com.baeldung.state.Package; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.instanceOf; + +import org.junit.Test; + +public class StatePatternUnitTest { + + @Test + public void givenNewPackage_whenPackageReceived_thenStateReceived() { + Package pkg = new Package(); + + assertThat(pkg.getState(), instanceOf(OrderedState.class)); + pkg.nextState(); + + assertThat(pkg.getState(), instanceOf(DeliveredState.class)); + pkg.nextState(); + + assertThat(pkg.getState(), instanceOf(ReceivedState.class)); + } + + @Test + public void givenDeliveredPackage_whenPrevState_thenStateOrdered() { + Package pkg = new Package(); + pkg.setState(new DeliveredState()); + pkg.previousState(); + + assertThat(pkg.getState(), instanceOf(OrderedState.class)); + } +} From 7c422c70ad16e7783ff5a919d6a330f9a45307ce Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 9 Aug 2018 21:06:30 +0200 Subject: [PATCH 3/7] BAEL-1997 add additional method to the states --- .../com/baeldung/state/DeliveredState.java | 6 ++++++ .../java/com/baeldung/state/OrderedState.java | 5 +++++ .../main/java/com/baeldung/state/Package.java | 4 ++++ .../java/com/baeldung/state/PackageState.java | 2 ++ .../com/baeldung/state/ReceivedState.java | 5 +++++ .../java/com/baeldung/state/StateDemo.java | 19 +++++++++++++++++++ 6 files changed, 41 insertions(+) create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java index adb0fd3f5a..9f5e4d8fc4 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java @@ -12,8 +12,14 @@ public class DeliveredState implements PackageState { pkg.setState(new OrderedState()); } + @Override + public void printStatus() { + System.out.println("Package delivered to post office, not received yet."); + } + @Override public String toString() { return "DeliveredState{}"; } + } \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java index 56a267f160..0642c4c73c 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java @@ -12,6 +12,11 @@ public class OrderedState implements PackageState { System.out.println("The package is in it's root state."); } + @Override + public void printStatus() { + System.out.println("Package ordered, not delivered to the office yet."); + } + @Override public String toString() { return "OrderedState{}"; diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java index ff04d543b8..f3dfbb3fa7 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java @@ -19,4 +19,8 @@ public class Package { public void nextState() { state.next(this); } + + public void printStatus() { + state.printStatus(); + } } diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java index d7f7a5c110..d6656c78ac 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java @@ -5,4 +5,6 @@ public interface PackageState { void next(Package pkg); void prev(Package pkg); + + void printStatus(); } \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java index be76937159..de25672e67 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java @@ -16,6 +16,11 @@ public class ReceivedState implements PackageState { pkg.setState(new DeliveredState()); } + @Override + public void printStatus() { + System.out.println("Package was received by client."); + } + @Override public String toString() { return "ReceivedState{}"; diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java new file mode 100644 index 0000000000..1a63ea3ddf --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java @@ -0,0 +1,19 @@ +package com.baeldung.state; + +public class StateDemo { + + public static void main(String[] args) { + + Package pkg = new Package(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + } +} From 1a1ea1560dfcae9b472a2e046ce5de1cc72c6186 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 10 Aug 2018 23:28:16 +0200 Subject: [PATCH 4/7] BAEL-1997 clean up in ReceivedState --- .../src/main/java/com/baeldung/state/ReceivedState.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java index de25672e67..84136fa48e 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java @@ -4,11 +4,7 @@ public class ReceivedState implements PackageState { @Override public void next(Package pkg) { - if (pkg.getState() instanceof ReceivedState) { - System.out.println("This package is already received by a client."); - return; - } - pkg.setState(this); + System.out.println("This package is already received by a client."); } @Override From b2644c5cfcf112f548fa3766207aa168a152bc7d Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 28 Aug 2018 17:28:41 +0200 Subject: [PATCH 5/7] BAEL-2144 sort string alphabetically --- .../string/anagram/AnagramValidator.java | 29 +++++++++++++++++ .../string/AnagramValidatorUnitTest.java | 26 ++++++++++++++++ .../sorting/string/SortStringUnitTest.java | 31 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java b/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java new file mode 100644 index 0000000000..a12571b2e2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java @@ -0,0 +1,29 @@ +package com.baeldung.sorting.string.anagram; + +import java.util.Arrays; + +public class AnagramValidator { + + public static boolean isValid(String text, String anagram) { + text = prepare(text); + anagram = prepare(anagram); + + String sortedText = sort(text); + String sortedAnagram = sort(anagram); + + return sortedText.equals(sortedAnagram); + } + + private static String sort(String text) { + char[] chars = prepare(text).toCharArray(); + + Arrays.sort(chars); + return new String(chars); + } + + private static String prepare(String text) { + return text.toLowerCase() + .trim() + .replaceAll("\\s+", ""); + } +} \ No newline at end of file diff --git a/core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java b/core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java new file mode 100644 index 0000000000..7759e929ae --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java @@ -0,0 +1,26 @@ +package org.baeldung.java.sorting.string; + + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; + +import org.junit.jupiter.api.Test; + +import com.baeldung.sorting.string.anagram.AnagramValidator; + +class AnagramValidatorUnitTest { + + @Test + void givenValidAnagrams_whenSorted_thenEqual() { + boolean isValidAnagram = AnagramValidator.isValid("Avida Dollars", "Salvador Dali"); + + assertTrue(isValidAnagram); + } + + @Test + void givenNotValidAnagrams_whenSorted_thenNotEqual() { + boolean isValidAnagram = AnagramValidator.isValid("abc", "def"); + + assertFalse(isValidAnagram); + } +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java b/core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java new file mode 100644 index 0000000000..73fd29f0d3 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java @@ -0,0 +1,31 @@ +package org.baeldung.java.sorting.string; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class SortStringUnitTest { + + @Test + void givenString_whenSort_thenSorted() { + String abcd = "bdca"; + char[] chars = abcd.toCharArray(); + + Arrays.sort(chars); + String sorted = new String(chars); + + assertThat(sorted).isEqualTo("abcd"); + } + + @Test + void givenString_whenSortJava8_thenSorted() { + String sorted = "bdca".chars() + .sorted() + .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) + .toString(); + + assertThat(sorted).isEqualTo("abcd"); + } +} From 61de6251ec763ab3aadbb64e2e061f2ce3849410 Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 30 Aug 2018 21:58:38 +0200 Subject: [PATCH 6/7] BAEL-2144 move code to different module --- .../string/sorting/AnagramValidator.java | 29 +++++++++++++++++++ .../sorting}/AnagramValidatorUnitTest.java | 4 +-- .../string/sorting}/SortStringUnitTest.java | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 java-strings/src/main/java/com/baeldung/string/sorting/AnagramValidator.java rename {core-java/src/test/java/org/baeldung/java/sorting/string => java-strings/src/test/java/com/baeldung/string/sorting}/AnagramValidatorUnitTest.java (85%) rename {core-java/src/test/java/org/baeldung/java/sorting/string => java-strings/src/test/java/com/baeldung/string/sorting}/SortStringUnitTest.java (94%) diff --git a/java-strings/src/main/java/com/baeldung/string/sorting/AnagramValidator.java b/java-strings/src/main/java/com/baeldung/string/sorting/AnagramValidator.java new file mode 100644 index 0000000000..c4f684383d --- /dev/null +++ b/java-strings/src/main/java/com/baeldung/string/sorting/AnagramValidator.java @@ -0,0 +1,29 @@ +package com.baeldung.string.sorting; + +import java.util.Arrays; + +public class AnagramValidator { + + public static boolean isValid(String text, String anagram) { + text = prepare(text); + anagram = prepare(anagram); + + String sortedText = sort(text); + String sortedAnagram = sort(anagram); + + return sortedText.equals(sortedAnagram); + } + + private static String sort(String text) { + char[] chars = prepare(text).toCharArray(); + + Arrays.sort(chars); + return new String(chars); + } + + private static String prepare(String text) { + return text.toLowerCase() + .trim() + .replaceAll("\\s+", ""); + } +} \ No newline at end of file diff --git a/core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java b/java-strings/src/test/java/com/baeldung/string/sorting/AnagramValidatorUnitTest.java similarity index 85% rename from core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java rename to java-strings/src/test/java/com/baeldung/string/sorting/AnagramValidatorUnitTest.java index 7759e929ae..07d31c7187 100644 --- a/core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java +++ b/java-strings/src/test/java/com/baeldung/string/sorting/AnagramValidatorUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.java.sorting.string; +package com.baeldung.string.sorting; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -6,7 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import org.junit.jupiter.api.Test; -import com.baeldung.sorting.string.anagram.AnagramValidator; +import com.baeldung.string.sorting.AnagramValidator; class AnagramValidatorUnitTest { diff --git a/core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java b/java-strings/src/test/java/com/baeldung/string/sorting/SortStringUnitTest.java similarity index 94% rename from core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java rename to java-strings/src/test/java/com/baeldung/string/sorting/SortStringUnitTest.java index 73fd29f0d3..90d1dad554 100644 --- a/core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java +++ b/java-strings/src/test/java/com/baeldung/string/sorting/SortStringUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.java.sorting.string; +package com.baeldung.string.sorting; import java.util.Arrays; From db0caea58884a3c573d08a1e4290e73743c3b4b5 Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 30 Aug 2018 22:08:34 +0200 Subject: [PATCH 7/7] BAEL-2144 remove duplicated anagram validator from old location --- .../string/anagram/AnagramValidator.java | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java diff --git a/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java b/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java deleted file mode 100644 index a12571b2e2..0000000000 --- a/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.sorting.string.anagram; - -import java.util.Arrays; - -public class AnagramValidator { - - public static boolean isValid(String text, String anagram) { - text = prepare(text); - anagram = prepare(anagram); - - String sortedText = sort(text); - String sortedAnagram = sort(anagram); - - return sortedText.equals(sortedAnagram); - } - - private static String sort(String text) { - char[] chars = prepare(text).toCharArray(); - - Arrays.sort(chars); - return new String(chars); - } - - private static String prepare(String text) { - return text.toLowerCase() - .trim() - .replaceAll("\\s+", ""); - } -} \ No newline at end of file