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:
parent
2628cc0dfc
commit
c954a436ad
@ -24,22 +24,20 @@
|
||||
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.
|
||||
* <p>
|
||||
* In this example the Facade is ({@link DwarvenGoldmineFacade}) and it provides a simpler interface
|
||||
* to the goldmine subsystem.
|
||||
*
|
||||
* <p>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
|
||||
*/
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* DwarvenCartOperator is one of the goldmine subsystems.
|
||||
*
|
||||
*/
|
||||
public class DwarvenCartOperator extends DwarvenMineWorker {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* DwarvenGoldDigger is one of the goldmine subsystems.
|
||||
*
|
||||
*/
|
||||
public class DwarvenGoldDigger extends DwarvenMineWorker {
|
||||
|
||||
|
@ -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.
|
||||
*
|
||||
* <p>This makes the goldmine easier to operate and cuts the dependencies from the goldmine user to
|
||||
* the subsystems.
|
||||
*/
|
||||
public class DwarvenGoldmineFacade {
|
||||
|
||||
private final List<DwarvenMineWorker> 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<DwarvenMineWorker> workers,
|
||||
DwarvenMineWorker.Action... actions) {
|
||||
private static void makeActions(
|
||||
Collection<DwarvenMineWorker> workers,
|
||||
DwarvenMineWorker.Action... actions
|
||||
) {
|
||||
for (DwarvenMineWorker worker : workers) {
|
||||
worker.action(actions);
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* DwarvenTunnelDigger is one of the goldmine subsystems.
|
||||
*
|
||||
*/
|
||||
public class DwarvenTunnelDigger extends DwarvenMineWorker {
|
||||
|
||||
|
@ -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.
|
||||
*
|
||||
* <p>
|
||||
* 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.
|
||||
* <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 desired class type in the factory instance.
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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 {
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Class representing Axe
|
||||
* Class representing Axe.
|
||||
*/
|
||||
public class Axe implements Weapon {
|
||||
@Override
|
||||
|
@ -24,7 +24,7 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Class representing Bows
|
||||
* Class representing Bows.
|
||||
*/
|
||||
public class Bow implements Weapon {
|
||||
@Override
|
||||
|
@ -22,8 +22,9 @@
|
||||
*/
|
||||
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Class representing Spear
|
||||
* Class representing Spear.
|
||||
*/
|
||||
public class Spear implements Weapon {
|
||||
@Override
|
||||
|
@ -22,8 +22,9 @@
|
||||
*/
|
||||
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Class representing Swords
|
||||
* Class representing Swords.
|
||||
*/
|
||||
public class Sword implements Weapon {
|
||||
@Override
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -24,7 +24,7 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Enumerates {@link Weapon} types
|
||||
* Enumerates {@link Weapon} types.
|
||||
*/
|
||||
public enum WeaponType {
|
||||
SWORD, AXE, BOW, SPEAR
|
||||
|
@ -23,53 +23,59 @@
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* <p>The idea behind the <b>Spatial Partition</b> 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
|
||||
* <p>The idea behind the <b>Spatial Partition</b> 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.</p>
|
||||
* <p>In our example, we use <b>{@link QuadTree} data structure</b> 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
|
||||
* <b>{@link Rect}</b> class to define the boundary of the quadtree. We use an abstract class <b>{@link Point}</b>
|
||||
* 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 <b>{@link SpatialPartitionGeneric}</b> 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.</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>
|
||||
* <p>In our example, we use <b>{@link QuadTree} data structure</b> 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
|
||||
* <b>{@link Rect}</b> class to define the boundary of the quadtree. We use an abstract class
|
||||
* <b>{@link Point}</b>
|
||||
* 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 <b>{@link SpatialPartitionGeneric}</b> 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.</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 {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
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>();
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
//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<Integer> e = bubbles.keys(); e.hasMoreElements();) {
|
||||
for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements(); ) {
|
||||
Integer i = e.nextElement();
|
||||
//bubble moves, new position gets updated, collisions checked with all bubbles in bubblesToCheck
|
||||
// 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<Integer, Bubble> bubbles) {
|
||||
static void withSpatialPartition(
|
||||
int height, int width, int numOfMovements, Hashtable<Integer, Bubble> 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<Integer> e = bubbles.keys(); e.hasMoreElements();) {
|
||||
qTree.insert(bubbles.get(e.nextElement()));
|
||||
for (Enumeration<Integer> e = bubbles.keys(); e.hasMoreElements(); ) {
|
||||
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();
|
||||
//bubble moves, new position gets updated, quadtree used to reduce computations
|
||||
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--;
|
||||
@ -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");
|
||||
|
@ -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<Bubble> {
|
||||
@ -42,24 +41,26 @@ public class Bubble extends Point<Bubble> {
|
||||
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<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);
|
||||
}
|
||||
|
||||
@ -68,8 +69,8 @@ public class Bubble extends Point<Bubble> {
|
||||
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;
|
||||
}
|
||||
|
@ -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> T will be type subclass
|
||||
*/
|
||||
|
||||
public abstract class Point<T> {
|
||||
|
||||
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<Point> pointsToCheck, Hashtable<Integer, T> allPoints);
|
||||
}
|
||||
|
@ -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 {
|
||||
@ -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<Point> query(Rect r, ArrayList<Point> relevantPoints) {
|
||||
//could also be a circle instead of a rectangle
|
||||
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();
|
||||
if (r.contains(this.points.get(i))) {
|
||||
relevantPoints.add(this.points.get(i));
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<Bubble> {
|
||||
|
||||
Hashtable<Integer, Bubble> bubbles;
|
||||
QuadTree qTree;
|
||||
final Hashtable<Integer, Bubble> bubbles;
|
||||
final QuadTree quadTree;
|
||||
|
||||
SpatialPartitionBubbles(Hashtable<Integer, Bubble> bubbles, QuadTree qTree) {
|
||||
SpatialPartitionBubbles(Hashtable<Integer, Bubble> 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<Point> quadTreeQueryResult = new ArrayList<Point>();
|
||||
this.qTree.query(rect, quadTreeQueryResult);
|
||||
this.quadTree.query(rect, quadTreeQueryResult);
|
||||
//handling these collisions
|
||||
b.handleCollision(quadTreeQueryResult, this.bubbles);
|
||||
}
|
||||
|
@ -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> T will be type of object (that extends Point)
|
||||
*/
|
||||
|
||||
public abstract class SpatialPartitionGeneric<T> {
|
||||
|
||||
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
|
||||
*/
|
||||
abstract void handleCollisionsUsingQt(T obj);
|
||||
|
@ -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
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Angry state.
|
||||
*
|
||||
*/
|
||||
public class AngryState implements State {
|
||||
|
||||
|
@ -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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* In this example the {@link Mammoth} changes its behavior as time passes by.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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) {
|
||||
|
||||
|
@ -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)) {
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Peaceful state.
|
||||
*
|
||||
*/
|
||||
public class PeacefulState implements State {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.state;
|
||||
|
||||
/**
|
||||
*
|
||||
* State interface.
|
||||
*
|
||||
*/
|
||||
public interface State {
|
||||
|
||||
|
@ -29,15 +29,13 @@ import org.slf4j.LoggerFactory;
|
||||
/**
|
||||
* Step Builder Pattern
|
||||
*
|
||||
* <p>
|
||||
* <b>Intent</b> <br>
|
||||
* <p><b>Intent</b> <br>
|
||||
* An extension of the Builder pattern that fully guides the user through the creation of the object
|
||||
* with no chances of confusion. <br>
|
||||
* 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. <br> 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.
|
||||
*
|
||||
* <p>
|
||||
* <b>Implementation</b> <br>
|
||||
* <p><b>Implementation</b> <br>
|
||||
* The concept is simple:
|
||||
* <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>
|
||||
* </ul>
|
||||
*
|
||||
* <p>
|
||||
* <b>Applicability</b> <br>
|
||||
* <p><b>Applicability</b> <br>
|
||||
* 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.
|
||||
* <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>
|
||||
*/
|
||||
public class App {
|
||||
@ -63,7 +61,7 @@ public class App {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,8 @@ import java.util.List;
|
||||
*/
|
||||
public final class CharacterStepBuilder {
|
||||
|
||||
private CharacterStepBuilder() {}
|
||||
private CharacterStepBuilder() {
|
||||
}
|
||||
|
||||
public static NameStep newBuilder() {
|
||||
return new CharacterSteps();
|
||||
|
Loading…
x
Reference in New Issue
Block a user