Clear Sonar Blockers (#1643)

* remove debt from CachingTest

https://sonarcloud.io/project/issues?fileUuids=AW3G0SevwB6UiZzQNqXR&id=iluwatar_java-design-patterns&open=AXK0Ozo--CiGJS70dLl0&resolved=false
 
Signed-off-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>

* fixed few debts for Spatial Partition module

Mainly convertig Hashtable to HashMaps

Signed-off-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>

* fixed some logger norms

Signed-off-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>

* fixed few errors as it got mixed with the stash

Signed-off-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
This commit is contained in:
Subhrodip Mohanta
2021-01-30 17:07:52 +05:30
committed by GitHub
parent 663dbd298e
commit 3f09fb70bb
7 changed files with 40 additions and 34 deletions
@@ -23,7 +23,7 @@
package com.iluwatar.spatialpartition;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -44,11 +44,11 @@ import org.slf4j.LoggerFactory;
* <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
* the hashmap. 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)
* hashmap containing all objects (we use hashmap 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
@@ -61,7 +61,7 @@ public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
private static final String BUBBLE = "Bubble ";
static void noSpatialPartition(int numOfMovements, Hashtable<Integer, Bubble> bubbles) {
static void noSpatialPartition(int numOfMovements, HashMap<Integer, Bubble> bubbles) {
//all bubbles have to be checked for collision for all bubbles
var bubblesToCheck = bubbles.values();
@@ -81,9 +81,9 @@ public class App {
}
static void withSpatialPartition(
int height, int width, int numOfMovements, Hashtable<Integer, Bubble> bubbles) {
int height, int width, int numOfMovements, HashMap<Integer, Bubble> bubbles) {
//creating quadtree
var rect = new Rect(width / 2, height / 2, width, height);
var rect = new Rect(width / 2D, height / 2D, width, height);
var quadTree = new QuadTree(rect, 4);
//will run numOfMovement times or till all bubbles have popped
@@ -110,15 +110,15 @@ public class App {
*/
public static void main(String[] args) {
var bubbles1 = new Hashtable<Integer, Bubble>();
var bubbles2 = new Hashtable<Integer, Bubble>();
var bubbles1 = new HashMap<Integer, Bubble>();
var bubbles2 = new HashMap<Integer, Bubble>();
var rand = new Random();
for (int i = 0; i < 10000; i++) {
var 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.coordinateX + "," + b.coordinateY + ")");
LOGGER.info(BUBBLE, i, " with radius ", b.radius,
" added at (", b.coordinateX, ",", b.coordinateY + ")");
}
var start1 = System.currentTimeMillis();
@@ -127,8 +127,8 @@ public class App {
var start2 = System.currentTimeMillis();
App.withSpatialPartition(300, 300, 20, bubbles2);
var end2 = System.currentTimeMillis();
LOGGER.info("Without spatial partition takes " + (end1 - start1) + "ms");
LOGGER.info("With spatial partition takes " + (end2 - start2) + "ms");
LOGGER.info("Without spatial partition takes ", (end1 - start1), "ms");
LOGGER.info("With spatial partition takes ", (end2 - start2), "ms");
}
}