Skip to content

Commit

Permalink
Resolves checkstyle errors for facade factory-kit spatial-partition s…
Browse files Browse the repository at this point in the history
…tate step-builder (iluwatar#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
  • Loading branch information
anuragagarwal561994 authored and iluwatar committed Nov 11, 2019
1 parent 2628cc0 commit c954a43
Show file tree
Hide file tree
Showing 29 changed files with 195 additions and 197 deletions.
12 changes: 5 additions & 7 deletions facade/src/main/java/com/iluwatar/facade/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <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
*/
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import org.slf4j.LoggerFactory;

/**
*
* DwarvenCartOperator is one of the goldmine subsystems.
*
*/
public class DwarvenCartOperator extends DwarvenMineWorker {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import org.slf4j.LoggerFactory;

/**
*
* DwarvenGoldDigger is one of the goldmine subsystems.
*
*/
public class DwarvenGoldDigger extends DwarvenMineWorker {

Expand Down
21 changes: 11 additions & 10 deletions facade/src/main/java/com/iluwatar/facade/DwarvenGoldmineFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,24 @@
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() {
Expand All @@ -60,8 +59,10 @@ public void endDay() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import org.slf4j.LoggerFactory;

/**
*
* DwarvenMineWorker is one of the goldmine subsystems.
*
*/
public abstract class DwarvenMineWorker {

Expand Down Expand Up @@ -75,7 +73,7 @@ private void action(Action action) {
}

/**
* Perform actions
* Perform actions.
*/
public void action(Action... actions) {
for (Action action : actions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import org.slf4j.LoggerFactory;

/**
*
* DwarvenTunnelDigger is one of the goldmine subsystems.
*
*/
public class DwarvenTunnelDigger extends DwarvenMineWorker {

Expand Down
19 changes: 9 additions & 10 deletions factory-kit/src/main/java/com/iluwatar/factorykit/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@
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 {

Expand Down
2 changes: 1 addition & 1 deletion factory-kit/src/main/java/com/iluwatar/factorykit/Axe.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package com.iluwatar.factorykit;

/**
* Class representing Axe
* Class representing Axe.
*/
public class Axe implements Weapon {
@Override
Expand Down
2 changes: 1 addition & 1 deletion factory-kit/src/main/java/com/iluwatar/factorykit/Bow.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package com.iluwatar.factorykit;

/**
* Class representing Bows
* Class representing Bows.
*/
public class Bow implements Weapon {
@Override
Expand Down
3 changes: 2 additions & 1 deletion factory-kit/src/main/java/com/iluwatar/factorykit/Spear.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
*/

package com.iluwatar.factorykit;

/**
* Class representing Spear
* Class representing Spear.
*/
public class Spear implements Weapon {
@Override
Expand Down
3 changes: 2 additions & 1 deletion factory-kit/src/main/java/com/iluwatar/factorykit/Sword.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
*/

package com.iluwatar.factorykit;

/**
* Class representing Swords
* Class representing Swords.
*/
public class Sword implements Weapon {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ 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.
*/
Weapon create(WeaponType name);

/**
* Creates factory - placeholder for specified {@link Builder}s.
*
* @param consumer for the new builder to the factory.
* @return factory with specified {@link Builder}s
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package com.iluwatar.factorykit;

/**
* Enumerates {@link Weapon} types
* Enumerates {@link Weapon} types.
*/
public enum WeaponType {
SWORD, AXE, BOW, SPEAR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <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) {
static void noSpatialPartition(int height, int width,
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
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);
}
Expand All @@ -82,24 +88,24 @@ static void noSpatialPartition(int height, int width,
}
}

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.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--;
Expand All @@ -109,7 +115,7 @@ static void withSpatialPartition(int height, int width,
LOGGER.info("Bubble " + key + " not popped");
}
}

/**
* Program entry point.
*
Expand All @@ -124,14 +130,15 @@ public static void main(String[] args) {
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");
Expand Down
Loading

0 comments on commit c954a43

Please sign in to comment.