diff --git a/facade/src/main/java/com/iluwatar/facade/App.java b/facade/src/main/java/com/iluwatar/facade/App.java index c7c36dce0..8d1c34c39 100644 --- a/facade/src/main/java/com/iluwatar/facade/App.java +++ b/facade/src/main/java/com/iluwatar/facade/App.java @@ -24,23 +24,21 @@ package com.iluwatar.facade; /** - * * 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 * 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 * members required by client. These members access the system on behalf of the facade client and * hide the implementation details. - *

- * In this example the Facade is ({@link DwarvenGoldmineFacade}) and it provides a simpler interface - * to the goldmine subsystem. - * + * + *

In this example the Facade is ({@link DwarvenGoldmineFacade}) and it provides a simpler + * interface to the goldmine subsystem. */ public class App { /** - * Program entry point - * + * Program entry point. + * * @param args command line args */ public static void main(String[] args) { diff --git a/facade/src/main/java/com/iluwatar/facade/DwarvenCartOperator.java b/facade/src/main/java/com/iluwatar/facade/DwarvenCartOperator.java index f4f660cb9..8c40fffab 100644 --- a/facade/src/main/java/com/iluwatar/facade/DwarvenCartOperator.java +++ b/facade/src/main/java/com/iluwatar/facade/DwarvenCartOperator.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * * DwarvenCartOperator is one of the goldmine subsystems. - * */ public class DwarvenCartOperator extends DwarvenMineWorker { diff --git a/facade/src/main/java/com/iluwatar/facade/DwarvenGoldDigger.java b/facade/src/main/java/com/iluwatar/facade/DwarvenGoldDigger.java index f1427a73c..4d41a4633 100644 --- a/facade/src/main/java/com/iluwatar/facade/DwarvenGoldDigger.java +++ b/facade/src/main/java/com/iluwatar/facade/DwarvenGoldDigger.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * * DwarvenGoldDigger is one of the goldmine subsystems. - * */ public class DwarvenGoldDigger extends DwarvenMineWorker { diff --git a/facade/src/main/java/com/iluwatar/facade/DwarvenGoldmineFacade.java b/facade/src/main/java/com/iluwatar/facade/DwarvenGoldmineFacade.java index b49d46e93..9982e556b 100644 --- a/facade/src/main/java/com/iluwatar/facade/DwarvenGoldmineFacade.java +++ b/facade/src/main/java/com/iluwatar/facade/DwarvenGoldmineFacade.java @@ -27,25 +27,24 @@ import java.util.Collection; import java.util.List; /** - * - * 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 + * 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. */ public class DwarvenGoldmineFacade { private final List workers; /** - * Constructor + * Constructor. */ public DwarvenGoldmineFacade() { workers = List.of( - new DwarvenGoldDigger(), - new DwarvenCartOperator(), - new DwarvenTunnelDigger()); + new DwarvenGoldDigger(), + new DwarvenCartOperator(), + new DwarvenTunnelDigger()); } public void startNewDay() { @@ -60,8 +59,10 @@ public class DwarvenGoldmineFacade { makeActions(workers, DwarvenMineWorker.Action.GO_HOME, DwarvenMineWorker.Action.GO_TO_SLEEP); } - private static void makeActions(Collection workers, - DwarvenMineWorker.Action... actions) { + private static void makeActions( + Collection workers, + DwarvenMineWorker.Action... actions + ) { for (DwarvenMineWorker worker : workers) { worker.action(actions); } diff --git a/facade/src/main/java/com/iluwatar/facade/DwarvenMineWorker.java b/facade/src/main/java/com/iluwatar/facade/DwarvenMineWorker.java index 4a3e94f97..8263d71f2 100644 --- a/facade/src/main/java/com/iluwatar/facade/DwarvenMineWorker.java +++ b/facade/src/main/java/com/iluwatar/facade/DwarvenMineWorker.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * * DwarvenMineWorker is one of the goldmine subsystems. - * */ public abstract class DwarvenMineWorker { @@ -75,7 +73,7 @@ public abstract class DwarvenMineWorker { } /** - * Perform actions + * Perform actions. */ public void action(Action... actions) { for (Action action : actions) { diff --git a/facade/src/main/java/com/iluwatar/facade/DwarvenTunnelDigger.java b/facade/src/main/java/com/iluwatar/facade/DwarvenTunnelDigger.java index e97f07d59..08cd3a15c 100644 --- a/facade/src/main/java/com/iluwatar/facade/DwarvenTunnelDigger.java +++ b/facade/src/main/java/com/iluwatar/facade/DwarvenTunnelDigger.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * * DwarvenTunnelDigger is one of the goldmine subsystems. - * */ public class DwarvenTunnelDigger extends DwarvenMineWorker { diff --git a/factory-kit/src/main/java/com/iluwatar/factorykit/App.java b/factory-kit/src/main/java/com/iluwatar/factorykit/App.java index 74a5d5b03..f767fb689 100644 --- a/factory-kit/src/main/java/com/iluwatar/factorykit/App.java +++ b/factory-kit/src/main/java/com/iluwatar/factorykit/App.java @@ -27,17 +27,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Factory-kit is a creational pattern which defines a factory of immutable content - * with separated builder and factory interfaces to deal with the problem of - * creating one of the objects specified directly in the factory-kit instance. + * Factory-kit is a creational pattern which defines a factory of immutable content with separated + * builder and factory interfaces to deal with the problem of creating one of the objects specified + * directly in the factory-kit instance. * - *

- * In the given example {@link WeaponFactory} represents the factory-kit, that contains - * four {@link Builder}s for creating new objects of - * the classes implementing {@link Weapon} interface. - *
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 desired class type in the factory instance. + *

In the given example {@link WeaponFactory} represents the factory-kit, that contains four + * {@link Builder}s for creating new objects of the classes implementing {@link Weapon} interface. + * + *

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 + * desired class type in the factory instance. */ public class App { diff --git a/factory-kit/src/main/java/com/iluwatar/factorykit/Axe.java b/factory-kit/src/main/java/com/iluwatar/factorykit/Axe.java index 7f8cb37b0..288026061 100644 --- a/factory-kit/src/main/java/com/iluwatar/factorykit/Axe.java +++ b/factory-kit/src/main/java/com/iluwatar/factorykit/Axe.java @@ -24,7 +24,7 @@ package com.iluwatar.factorykit; /** - * Class representing Axe + * Class representing Axe. */ public class Axe implements Weapon { @Override diff --git a/factory-kit/src/main/java/com/iluwatar/factorykit/Bow.java b/factory-kit/src/main/java/com/iluwatar/factorykit/Bow.java index 90413390c..24795eafa 100644 --- a/factory-kit/src/main/java/com/iluwatar/factorykit/Bow.java +++ b/factory-kit/src/main/java/com/iluwatar/factorykit/Bow.java @@ -24,7 +24,7 @@ package com.iluwatar.factorykit; /** - * Class representing Bows + * Class representing Bows. */ public class Bow implements Weapon { @Override diff --git a/factory-kit/src/main/java/com/iluwatar/factorykit/Spear.java b/factory-kit/src/main/java/com/iluwatar/factorykit/Spear.java index 0be3f4c2c..52d26613a 100644 --- a/factory-kit/src/main/java/com/iluwatar/factorykit/Spear.java +++ b/factory-kit/src/main/java/com/iluwatar/factorykit/Spear.java @@ -22,8 +22,9 @@ */ package com.iluwatar.factorykit; + /** - * Class representing Spear + * Class representing Spear. */ public class Spear implements Weapon { @Override diff --git a/factory-kit/src/main/java/com/iluwatar/factorykit/Sword.java b/factory-kit/src/main/java/com/iluwatar/factorykit/Sword.java index e49d45584..ea41e4ce5 100644 --- a/factory-kit/src/main/java/com/iluwatar/factorykit/Sword.java +++ b/factory-kit/src/main/java/com/iluwatar/factorykit/Sword.java @@ -22,8 +22,9 @@ */ package com.iluwatar.factorykit; + /** - * Class representing Swords + * Class representing Swords. */ public class Sword implements Weapon { @Override diff --git a/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponFactory.java b/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponFactory.java index 1364b45c6..2bdde87f7 100644 --- a/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponFactory.java +++ b/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponFactory.java @@ -39,6 +39,7 @@ public interface WeaponFactory { /** * Creates an instance of the given type. + * * @param name representing enum of an object type to be created. * @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. + * * @param consumer for the new builder to the factory. * @return factory with specified {@link Builder}s */ diff --git a/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponType.java b/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponType.java index 56ca03e23..9d59050c3 100644 --- a/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponType.java +++ b/factory-kit/src/main/java/com/iluwatar/factorykit/WeaponType.java @@ -24,7 +24,7 @@ package com.iluwatar.factorykit; /** - * Enumerates {@link Weapon} types + * Enumerates {@link Weapon} types. */ public enum WeaponType { SWORD, AXE, BOW, SPEAR diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java index 3e7a64b6c..9a24f2229 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java @@ -23,54 +23,60 @@ package com.iluwatar.spatialpartition; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; import java.util.Random; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - *

The idea behind the Spatial Partition design pattern is to enable efficient location of objects - * by storing them in a data structure that is organised by their positions. This is especially useful in the - * gaming world, where one may need to look up all the objects within a certain boundary, or near a certain - * other object, repeatedly. The data structure can be used to store moving and static objects, though in order - * to keep track of the moving objects, their positions will have to be reset each time they move. This would - * mean having to create a new 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 + *

The idea behind the Spatial Partition design pattern is to enable efficient location + * of objects by storing them in a data structure that is organised by their positions. This is + * especially useful in the gaming world, where one may need to look up all the objects within a + * certain boundary, or near a certain other object, repeatedly. The data structure can be used to + * store moving and static objects, though in order to keep track of the moving objects, their + * positions will have to be reset each time they move. This would mean having to create a new + * 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.

- *

In our example, we use {@link QuadTree} data structure which divides into 4 (quad) sub-sections when - * the number of objects added to it exceeds a certain number (int field capacity). There is also a - * {@link Rect} class to define the boundary of the quadtree. We use an abstract class {@link Point} - * with x and y coordinate fields and also an id field so that it can easily be put and looked up in the hashtable. - * This class has abstract methods to define how the object moves (move()), when to check for collision with any - * object (touches(obj)) and how to handle collision (handleCollision(obj)), and will be extended by any object - * whose position has to be kept track of in the quadtree. The {@link SpatialPartitionGeneric} abstract class - * has 2 fields - a hashtable containing all objects (we use hashtable for faster lookups, insertion and deletion) - * and a quadtree, and contains an abstract method which defines how to handle interactions between objects using - * the quadtree.

- *

Using the quadtree data structure will reduce the time complexity of finding the objects within a - * certain range from O(n^2) to O(nlogn), 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.

+ *

In our example, we use {@link QuadTree} data structure which divides into 4 (quad) + * sub-sections when the number of objects added to it exceeds a certain number (int field + * capacity). There is also a + * {@link Rect} class to define the boundary of the quadtree. We use an abstract class + * {@link Point} + * with x and y coordinate fields and also an id field so that it can easily be put and looked up in + * the hashtable. This class has abstract methods to define how the object moves (move()), when to + * check for collision with any object (touches(obj)) and how to handle collision + * (handleCollision(obj)), and will be extended by any object whose position has to be kept track of + * in the quadtree. The {@link SpatialPartitionGeneric} abstract class has 2 fields - a + * hashtable containing all objects (we use hashtable for faster lookups, insertion and deletion) + * and a quadtree, and contains an abstract method which defines how to handle interactions between + * objects using the quadtree.

+ *

Using the quadtree data structure will reduce the time complexity of finding the objects + * within a certain range from O(n^2) to O(nlogn), 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.

*/ public class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); - static void noSpatialPartition(int height, int width, - int numOfMovements, Hashtable bubbles) { + static void noSpatialPartition(int height, int width, + int numOfMovements, Hashtable bubbles) { ArrayList bubblesToCheck = new ArrayList(); - for (Enumeration e = bubbles.keys(); e.hasMoreElements();) { - bubblesToCheck.add(bubbles.get(e.nextElement())); //all bubbles have to be checked for collision for all bubbles + for (Enumeration e = bubbles.keys(); e.hasMoreElements(); ) { + 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()) { - for (Enumeration e = bubbles.keys(); e.hasMoreElements();) { + for (Enumeration e = bubbles.keys(); e.hasMoreElements(); ) { Integer i = e.nextElement(); - //bubble moves, new position gets updated, collisions checked with all bubbles in bubblesToCheck - bubbles.get(i).move(); + // bubble moves, new position gets updated + // and collisions are checked with all bubbles in bubblesToCheck + bubbles.get(i).move(); bubbles.replace(i, bubbles.get(i)); bubbles.get(i).handleCollision(bubblesToCheck, bubbles); } @@ -82,24 +88,24 @@ public class App { } } - static void withSpatialPartition(int height, int width, - int numOfMovements, Hashtable bubbles) { + static void withSpatialPartition( + int height, int width, int numOfMovements, Hashtable bubbles) { //creating quadtree - Rect rect = new Rect(width / 2,height / 2,width,height); - QuadTree qTree = new QuadTree(rect, 4); - + Rect rect = new Rect(width / 2, height / 2, width, height); + QuadTree quadTree = new QuadTree(rect, 4); + //will run numOfMovement times or till all bubbles have popped while (numOfMovements > 0 && !bubbles.isEmpty()) { //quadtree updated each time - for (Enumeration e = bubbles.keys(); e.hasMoreElements();) { - qTree.insert(bubbles.get(e.nextElement())); + for (Enumeration e = bubbles.keys(); e.hasMoreElements(); ) { + quadTree.insert(bubbles.get(e.nextElement())); } - for (Enumeration e = bubbles.keys(); e.hasMoreElements();) { + for (Enumeration e = bubbles.keys(); e.hasMoreElements(); ) { Integer i = e.nextElement(); //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)); - SpatialPartitionBubbles sp = new SpatialPartitionBubbles(bubbles, qTree); + SpatialPartitionBubbles sp = new SpatialPartitionBubbles(bubbles, quadTree); sp.handleCollisionsUsingQt(bubbles.get(i)); } numOfMovements--; @@ -109,7 +115,7 @@ public class App { LOGGER.info("Bubble " + key + " not popped"); } } - + /** * 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); bubbles1.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(); - App.noSpatialPartition(300,300,20,bubbles1); + App.noSpatialPartition(300, 300, 20, bubbles1); long end1 = System.currentTimeMillis(); long start2 = System.currentTimeMillis(); - App.withSpatialPartition(300,300,20,bubbles2); + App.withSpatialPartition(300, 300, 20, bubbles2); long end2 = System.currentTimeMillis(); LOGGER.info("Without spatial partition takes " + (end1 - start1) + "ms"); LOGGER.info("With spatial partition takes " + (end2 - start2) + "ms"); diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java index 8a11c4f88..57dc06491 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java @@ -23,16 +23,15 @@ package com.iluwatar.spatialpartition; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.Hashtable; 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, - * let them move and keep track of which ones have popped and which ones remain. + * Bubble class extends Point. In this example, we create several bubbles in the field, let them + * move and keep track of which ones have popped and which ones remain. */ public class Bubble extends Point { @@ -42,24 +41,26 @@ public class Bubble extends Point { final int radius; Bubble(int x, int y, int id, int radius) { - super(x,y,id); + super(x, y, id); this.radius = radius; } void move() { //moves by 1 unit in either direction - this.x += RANDOM.nextInt(3) - 1; - this.y += RANDOM.nextInt(3) - 1; + this.coordinateX += RANDOM.nextInt(3) - 1; + this.coordinateY += RANDOM.nextInt(3) - 1; } boolean touches(Bubble b) { //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) - <= (this.radius + b.radius) * (this.radius + b.radius); + return (this.coordinateX - b.coordinateX) * (this.coordinateX - b.coordinateX) + + (this.coordinateY - b.coordinateY) * (this.coordinateY - b.coordinateY) + <= (this.radius + b.radius) * (this.radius + b.radius); } void pop(Hashtable 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); } @@ -68,8 +69,8 @@ public class Bubble extends Point { for (int i = 0; i < bubblesToCheck.size(); i++) { Integer otherId = bubblesToCheck.get(i).id; if (allBubbles.get(otherId) != null && //the bubble hasn't been popped yet - this.id != otherId && //the two bubbles are not the same - this.touches(allBubbles.get(otherId))) { //the bubbles touch + this.id != otherId && //the two bubbles are not the same + this.touches(allBubbles.get(otherId))) { //the bubbles touch allBubbles.get(otherId).pop(allBubbles); toBePopped = true; } diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Point.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Point.java index 3b5fb963f..ed4fb22cd 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Point.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Point.java @@ -27,40 +27,42 @@ import java.util.ArrayList; import java.util.Hashtable; /** - * The abstract Point class which will be extended by any object in the field - * whose location has to be kept track of. Defined by x,y coordinates and an id - * for easy hashing into hashtable. + * The abstract Point class which will be extended by any object in the field whose location has to + * be kept track of. Defined by x,y coordinates and an id for easy hashing into hashtable. + * * @param T will be type subclass */ public abstract class Point { - public int x; - public int y; + public int coordinateX; + public int coordinateY; public final int id; - + Point(int x, int y, int id) { - this.x = x; - this.y = y; + this.coordinateX = x; + this.coordinateY = y; this.id = id; } /** - * defines how the object moves + * defines how the object moves. */ 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 * @return whether the object can interact with the other or not */ 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 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 pointsToCheck, Hashtable allPoints); } diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/QuadTree.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/QuadTree.java index 7c2a93bda..79731e6ac 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/QuadTree.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/QuadTree.java @@ -28,9 +28,9 @@ import java.util.Enumeration; import java.util.Hashtable; /** - * The quadtree data structure is being used to keep track of the objects' locations. - * It has the insert(Point) and query(range) methods to insert a new object and find - * the objects within a certain (rectangular) range respectively. + * The quadtree data structure is being used to keep track of the objects' locations. It has the + * insert(Point) and query(range) methods to insert a new object and find the objects within a + * certain (rectangular) range respectively. */ public class QuadTree { @@ -48,9 +48,9 @@ public class QuadTree { this.capacity = capacity; this.divided = false; this.points = new Hashtable(); - this.northwest = null; - this.northeast = null; - this.southwest = null; + this.northwest = null; + this.northeast = null; + this.southwest = null; this.southeast = null; } @@ -76,25 +76,25 @@ public class QuadTree { } void divide() { - Rect nw = new Rect(this.boundary.x - this.boundary.width / 4, this.boundary.y + this.boundary.height / 4, - this.boundary.width / 2, this.boundary.height / 2); - this.northwest = new QuadTree(nw , this.capacity); - Rect ne = new Rect(this.boundary.x + this.boundary.width / 4, this.boundary.y + this.boundary.height / 4, - this.boundary.width / 2, this.boundary.height / 2); - this.northeast = new QuadTree(ne , this.capacity); - Rect sw = new Rect(this.boundary.x - this.boundary.width / 4, this.boundary.y - this.boundary.height / 4, - this.boundary.width / 2, this.boundary.height / 2); - this.southwest = new QuadTree(sw , this.capacity); - Rect se = new Rect(this.boundary.x + this.boundary.width / 4, this.boundary.y - this.boundary.height / 4, - this.boundary.width / 2, this.boundary.height / 2); - this.southeast = new QuadTree(se , this.capacity); + double x = this.boundary.coordinateX; + double y = this.boundary.coordinateY; + double width = this.boundary.width; + double height = this.boundary.height; + Rect nw = new Rect(x - width / 4, y + height / 4, width / 2, height / 2); + this.northwest = new QuadTree(nw, this.capacity); + Rect ne = new Rect(x + width / 4, y + height / 4, width / 2, height / 2); + this.northeast = new QuadTree(ne, this.capacity); + Rect sw = new Rect(x - width / 4, y - height / 4, width / 2, height / 2); + this.southwest = new QuadTree(sw, this.capacity); + Rect se = new Rect(x + width / 4, y - height / 4, width / 2, height / 2); + this.southeast = new QuadTree(se, this.capacity); this.divided = true; } ArrayList query(Rect r, ArrayList relevantPoints) { //could also be a circle instead of a rectangle if (this.boundary.intersects(r)) { - for (Enumeration e = this.points.keys(); e.hasMoreElements();) { + for (Enumeration e = this.points.keys(); e.hasMoreElements(); ) { Integer i = e.nextElement(); if (r.contains(this.points.get(i))) { relevantPoints.add(this.points.get(i)); diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Rect.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Rect.java index 6142c99d8..02acfb640 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Rect.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Rect.java @@ -24,35 +24,37 @@ package com.iluwatar.spatialpartition; /** - * The Rect class helps in defining the boundary of the quadtree and is also used to - * define the range within which objects need to be found in our example. + * The Rect class helps in defining the boundary of the quadtree and is also used to define the + * range within which objects need to be found in our example. */ public class Rect { - double x; - double y; + double coordinateX; + double coordinateY; double width; double height; //(x,y) - centre of rectangle Rect(double x, double y, double width, double height) { - this.x = x; - this.y = y; + this.coordinateX = x; + this.coordinateY = y; this.width = width; this.height = height; } boolean contains(Point p) { - return p.x >= this.x - this.width / 2 && p.x <= this.x + this.width / 2 - && p.y >= this.y - this.height / 2 && p.y <= this.y + this.height / 2; + return p.coordinateX >= this.coordinateX - this.width / 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) { - return !(this.x + this.width / 2 <= other.x - other.width / 2 - || this.x - this.width / 2 >= other.x + other.width / 2 - || this.y + this.height / 2 <= other.y - other.height / 2 - || this.y - this.height / 2 >= other.y + other.height / 2); + return !(this.coordinateX + this.width / 2 <= other.coordinateX - other.width / 2 + || this.coordinateX - this.width / 2 >= other.coordinateX + other.width / 2 + || this.coordinateY + this.height / 2 <= other.coordinateY - other.height / 2 + || this.coordinateY - this.height / 2 >= other.coordinateY + other.height / 2); } } diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionBubbles.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionBubbles.java index 48714aa0a..3dd6c113b 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionBubbles.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionBubbles.java @@ -27,25 +27,26 @@ import java.util.ArrayList; import java.util.Hashtable; /** - * This class extends the generic SpatialPartition abstract class and is used in - * our example to keep track of all the bubbles that collide, pop and stay un-popped. + * This class extends the generic SpatialPartition abstract class and is used in our example to keep + * track of all the bubbles that collide, pop and stay un-popped. */ public class SpatialPartitionBubbles extends SpatialPartitionGeneric { - Hashtable bubbles; - QuadTree qTree; + final Hashtable bubbles; + final QuadTree quadTree; - SpatialPartitionBubbles(Hashtable bubbles, QuadTree qTree) { + SpatialPartitionBubbles(Hashtable bubbles, QuadTree quadTree) { this.bubbles = bubbles; - this.qTree = qTree; + this.quadTree = quadTree; } void handleCollisionsUsingQt(Bubble b) { - //finding points within area of a square drawn with centre same as centre of bubble and length = radius of bubble - Rect rect = new Rect(b.x, b.y, 2 * b.radius, 2 * b.radius); + // finding points within area of a square drawn with centre same as + // centre of bubble and length = radius of bubble + Rect rect = new Rect(b.coordinateX, b.coordinateY, 2 * b.radius, 2 * b.radius); ArrayList quadTreeQueryResult = new ArrayList(); - this.qTree.query(rect, quadTreeQueryResult); + this.quadTree.query(rect, quadTreeQueryResult); //handling these collisions b.handleCollision(quadTreeQueryResult, this.bubbles); } diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionGeneric.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionGeneric.java index d5f1818ed..5ed73a7a1 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionGeneric.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionGeneric.java @@ -26,18 +26,20 @@ package com.iluwatar.spatialpartition; import java.util.Hashtable; /** - * This abstract class has 2 fields, one of which is a hashtable containing all objects - * that currently exist on the field and a quadtree which keeps track of locations. + * This abstract class has 2 fields, one of which is a hashtable containing all objects that + * currently exist on the field and a quadtree which keeps track of locations. + * * @param T will be type of object (that extends Point) */ public abstract class SpatialPartitionGeneric { Hashtable 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 */ abstract void handleCollisionsUsingQt(T obj); diff --git a/spatial-partition/src/test/java/com/iluwatar/spatialpartition/BubbleTest.java b/spatial-partition/src/test/java/com/iluwatar/spatialpartition/BubbleTest.java index e26de5b69..c9fab045b 100644 --- a/spatial-partition/src/test/java/com/iluwatar/spatialpartition/BubbleTest.java +++ b/spatial-partition/src/test/java/com/iluwatar/spatialpartition/BubbleTest.java @@ -37,11 +37,11 @@ class BubbleTest { @Test void moveTest() { Bubble b = new Bubble(10,10,1,2); - int initialX = b.x; - int initialY = b.y; + int initialX = b.coordinateX; + int initialY = b.coordinateY; b.move(); //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 diff --git a/state/src/main/java/com/iluwatar/state/AngryState.java b/state/src/main/java/com/iluwatar/state/AngryState.java index 0ce9f868c..8dc296c53 100644 --- a/state/src/main/java/com/iluwatar/state/AngryState.java +++ b/state/src/main/java/com/iluwatar/state/AngryState.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * * Angry state. - * */ public class AngryState implements State { diff --git a/state/src/main/java/com/iluwatar/state/App.java b/state/src/main/java/com/iluwatar/state/App.java index 86f019328..75c42c4aa 100644 --- a/state/src/main/java/com/iluwatar/state/App.java +++ b/state/src/main/java/com/iluwatar/state/App.java @@ -24,20 +24,18 @@ package com.iluwatar.state; /** - * * 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. - *

- * This can be a cleaner way for an object to change its behavior at runtime without resorting to - * large monolithic conditional statements and thus improves maintainability. - *

- * In this example the {@link Mammoth} changes its behavior as time passes by. - * + * + *

This can be a cleaner way for an object to change its behavior at runtime without resorting + * to large monolithic conditional statements and thus improves maintainability. + * + *

In this example the {@link Mammoth} changes its behavior as time passes by. */ public class App { /** - * Program entry point + * Program entry point. */ public static void main(String[] args) { diff --git a/state/src/main/java/com/iluwatar/state/Mammoth.java b/state/src/main/java/com/iluwatar/state/Mammoth.java index 0791815f0..1aa541849 100644 --- a/state/src/main/java/com/iluwatar/state/Mammoth.java +++ b/state/src/main/java/com/iluwatar/state/Mammoth.java @@ -24,9 +24,7 @@ package com.iluwatar.state; /** - * * Mammoth has internal state that defines its behavior. - * */ 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() { if (state.getClass().equals(PeacefulState.class)) { diff --git a/state/src/main/java/com/iluwatar/state/PeacefulState.java b/state/src/main/java/com/iluwatar/state/PeacefulState.java index 49586f877..ed83a0738 100644 --- a/state/src/main/java/com/iluwatar/state/PeacefulState.java +++ b/state/src/main/java/com/iluwatar/state/PeacefulState.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * * Peaceful state. - * */ public class PeacefulState implements State { diff --git a/state/src/main/java/com/iluwatar/state/State.java b/state/src/main/java/com/iluwatar/state/State.java index c2ae69780..4e3e8f6dc 100644 --- a/state/src/main/java/com/iluwatar/state/State.java +++ b/state/src/main/java/com/iluwatar/state/State.java @@ -24,9 +24,7 @@ package com.iluwatar.state; /** - * * State interface. - * */ public interface State { diff --git a/step-builder/src/main/java/com/iluwatar/stepbuilder/App.java b/step-builder/src/main/java/com/iluwatar/stepbuilder/App.java index da301db6b..3a9021a0f 100644 --- a/step-builder/src/main/java/com/iluwatar/stepbuilder/App.java +++ b/step-builder/src/main/java/com/iluwatar/stepbuilder/App.java @@ -29,15 +29,13 @@ import org.slf4j.LoggerFactory; /** * Step Builder Pattern * - *

- * Intent
+ *

Intent
* An extension of the Builder pattern that fully guides the user through the creation of the object - * with no chances of confusion.
- * The user experience will be much more improved by the fact that he will only see the next step - * methods available, NO build method until is the right time to build the object. + * with no chances of confusion.
The user experience will be much more improved by the fact + * that he will only see the next step methods available, NO build method until is the right time to + * build the object. * - *

- * Implementation
+ *

Implementation
* The concept is simple: *

    * @@ -49,13 +47,13 @@ import org.slf4j.LoggerFactory; *
  • Last step is the BuildStep, in charge of creating the object you need to build.
  • *
* - *

- * Applicability
+ *

Applicability
* 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 * process must allow different representations for the object that's constructed when in the * process of constructing the order is important. - *

+ *
+ * * @see http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html */ public class App { @@ -63,8 +61,8 @@ public class App { private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** - * Program entry point - * + * Program entry point. + * * @param args command line args */ public static void main(String[] args) { diff --git a/step-builder/src/main/java/com/iluwatar/stepbuilder/Character.java b/step-builder/src/main/java/com/iluwatar/stepbuilder/Character.java index e643d19ff..ba460c06f 100644 --- a/step-builder/src/main/java/com/iluwatar/stepbuilder/Character.java +++ b/step-builder/src/main/java/com/iluwatar/stepbuilder/Character.java @@ -93,13 +93,13 @@ public class Character { public String toString() { var sb = new StringBuilder(); sb.append("This is a ") - .append(fighterClass != null ? fighterClass : wizardClass) - .append(" named ") - .append(name) - .append(" armed with a ") - .append(weapon != null ? weapon : spell != null ? spell : "with nothing") - .append(abilities != null ? " and wielding " + abilities + " abilities" : "") - .append('.'); + .append(fighterClass != null ? fighterClass : wizardClass) + .append(" named ") + .append(name) + .append(" armed with a ") + .append(weapon != null ? weapon : spell != null ? spell : "with nothing") + .append(abilities != null ? " and wielding " + abilities + " abilities" : "") + .append('.'); return sb.toString(); } } diff --git a/step-builder/src/main/java/com/iluwatar/stepbuilder/CharacterStepBuilder.java b/step-builder/src/main/java/com/iluwatar/stepbuilder/CharacterStepBuilder.java index e11698b2a..9eed4d8bc 100644 --- a/step-builder/src/main/java/com/iluwatar/stepbuilder/CharacterStepBuilder.java +++ b/step-builder/src/main/java/com/iluwatar/stepbuilder/CharacterStepBuilder.java @@ -31,7 +31,8 @@ import java.util.List; */ public final class CharacterStepBuilder { - private CharacterStepBuilder() {} + private CharacterStepBuilder() { + } public static NameStep newBuilder() { return new CharacterSteps();