Resolves checkstyle errors for facade factory-kit spatial-partition state step-builder (#1077)
* Reduces checkstyle errors in facade * Reduces checkstyle errors in factory-kit * Reduces checkstyle errors in spatial-partition * Reduces checkstyle errors in state * Reduces checkstyle errors in step-builder
This commit is contained in:
		
				
					committed by
					
						 Ilkka Seppälä
						Ilkka Seppälä
					
				
			
			
				
	
			
			
			
						parent
						
							2628cc0dfc
						
					
				
				
					commit
					c954a436ad
				
			| @@ -24,23 +24,21 @@ | |||||||
| package com.iluwatar.facade; | package com.iluwatar.facade; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  | ||||||
|  * The Facade design pattern is often used when a system is very complex or difficult to understand |  * The Facade design pattern is often used when a system is very complex or difficult to understand | ||||||
|  * because the system has a large number of interdependent classes or its source code is |  * because the system has a large number of interdependent classes or its source code is | ||||||
|  * unavailable. This pattern hides the complexities of the larger system and provides a simpler |  * unavailable. This pattern hides the complexities of the larger system and provides a simpler | ||||||
|  * interface to the client. It typically involves a single wrapper class which contains a set of |  * interface to the client. It typically involves a single wrapper class which contains a set of | ||||||
|  * members required by client. These members access the system on behalf of the facade client and |  * members required by client. These members access the system on behalf of the facade client and | ||||||
|  * hide the implementation details. |  * hide the implementation details. | ||||||
|  * <p> |  * | ||||||
|  * In this example the Facade is ({@link DwarvenGoldmineFacade}) and it provides a simpler interface |  * <p>In this example the Facade is ({@link DwarvenGoldmineFacade}) and it provides a simpler | ||||||
|  * to the goldmine subsystem. |  * interface to the goldmine subsystem. | ||||||
|  *  |  | ||||||
|  */ |  */ | ||||||
| public class App { | public class App { | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * Program entry point |    * Program entry point. | ||||||
|    *  |    * | ||||||
|    * @param args command line args |    * @param args command line args | ||||||
|    */ |    */ | ||||||
|   public static void main(String[] args) { |   public static void main(String[] args) { | ||||||
|   | |||||||
| @@ -27,9 +27,7 @@ import org.slf4j.Logger; | |||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  | ||||||
|  * DwarvenCartOperator is one of the goldmine subsystems. |  * DwarvenCartOperator is one of the goldmine subsystems. | ||||||
|  * |  | ||||||
|  */ |  */ | ||||||
| public class DwarvenCartOperator extends DwarvenMineWorker { | public class DwarvenCartOperator extends DwarvenMineWorker { | ||||||
|  |  | ||||||
|   | |||||||
| @@ -27,9 +27,7 @@ import org.slf4j.Logger; | |||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  | ||||||
|  * DwarvenGoldDigger is one of the goldmine subsystems. |  * DwarvenGoldDigger is one of the goldmine subsystems. | ||||||
|  * |  | ||||||
|  */ |  */ | ||||||
| public class DwarvenGoldDigger extends DwarvenMineWorker { | public class DwarvenGoldDigger extends DwarvenMineWorker { | ||||||
|  |  | ||||||
|   | |||||||
| @@ -27,25 +27,24 @@ import java.util.Collection; | |||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  * DwarvenGoldmineFacade provides a single interface through which users can operate the | ||||||
|  * DwarvenGoldmineFacade provides a single interface through which users can operate the subsystems. |  | ||||||
|  *  |  | ||||||
|  * This makes the goldmine easier to operate and cuts the dependencies from the goldmine user to the |  | ||||||
|  * subsystems. |  * subsystems. | ||||||
|  * |  * | ||||||
|  |  * <p>This makes the goldmine easier to operate and cuts the dependencies from the goldmine user to | ||||||
|  |  * the subsystems. | ||||||
|  */ |  */ | ||||||
| public class DwarvenGoldmineFacade { | public class DwarvenGoldmineFacade { | ||||||
|  |  | ||||||
|   private final List<DwarvenMineWorker> workers; |   private final List<DwarvenMineWorker> workers; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * Constructor |    * Constructor. | ||||||
|    */ |    */ | ||||||
|   public DwarvenGoldmineFacade() { |   public DwarvenGoldmineFacade() { | ||||||
|     workers = List.of( |     workers = List.of( | ||||||
|             new DwarvenGoldDigger(), |         new DwarvenGoldDigger(), | ||||||
|             new DwarvenCartOperator(), |         new DwarvenCartOperator(), | ||||||
|             new DwarvenTunnelDigger()); |         new DwarvenTunnelDigger()); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public void startNewDay() { |   public void startNewDay() { | ||||||
| @@ -60,8 +59,10 @@ public class DwarvenGoldmineFacade { | |||||||
|     makeActions(workers, DwarvenMineWorker.Action.GO_HOME, DwarvenMineWorker.Action.GO_TO_SLEEP); |     makeActions(workers, DwarvenMineWorker.Action.GO_HOME, DwarvenMineWorker.Action.GO_TO_SLEEP); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   private static void makeActions(Collection<DwarvenMineWorker> workers, |   private static void makeActions( | ||||||
|       DwarvenMineWorker.Action... actions) { |       Collection<DwarvenMineWorker> workers, | ||||||
|  |       DwarvenMineWorker.Action... actions | ||||||
|  |   ) { | ||||||
|     for (DwarvenMineWorker worker : workers) { |     for (DwarvenMineWorker worker : workers) { | ||||||
|       worker.action(actions); |       worker.action(actions); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -27,9 +27,7 @@ import org.slf4j.Logger; | |||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  | ||||||
|  * DwarvenMineWorker is one of the goldmine subsystems. |  * DwarvenMineWorker is one of the goldmine subsystems. | ||||||
|  * |  | ||||||
|  */ |  */ | ||||||
| public abstract class DwarvenMineWorker { | public abstract class DwarvenMineWorker { | ||||||
|  |  | ||||||
| @@ -75,7 +73,7 @@ public abstract class DwarvenMineWorker { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * Perform actions |    * Perform actions. | ||||||
|    */ |    */ | ||||||
|   public void action(Action... actions) { |   public void action(Action... actions) { | ||||||
|     for (Action action : actions) { |     for (Action action : actions) { | ||||||
|   | |||||||
| @@ -27,9 +27,7 @@ import org.slf4j.Logger; | |||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  | ||||||
|  * DwarvenTunnelDigger is one of the goldmine subsystems. |  * DwarvenTunnelDigger is one of the goldmine subsystems. | ||||||
|  * |  | ||||||
|  */ |  */ | ||||||
| public class DwarvenTunnelDigger extends DwarvenMineWorker { | public class DwarvenTunnelDigger extends DwarvenMineWorker { | ||||||
|  |  | ||||||
|   | |||||||
| @@ -27,17 +27,16 @@ import org.slf4j.Logger; | |||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Factory-kit is a creational pattern which defines a factory of immutable content |  * Factory-kit is a creational pattern which defines a factory of immutable content with separated | ||||||
|  * with separated builder and factory interfaces to deal with the problem of |  * builder and factory interfaces to deal with the problem of creating one of the objects specified | ||||||
|  * creating one of the objects specified directly in the factory-kit instance. |  * directly in the factory-kit instance. | ||||||
|  * |  * | ||||||
|  * <p> |  * <p>In the given example {@link WeaponFactory} represents the factory-kit, that contains four | ||||||
|  * In the given example {@link WeaponFactory} represents the factory-kit, that contains |  * {@link Builder}s for creating new objects of the classes implementing {@link Weapon} interface. | ||||||
|  * four {@link Builder}s for creating new objects of |  * | ||||||
|  * the classes implementing {@link Weapon} interface. |  * <p>Each of them can be called with {@link WeaponFactory#create(WeaponType)} method, with | ||||||
|  * <br>Each of them can be called with {@link WeaponFactory#create(WeaponType)} method, with |  * an input representing an instance of {@link WeaponType} that needs to be mapped explicitly with | ||||||
|  * an input representing an instance of {@link WeaponType} that needs to |  * desired class type in the factory instance. | ||||||
|  * be mapped explicitly with desired class type in the factory instance. |  | ||||||
|  */ |  */ | ||||||
| public class App { | public class App { | ||||||
|  |  | ||||||
|   | |||||||
| @@ -24,7 +24,7 @@ | |||||||
| package com.iluwatar.factorykit; | package com.iluwatar.factorykit; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Class representing Axe |  * Class representing Axe. | ||||||
|  */ |  */ | ||||||
| public class Axe implements Weapon { | public class Axe implements Weapon { | ||||||
|   @Override |   @Override | ||||||
|   | |||||||
| @@ -24,7 +24,7 @@ | |||||||
| package com.iluwatar.factorykit; | package com.iluwatar.factorykit; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Class representing Bows |  * Class representing Bows. | ||||||
|  */ |  */ | ||||||
| public class Bow implements Weapon { | public class Bow implements Weapon { | ||||||
|   @Override |   @Override | ||||||
|   | |||||||
| @@ -22,8 +22,9 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| package com.iluwatar.factorykit; | package com.iluwatar.factorykit; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Class representing Spear |  * Class representing Spear. | ||||||
|  */ |  */ | ||||||
| public class Spear implements Weapon { | public class Spear implements Weapon { | ||||||
|   @Override |   @Override | ||||||
|   | |||||||
| @@ -22,8 +22,9 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| package com.iluwatar.factorykit; | package com.iluwatar.factorykit; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Class representing Swords |  * Class representing Swords. | ||||||
|  */ |  */ | ||||||
| public class Sword implements Weapon { | public class Sword implements Weapon { | ||||||
|   @Override |   @Override | ||||||
|   | |||||||
| @@ -39,6 +39,7 @@ public interface WeaponFactory { | |||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * Creates an instance of the given type. |    * Creates an instance of the given type. | ||||||
|  |    * | ||||||
|    * @param name representing enum of an object type to be created. |    * @param name representing enum of an object type to be created. | ||||||
|    * @return new instance of a requested class implementing {@link Weapon} interface. |    * @return new instance of a requested class implementing {@link Weapon} interface. | ||||||
|    */ |    */ | ||||||
| @@ -46,6 +47,7 @@ public interface WeaponFactory { | |||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * Creates factory - placeholder for specified {@link Builder}s. |    * Creates factory - placeholder for specified {@link Builder}s. | ||||||
|  |    * | ||||||
|    * @param consumer for the new builder to the factory. |    * @param consumer for the new builder to the factory. | ||||||
|    * @return factory with specified {@link Builder}s |    * @return factory with specified {@link Builder}s | ||||||
|    */ |    */ | ||||||
|   | |||||||
| @@ -24,7 +24,7 @@ | |||||||
| package com.iluwatar.factorykit; | package com.iluwatar.factorykit; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Enumerates {@link Weapon} types |  * Enumerates {@link Weapon} types. | ||||||
|  */ |  */ | ||||||
| public enum WeaponType { | public enum WeaponType { | ||||||
|   SWORD, AXE, BOW, SPEAR |   SWORD, AXE, BOW, SPEAR | ||||||
|   | |||||||
| @@ -23,54 +23,60 @@ | |||||||
|  |  | ||||||
| package com.iluwatar.spatialpartition; | package com.iluwatar.spatialpartition; | ||||||
|  |  | ||||||
| import org.slf4j.Logger; |  | ||||||
| import org.slf4j.LoggerFactory; |  | ||||||
|  |  | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.Enumeration; | import java.util.Enumeration; | ||||||
| import java.util.Hashtable; | import java.util.Hashtable; | ||||||
| import java.util.Random; | import java.util.Random; | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * <p>The idea behind the <b>Spatial Partition</b> design pattern is to enable efficient location of objects  |  * <p>The idea behind the <b>Spatial Partition</b> design pattern is to enable efficient location | ||||||
|  * by storing them in a data structure that is organised by their positions. This is especially useful in the  |  * of objects by storing them in a data structure that is organised by their positions. This is | ||||||
|  * gaming world, where one may need to look up all the objects within a certain boundary, or near a certain  |  * especially useful in the gaming world, where one may need to look up all the objects within a | ||||||
|  * other object, repeatedly. The data structure can be used to store moving and static objects, though in order  |  * certain boundary, or near a certain other object, repeatedly. The data structure can be used to | ||||||
|  * to keep track of the moving objects, their positions will have to be reset each time they move. This would  |  * store moving and static objects, though in order to keep track of the moving objects, their | ||||||
|  * mean having to create a new instance of the data structure each frame, which would use up additional memory,  |  * positions will have to be reset each time they move. This would mean having to create a new | ||||||
|  * and so this pattern should only be used if one does not mind trading memory for speed and the number of  |  * instance of the data structure each frame, which would use up additional memory, and so this | ||||||
|  |  * pattern should only be used if one does not mind trading memory for speed and the number of | ||||||
|  * objects to keep track of is large to justify the use of the extra space.</p> |  * objects to keep track of is large to justify the use of the extra space.</p> | ||||||
|  * <p>In our example, we use <b>{@link QuadTree} data structure</b> which divides into 4 (quad) sub-sections when  |  * <p>In our example, we use <b>{@link QuadTree} data structure</b> which divides into 4 (quad) | ||||||
|  * the number of objects added to it exceeds a certain number (int field capacity). There is also a  |  * sub-sections when the number of objects added to it exceeds a certain number (int field | ||||||
|  * <b>{@link Rect}</b> class to define the boundary of the quadtree. We use an abstract class <b>{@link Point}</b>  |  * capacity). There is also a | ||||||
|  * with x and y coordinate fields and also an id field so that it can easily be put and looked up in the hashtable.  |  * <b>{@link Rect}</b> class to define the boundary of the quadtree. We use an abstract class | ||||||
|  * This class has abstract methods to define how the object moves (move()), when to check for collision with any  |  * <b>{@link Point}</b> | ||||||
|  * object (touches(obj)) and how to handle collision (handleCollision(obj)), and will be extended by any object  |  * with x and y coordinate fields and also an id field so that it can easily be put and looked up in | ||||||
|  * whose position has to be kept track of in the quadtree. The <b>{@link SpatialPartitionGeneric}</b> abstract class  |  * the hashtable. This class has abstract methods to define how the object moves (move()), when to | ||||||
|  * has 2 fields - a hashtable containing all objects (we use hashtable for faster lookups, insertion and deletion)  |  * check for collision with any object (touches(obj)) and how to handle collision | ||||||
|  * and a quadtree, and contains an abstract method which defines how to handle interactions between objects using  |  * (handleCollision(obj)), and will be extended by any object whose position has to be kept track of | ||||||
|  * the quadtree.</p> |  * in the quadtree. The <b>{@link SpatialPartitionGeneric}</b> abstract class has 2 fields - a | ||||||
|  * <p>Using the quadtree data structure will reduce the time complexity of finding the objects within a |  * hashtable containing all objects (we use hashtable for faster lookups, insertion and deletion) | ||||||
|  * certain range from <b>O(n^2) to O(nlogn)</b>, increasing the speed of computations immensely in case of |  * and a quadtree, and contains an abstract method which defines how to handle interactions between | ||||||
|  * large number of objects, which will have a positive effect on the rendering speed of the game.</p> |  * objects using the quadtree.</p> | ||||||
|  |  * <p>Using the quadtree data structure will reduce the time complexity of finding the objects | ||||||
|  |  * within a certain range from <b>O(n^2) to O(nlogn)</b>, increasing the speed of computations | ||||||
|  |  * immensely in case of large number of objects, which will have a positive effect on the rendering | ||||||
|  |  * speed of the game.</p> | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| public class App { | public class App { | ||||||
|   private static final Logger LOGGER = LoggerFactory.getLogger(App.class); |   private static final Logger LOGGER = LoggerFactory.getLogger(App.class); | ||||||
|  |  | ||||||
|   static void noSpatialPartition(int height, int width,  |   static void noSpatialPartition(int height, int width, | ||||||
|         int numOfMovements, Hashtable<Integer, Bubble> bubbles) { |                                  int numOfMovements, Hashtable<Integer, Bubble> bubbles) { | ||||||
|     ArrayList<Point> bubblesToCheck = new ArrayList<Point>(); |     ArrayList<Point> bubblesToCheck = new ArrayList<Point>(); | ||||||
|     for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements();) { |     for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements(); ) { | ||||||
|       bubblesToCheck.add(bubbles.get(e.nextElement())); //all bubbles have to be checked for collision for all bubbles |       bubblesToCheck.add(bubbles | ||||||
|  |           .get(e.nextElement())); //all bubbles have to be checked for collision for all bubbles | ||||||
|     } |     } | ||||||
|  |  | ||||||
|         //will run numOfMovement times or till all bubbles have popped |     //will run numOfMovement times or till all bubbles have popped | ||||||
|     while (numOfMovements > 0 && !bubbles.isEmpty()) { |     while (numOfMovements > 0 && !bubbles.isEmpty()) { | ||||||
|       for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements();) { |       for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements(); ) { | ||||||
|         Integer i = e.nextElement(); |         Integer i = e.nextElement(); | ||||||
|         //bubble moves, new position gets updated, collisions checked with all bubbles in bubblesToCheck |         // bubble moves, new position gets updated | ||||||
|         bubbles.get(i).move();   |         // and collisions are checked with all bubbles in bubblesToCheck | ||||||
|  |         bubbles.get(i).move(); | ||||||
|         bubbles.replace(i, bubbles.get(i)); |         bubbles.replace(i, bubbles.get(i)); | ||||||
|         bubbles.get(i).handleCollision(bubblesToCheck, bubbles); |         bubbles.get(i).handleCollision(bubblesToCheck, bubbles); | ||||||
|       } |       } | ||||||
| @@ -82,24 +88,24 @@ public class App { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   static void withSpatialPartition(int height, int width,  |   static void withSpatialPartition( | ||||||
|         int numOfMovements, Hashtable<Integer, Bubble> bubbles) { |       int height, int width, int numOfMovements, Hashtable<Integer, Bubble> bubbles) { | ||||||
|     //creating quadtree |     //creating quadtree | ||||||
|     Rect rect = new Rect(width / 2,height / 2,width,height); |     Rect rect = new Rect(width / 2, height / 2, width, height); | ||||||
|     QuadTree qTree = new QuadTree(rect, 4); |     QuadTree quadTree = new QuadTree(rect, 4); | ||||||
|      |  | ||||||
|     //will run numOfMovement times or till all bubbles have popped |     //will run numOfMovement times or till all bubbles have popped | ||||||
|     while (numOfMovements > 0 && !bubbles.isEmpty()) { |     while (numOfMovements > 0 && !bubbles.isEmpty()) { | ||||||
|       //quadtree updated each time |       //quadtree updated each time | ||||||
|       for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements();) { |       for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements(); ) { | ||||||
|         qTree.insert(bubbles.get(e.nextElement())); |         quadTree.insert(bubbles.get(e.nextElement())); | ||||||
|       } |       } | ||||||
|       for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements();) { |       for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements(); ) { | ||||||
|         Integer i = e.nextElement(); |         Integer i = e.nextElement(); | ||||||
|         //bubble moves, new position gets updated, quadtree used to reduce computations |         //bubble moves, new position gets updated, quadtree used to reduce computations | ||||||
|         bubbles.get(i).move();   |         bubbles.get(i).move(); | ||||||
|         bubbles.replace(i, bubbles.get(i)); |         bubbles.replace(i, bubbles.get(i)); | ||||||
|         SpatialPartitionBubbles sp = new SpatialPartitionBubbles(bubbles, qTree); |         SpatialPartitionBubbles sp = new SpatialPartitionBubbles(bubbles, quadTree); | ||||||
|         sp.handleCollisionsUsingQt(bubbles.get(i)); |         sp.handleCollisionsUsingQt(bubbles.get(i)); | ||||||
|       } |       } | ||||||
|       numOfMovements--; |       numOfMovements--; | ||||||
| @@ -109,7 +115,7 @@ public class App { | |||||||
|       LOGGER.info("Bubble " + key + " not popped"); |       LOGGER.info("Bubble " + key + " not popped"); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|    |  | ||||||
|   /** |   /** | ||||||
|    * Program entry point. |    * Program entry point. | ||||||
|    * |    * | ||||||
| @@ -124,14 +130,15 @@ public class App { | |||||||
|       Bubble b = new Bubble(rand.nextInt(300), rand.nextInt(300), i, rand.nextInt(2) + 1); |       Bubble b = new Bubble(rand.nextInt(300), rand.nextInt(300), i, rand.nextInt(2) + 1); | ||||||
|       bubbles1.put(i, b); |       bubbles1.put(i, b); | ||||||
|       bubbles2.put(i, b); |       bubbles2.put(i, b); | ||||||
|       LOGGER.info("Bubble " + i + " with radius " + b.radius + " added at (" + b.x + "," + b.y + ")"); |       LOGGER.info("Bubble " + i + " with radius " + b.radius | ||||||
|  |           + " added at (" + b.coordinateX + "," + b.coordinateY + ")"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     long start1 = System.currentTimeMillis(); |     long start1 = System.currentTimeMillis(); | ||||||
|     App.noSpatialPartition(300,300,20,bubbles1); |     App.noSpatialPartition(300, 300, 20, bubbles1); | ||||||
|     long end1 = System.currentTimeMillis(); |     long end1 = System.currentTimeMillis(); | ||||||
|     long start2 = System.currentTimeMillis(); |     long start2 = System.currentTimeMillis(); | ||||||
|     App.withSpatialPartition(300,300,20,bubbles2); |     App.withSpatialPartition(300, 300, 20, bubbles2); | ||||||
|     long end2 = System.currentTimeMillis(); |     long end2 = System.currentTimeMillis(); | ||||||
|     LOGGER.info("Without spatial partition takes " + (end1 - start1) + "ms"); |     LOGGER.info("Without spatial partition takes " + (end1 - start1) + "ms"); | ||||||
|     LOGGER.info("With spatial partition takes " + (end2 - start2) + "ms"); |     LOGGER.info("With spatial partition takes " + (end2 - start2) + "ms"); | ||||||
|   | |||||||
| @@ -23,16 +23,15 @@ | |||||||
|  |  | ||||||
| package com.iluwatar.spatialpartition; | package com.iluwatar.spatialpartition; | ||||||
|  |  | ||||||
| import org.slf4j.Logger; |  | ||||||
| import org.slf4j.LoggerFactory; |  | ||||||
|  |  | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.Hashtable; | import java.util.Hashtable; | ||||||
| import java.util.Random; | import java.util.Random; | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Bubble class extends Point. In this example, we create several bubbles in the field, |  * Bubble class extends Point. In this example, we create several bubbles in the field, let them | ||||||
|  * let them move and keep track of which ones have popped and which ones remain. |  * move and keep track of which ones have popped and which ones remain. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| public class Bubble extends Point<Bubble> { | public class Bubble extends Point<Bubble> { | ||||||
| @@ -42,24 +41,26 @@ public class Bubble extends Point<Bubble> { | |||||||
|   final int radius; |   final int radius; | ||||||
|  |  | ||||||
|   Bubble(int x, int y, int id, int radius) { |   Bubble(int x, int y, int id, int radius) { | ||||||
|     super(x,y,id); |     super(x, y, id); | ||||||
|     this.radius = radius; |     this.radius = radius; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void move() { |   void move() { | ||||||
|     //moves by 1 unit in either direction |     //moves by 1 unit in either direction | ||||||
|     this.x += RANDOM.nextInt(3) - 1; |     this.coordinateX += RANDOM.nextInt(3) - 1; | ||||||
|     this.y += RANDOM.nextInt(3) - 1; |     this.coordinateY += RANDOM.nextInt(3) - 1; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   boolean touches(Bubble b) { |   boolean touches(Bubble b) { | ||||||
|     //distance between them is greater than sum of radii (both sides of equation squared) |     //distance between them is greater than sum of radii (both sides of equation squared) | ||||||
|     return (this.x - b.x) * (this.x - b.x) + (this.y - b.y) * (this.y - b.y) |     return (this.coordinateX - b.coordinateX) * (this.coordinateX - b.coordinateX) | ||||||
|                 <= (this.radius + b.radius) * (this.radius + b.radius); |         + (this.coordinateY - b.coordinateY) * (this.coordinateY - b.coordinateY) | ||||||
|  |         <= (this.radius + b.radius) * (this.radius + b.radius); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void pop(Hashtable<Integer, Bubble> allBubbles) { |   void pop(Hashtable<Integer, Bubble> allBubbles) { | ||||||
|     LOGGER.info("Bubble " + this.id + " popped at (" + this.x + "," + this.y + ")!"); |     LOGGER.info("Bubble " + this.id | ||||||
|  |         + " popped at (" + this.coordinateX + "," + this.coordinateY + ")!"); | ||||||
|     allBubbles.remove(this.id); |     allBubbles.remove(this.id); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -68,8 +69,8 @@ public class Bubble extends Point<Bubble> { | |||||||
|     for (int i = 0; i < bubblesToCheck.size(); i++) { |     for (int i = 0; i < bubblesToCheck.size(); i++) { | ||||||
|       Integer otherId = bubblesToCheck.get(i).id; |       Integer otherId = bubblesToCheck.get(i).id; | ||||||
|       if (allBubbles.get(otherId) != null && //the bubble hasn't been popped yet |       if (allBubbles.get(otherId) != null && //the bubble hasn't been popped yet | ||||||
|                     this.id != otherId && //the two bubbles are not the same |           this.id != otherId && //the two bubbles are not the same | ||||||
|                     this.touches(allBubbles.get(otherId))) { //the bubbles touch |           this.touches(allBubbles.get(otherId))) { //the bubbles touch | ||||||
|         allBubbles.get(otherId).pop(allBubbles); |         allBubbles.get(otherId).pop(allBubbles); | ||||||
|         toBePopped = true; |         toBePopped = true; | ||||||
|       } |       } | ||||||
|   | |||||||
| @@ -27,40 +27,42 @@ import java.util.ArrayList; | |||||||
| import java.util.Hashtable; | import java.util.Hashtable; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * The abstract Point class which will be extended by any object in the field |  * The abstract Point class which will be extended by any object in the field whose location has to | ||||||
|  * whose location has to be kept track of. Defined by x,y coordinates and an id |  * be kept track of. Defined by x,y coordinates and an id for easy hashing into hashtable. | ||||||
|  * for easy hashing into hashtable. |  * | ||||||
|  * @param <T> T will be type subclass |  * @param <T> T will be type subclass | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| public abstract class Point<T> { | public abstract class Point<T> { | ||||||
|  |  | ||||||
|   public int x;  |   public int coordinateX; | ||||||
|   public int y;  |   public int coordinateY; | ||||||
|   public final int id; |   public final int id; | ||||||
|    |  | ||||||
|   Point(int x, int y, int id) { |   Point(int x, int y, int id) { | ||||||
|     this.x = x; |     this.coordinateX = x; | ||||||
|     this.y = y; |     this.coordinateY = y; | ||||||
|     this.id = id; |     this.id = id; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * defines how the object moves |    * defines how the object moves. | ||||||
|    */ |    */ | ||||||
|   abstract void move(); |   abstract void move(); | ||||||
|    |  | ||||||
|   /** |   /** | ||||||
|    * defines conditions for interacting with an object obj |    * defines conditions for interacting with an object obj. | ||||||
|  |    * | ||||||
|    * @param obj is another object on field which also extends Point |    * @param obj is another object on field which also extends Point | ||||||
|    * @return whether the object can interact with the other or not |    * @return whether the object can interact with the other or not | ||||||
|    */ |    */ | ||||||
|   abstract boolean touches(T obj); |   abstract boolean touches(T obj); | ||||||
|    |  | ||||||
|   /** |   /** | ||||||
|    * handling interactions/collisions with other objects |    * handling interactions/collisions with other objects. | ||||||
|  |    * | ||||||
|    * @param pointsToCheck contains the objects which need to be checked |    * @param pointsToCheck contains the objects which need to be checked | ||||||
|    * @param allPoints contains hashtable of all points on field at this time |    * @param allPoints     contains hashtable of all points on field at this time | ||||||
|    */ |    */ | ||||||
|   abstract void handleCollision(ArrayList<Point> pointsToCheck, Hashtable<Integer, T> allPoints); |   abstract void handleCollision(ArrayList<Point> pointsToCheck, Hashtable<Integer, T> allPoints); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -28,9 +28,9 @@ import java.util.Enumeration; | |||||||
| import java.util.Hashtable; | import java.util.Hashtable; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * The quadtree data structure is being used to keep track of the objects' locations. |  * The quadtree data structure is being used to keep track of the objects' locations. It has the | ||||||
|  * It has the insert(Point) and query(range) methods to insert a new object and find |  * insert(Point) and query(range) methods to insert a new object and find the objects within a | ||||||
|  * the objects within a certain (rectangular) range respectively. |  * certain (rectangular) range respectively. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| public class QuadTree { | public class QuadTree { | ||||||
| @@ -48,9 +48,9 @@ public class QuadTree { | |||||||
|     this.capacity = capacity; |     this.capacity = capacity; | ||||||
|     this.divided = false; |     this.divided = false; | ||||||
|     this.points = new Hashtable<Integer, Point>(); |     this.points = new Hashtable<Integer, Point>(); | ||||||
|     this.northwest = null;  |     this.northwest = null; | ||||||
|     this.northeast = null;  |     this.northeast = null; | ||||||
|     this.southwest = null;  |     this.southwest = null; | ||||||
|     this.southeast = null; |     this.southeast = null; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -76,25 +76,25 @@ public class QuadTree { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   void divide() { |   void divide() { | ||||||
|     Rect nw = new Rect(this.boundary.x - this.boundary.width / 4, this.boundary.y + this.boundary.height / 4,  |     double x = this.boundary.coordinateX; | ||||||
|           this.boundary.width / 2, this.boundary.height / 2); |     double y = this.boundary.coordinateY; | ||||||
|     this.northwest = new QuadTree(nw , this.capacity); |     double width = this.boundary.width; | ||||||
|     Rect ne = new Rect(this.boundary.x + this.boundary.width / 4, this.boundary.y + this.boundary.height / 4,  |     double height = this.boundary.height; | ||||||
|           this.boundary.width / 2, this.boundary.height / 2); |     Rect nw = new Rect(x - width / 4, y + height / 4, width / 2, height / 2); | ||||||
|     this.northeast = new QuadTree(ne , this.capacity); |     this.northwest = new QuadTree(nw, this.capacity); | ||||||
|     Rect sw = new Rect(this.boundary.x - this.boundary.width / 4, this.boundary.y - this.boundary.height / 4,  |     Rect ne = new Rect(x + width / 4, y + height / 4, width / 2, height / 2); | ||||||
|           this.boundary.width / 2, this.boundary.height / 2); |     this.northeast = new QuadTree(ne, this.capacity); | ||||||
|     this.southwest = new QuadTree(sw , this.capacity); |     Rect sw = new Rect(x - width / 4, y - height / 4, width / 2, height / 2); | ||||||
|     Rect se = new Rect(this.boundary.x + this.boundary.width / 4, this.boundary.y - this.boundary.height / 4,  |     this.southwest = new QuadTree(sw, this.capacity); | ||||||
|           this.boundary.width / 2, this.boundary.height / 2); |     Rect se = new Rect(x + width / 4, y - height / 4, width / 2, height / 2); | ||||||
|     this.southeast = new QuadTree(se , this.capacity); |     this.southeast = new QuadTree(se, this.capacity); | ||||||
|     this.divided = true; |     this.divided = true; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   ArrayList<Point> query(Rect r, ArrayList<Point> relevantPoints) { |   ArrayList<Point> query(Rect r, ArrayList<Point> relevantPoints) { | ||||||
|     //could also be a circle instead of a rectangle |     //could also be a circle instead of a rectangle | ||||||
|     if (this.boundary.intersects(r)) { |     if (this.boundary.intersects(r)) { | ||||||
|       for (Enumeration<Integer> e = this.points.keys(); e.hasMoreElements();) { |       for (Enumeration<Integer> e = this.points.keys(); e.hasMoreElements(); ) { | ||||||
|         Integer i = e.nextElement(); |         Integer i = e.nextElement(); | ||||||
|         if (r.contains(this.points.get(i))) { |         if (r.contains(this.points.get(i))) { | ||||||
|           relevantPoints.add(this.points.get(i)); |           relevantPoints.add(this.points.get(i)); | ||||||
|   | |||||||
| @@ -24,35 +24,37 @@ | |||||||
| package com.iluwatar.spatialpartition; | package com.iluwatar.spatialpartition; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * The Rect class helps in defining the boundary of the quadtree and is also used to |  * The Rect class helps in defining the boundary of the quadtree and is also used to define the | ||||||
|  * define the range within which objects need to be found in our example. |  * range within which objects need to be found in our example. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| public class Rect { | public class Rect { | ||||||
|   double x; |   double coordinateX; | ||||||
|   double y; |   double coordinateY; | ||||||
|   double width; |   double width; | ||||||
|   double height; |   double height; | ||||||
|  |  | ||||||
|   //(x,y) - centre of rectangle |   //(x,y) - centre of rectangle | ||||||
|  |  | ||||||
|   Rect(double x, double y, double width, double height) { |   Rect(double x, double y, double width, double height) { | ||||||
|     this.x = x; |     this.coordinateX = x; | ||||||
|     this.y = y; |     this.coordinateY = y; | ||||||
|     this.width = width; |     this.width = width; | ||||||
|     this.height = height; |     this.height = height; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   boolean contains(Point p) { |   boolean contains(Point p) { | ||||||
|     return p.x >= this.x - this.width / 2 && p.x <= this.x + this.width / 2 |     return p.coordinateX >= this.coordinateX - this.width / 2 | ||||||
|         && p.y >= this.y - this.height / 2 && p.y <= this.y + this.height / 2; |         && p.coordinateX <= this.coordinateX + this.width / 2 | ||||||
|  |         && p.coordinateY >= this.coordinateY - this.height / 2 | ||||||
|  |         && p.coordinateY <= this.coordinateY + this.height / 2; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   boolean intersects(Rect other) { |   boolean intersects(Rect other) { | ||||||
|     return !(this.x + this.width / 2 <= other.x - other.width / 2  |     return !(this.coordinateX + this.width / 2 <= other.coordinateX - other.width / 2 | ||||||
|           || this.x - this.width / 2 >= other.x + other.width / 2  |         || this.coordinateX - this.width / 2 >= other.coordinateX + other.width / 2 | ||||||
|           || this.y + this.height / 2 <= other.y - other.height / 2  |         || this.coordinateY + this.height / 2 <= other.coordinateY - other.height / 2 | ||||||
|           || this.y - this.height / 2 >= other.y + other.height / 2); |         || this.coordinateY - this.height / 2 >= other.coordinateY + other.height / 2); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -27,25 +27,26 @@ import java.util.ArrayList; | |||||||
| import java.util.Hashtable; | import java.util.Hashtable; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * This class extends the generic SpatialPartition abstract class and is used in |  * This class extends the generic SpatialPartition abstract class and is used in our example to keep | ||||||
|  * our example to keep track of all the bubbles that collide, pop and stay un-popped. |  * track of all the bubbles that collide, pop and stay un-popped. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> { | public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> { | ||||||
|  |  | ||||||
|   Hashtable<Integer, Bubble> bubbles; |   final Hashtable<Integer, Bubble> bubbles; | ||||||
|   QuadTree qTree; |   final QuadTree quadTree; | ||||||
|  |  | ||||||
|   SpatialPartitionBubbles(Hashtable<Integer, Bubble> bubbles, QuadTree qTree) { |   SpatialPartitionBubbles(Hashtable<Integer, Bubble> bubbles, QuadTree quadTree) { | ||||||
|     this.bubbles = bubbles; |     this.bubbles = bubbles; | ||||||
|     this.qTree = qTree; |     this.quadTree = quadTree; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void handleCollisionsUsingQt(Bubble b) { |   void handleCollisionsUsingQt(Bubble b) { | ||||||
|     //finding points within area of a square drawn with centre same as centre of bubble and length = radius of bubble |     // finding points within area of a square drawn with centre same as | ||||||
|     Rect rect = new Rect(b.x, b.y, 2 * b.radius, 2 * b.radius); |     // centre of bubble and length = radius of bubble | ||||||
|  |     Rect rect = new Rect(b.coordinateX, b.coordinateY, 2 * b.radius, 2 * b.radius); | ||||||
|     ArrayList<Point> quadTreeQueryResult = new ArrayList<Point>(); |     ArrayList<Point> quadTreeQueryResult = new ArrayList<Point>(); | ||||||
|     this.qTree.query(rect, quadTreeQueryResult); |     this.quadTree.query(rect, quadTreeQueryResult); | ||||||
|     //handling these collisions |     //handling these collisions | ||||||
|     b.handleCollision(quadTreeQueryResult, this.bubbles); |     b.handleCollision(quadTreeQueryResult, this.bubbles); | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -26,18 +26,20 @@ package com.iluwatar.spatialpartition; | |||||||
| import java.util.Hashtable; | import java.util.Hashtable; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * This abstract class has 2 fields, one of which is a hashtable containing all objects |  * This abstract class has 2 fields, one of which is a hashtable containing all objects that | ||||||
|  * that currently exist on the field and a quadtree which keeps track of locations. |  * currently exist on the field and a quadtree which keeps track of locations. | ||||||
|  |  * | ||||||
|  * @param <T> T will be type of object (that extends Point) |  * @param <T> T will be type of object (that extends Point) | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| public abstract class SpatialPartitionGeneric<T> { | public abstract class SpatialPartitionGeneric<T> { | ||||||
|  |  | ||||||
|   Hashtable<Integer, T> playerPositions; |   Hashtable<Integer, T> playerPositions; | ||||||
|   QuadTree qTree; |   QuadTree quadTree; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * handles collisions for object obj using quadtree |    * handles collisions for object obj using quadtree. | ||||||
|  |    * | ||||||
|    * @param obj is the object for which collisions need to be checked |    * @param obj is the object for which collisions need to be checked | ||||||
|    */ |    */ | ||||||
|   abstract void handleCollisionsUsingQt(T obj); |   abstract void handleCollisionsUsingQt(T obj); | ||||||
|   | |||||||
| @@ -37,11 +37,11 @@ class BubbleTest { | |||||||
|   @Test |   @Test | ||||||
|   void moveTest() { |   void moveTest() { | ||||||
|     Bubble b = new Bubble(10,10,1,2); |     Bubble b = new Bubble(10,10,1,2); | ||||||
|     int initialX = b.x; |     int initialX = b.coordinateX; | ||||||
|     int initialY = b.y; |     int initialY = b.coordinateY; | ||||||
|     b.move(); |     b.move(); | ||||||
|     //change in x and y < |2| |     //change in x and y < |2| | ||||||
|     assertTrue((b.x - initialX < 2 && b.x - initialX > -2) && (b.y - initialY < 2 && b.y - initialY > -2)); |     assertTrue((b.coordinateX - initialX < 2 && b.coordinateX - initialX > -2) && (b.coordinateY - initialY < 2 && b.coordinateY - initialY > -2)); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Test |   @Test | ||||||
|   | |||||||
| @@ -27,9 +27,7 @@ import org.slf4j.Logger; | |||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  | ||||||
|  * Angry state. |  * Angry state. | ||||||
|  * |  | ||||||
|  */ |  */ | ||||||
| public class AngryState implements State { | public class AngryState implements State { | ||||||
|  |  | ||||||
|   | |||||||
| @@ -24,20 +24,18 @@ | |||||||
| package com.iluwatar.state; | package com.iluwatar.state; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  | ||||||
|  * In State pattern the container object has an internal state object that defines the current |  * In State pattern the container object has an internal state object that defines the current | ||||||
|  * behavior. The state object can be changed to alter the behavior. |  * behavior. The state object can be changed to alter the behavior. | ||||||
|  * <p> |  * | ||||||
|  * This can be a cleaner way for an object to change its behavior at runtime without resorting to |  * <p>This can be a cleaner way for an object to change its behavior at runtime without resorting | ||||||
|  * large monolithic conditional statements and thus improves maintainability. |  * to large monolithic conditional statements and thus improves maintainability. | ||||||
|  * <p> |  * | ||||||
|  * In this example the {@link Mammoth} changes its behavior as time passes by. |  * <p>In this example the {@link Mammoth} changes its behavior as time passes by. | ||||||
|  *  |  | ||||||
|  */ |  */ | ||||||
| public class App { | public class App { | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * Program entry point |    * Program entry point. | ||||||
|    */ |    */ | ||||||
|   public static void main(String[] args) { |   public static void main(String[] args) { | ||||||
|  |  | ||||||
|   | |||||||
| @@ -24,9 +24,7 @@ | |||||||
| package com.iluwatar.state; | package com.iluwatar.state; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  | ||||||
|  * Mammoth has internal state that defines its behavior. |  * Mammoth has internal state that defines its behavior. | ||||||
|  *  |  | ||||||
|  */ |  */ | ||||||
| public class Mammoth { | public class Mammoth { | ||||||
|  |  | ||||||
| @@ -37,7 +35,7 @@ public class Mammoth { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * Makes time pass for the mammoth |    * Makes time pass for the mammoth. | ||||||
|    */ |    */ | ||||||
|   public void timePasses() { |   public void timePasses() { | ||||||
|     if (state.getClass().equals(PeacefulState.class)) { |     if (state.getClass().equals(PeacefulState.class)) { | ||||||
|   | |||||||
| @@ -27,9 +27,7 @@ import org.slf4j.Logger; | |||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  | ||||||
|  * Peaceful state. |  * Peaceful state. | ||||||
|  * |  | ||||||
|  */ |  */ | ||||||
| public class PeacefulState implements State { | public class PeacefulState implements State { | ||||||
|  |  | ||||||
|   | |||||||
| @@ -24,9 +24,7 @@ | |||||||
| package com.iluwatar.state; | package com.iluwatar.state; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  | ||||||
|  * State interface. |  * State interface. | ||||||
|  *  |  | ||||||
|  */ |  */ | ||||||
| public interface State { | public interface State { | ||||||
|  |  | ||||||
|   | |||||||
| @@ -29,15 +29,13 @@ import org.slf4j.LoggerFactory; | |||||||
| /** | /** | ||||||
|  * Step Builder Pattern |  * Step Builder Pattern | ||||||
|  * |  * | ||||||
|  * <p> |  * <p><b>Intent</b> <br> | ||||||
|  * <b>Intent</b> <br> |  | ||||||
|  * An extension of the Builder pattern that fully guides the user through the creation of the object |  * An extension of the Builder pattern that fully guides the user through the creation of the object | ||||||
|  * with no chances of confusion. <br> |  * with no chances of confusion. <br> The user experience will be much more improved by the fact | ||||||
|  * The user experience will be much more improved by the fact that he will only see the next step |  * that he will only see the next step methods available, NO build method until is the right time to | ||||||
|  * methods available, NO build method until is the right time to build the object. |  * build the object. | ||||||
|  * |  * | ||||||
|  * <p> |  * <p><b>Implementation</b> <br> | ||||||
|  * <b>Implementation</b> <br> |  | ||||||
|  * The concept is simple: |  * The concept is simple: | ||||||
|  * <ul> |  * <ul> | ||||||
|  * |  * | ||||||
| @@ -49,13 +47,13 @@ import org.slf4j.LoggerFactory; | |||||||
|  * <li>Last step is the BuildStep, in charge of creating the object you need to build.</li> |  * <li>Last step is the BuildStep, in charge of creating the object you need to build.</li> | ||||||
|  * </ul> |  * </ul> | ||||||
|  * |  * | ||||||
|  * <p> |  * <p><b>Applicability</b> <br> | ||||||
|  * <b>Applicability</b> <br> |  | ||||||
|  * Use the Step Builder pattern when the algorithm for creating a complex object should be |  * Use the Step Builder pattern when the algorithm for creating a complex object should be | ||||||
|  * independent of the parts that make up the object and how they're assembled the construction |  * independent of the parts that make up the object and how they're assembled the construction | ||||||
|  * process must allow different representations for the object that's constructed when in the |  * process must allow different representations for the object that's constructed when in the | ||||||
|  * process of constructing the order is important. |  * process of constructing the order is important. | ||||||
|  * <p> |  * <br> | ||||||
|  |  * | ||||||
|  * @see <a href="http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html">http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html</a> |  * @see <a href="http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html">http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html</a> | ||||||
|  */ |  */ | ||||||
| public class App { | public class App { | ||||||
| @@ -63,8 +61,8 @@ public class App { | |||||||
|   private static final Logger LOGGER = LoggerFactory.getLogger(App.class); |   private static final Logger LOGGER = LoggerFactory.getLogger(App.class); | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * Program entry point |    * Program entry point. | ||||||
|    *  |    * | ||||||
|    * @param args command line args |    * @param args command line args | ||||||
|    */ |    */ | ||||||
|   public static void main(String[] args) { |   public static void main(String[] args) { | ||||||
|   | |||||||
| @@ -93,13 +93,13 @@ public class Character { | |||||||
|   public String toString() { |   public String toString() { | ||||||
|     var sb = new StringBuilder(); |     var sb = new StringBuilder(); | ||||||
|     sb.append("This is a ") |     sb.append("This is a ") | ||||||
|             .append(fighterClass != null ? fighterClass : wizardClass) |         .append(fighterClass != null ? fighterClass : wizardClass) | ||||||
|             .append(" named ") |         .append(" named ") | ||||||
|             .append(name) |         .append(name) | ||||||
|             .append(" armed with a ") |         .append(" armed with a ") | ||||||
|             .append(weapon != null ? weapon : spell != null ? spell : "with nothing") |         .append(weapon != null ? weapon : spell != null ? spell : "with nothing") | ||||||
|             .append(abilities != null ? " and wielding " + abilities + " abilities" : "") |         .append(abilities != null ? " and wielding " + abilities + " abilities" : "") | ||||||
|             .append('.'); |         .append('.'); | ||||||
|     return sb.toString(); |     return sb.toString(); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -31,7 +31,8 @@ import java.util.List; | |||||||
|  */ |  */ | ||||||
| public final class CharacterStepBuilder { | public final class CharacterStepBuilder { | ||||||
|  |  | ||||||
|   private CharacterStepBuilder() {} |   private CharacterStepBuilder() { | ||||||
|  |   } | ||||||
|  |  | ||||||
|   public static NameStep newBuilder() { |   public static NameStep newBuilder() { | ||||||
|     return new CharacterSteps(); |     return new CharacterSteps(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user