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:
parent
663dbd298e
commit
3f09fb70bb
@ -23,13 +23,15 @@
|
||||
|
||||
package com.iluwatar.caching;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class CachingTest {
|
||||
class CachingTest {
|
||||
private App app;
|
||||
|
||||
/**
|
||||
@ -47,22 +49,26 @@ public class CachingTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadAndWriteThroughStrategy() {
|
||||
void testReadAndWriteThroughStrategy() {
|
||||
assertNotNull(app);
|
||||
app.useReadAndWriteThroughStrategy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadThroughAndWriteAroundStrategy() {
|
||||
void testReadThroughAndWriteAroundStrategy() {
|
||||
assertNotNull(app);
|
||||
app.useReadThroughAndWriteAroundStrategy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadThroughAndWriteBehindStrategy() {
|
||||
void testReadThroughAndWriteBehindStrategy() {
|
||||
assertNotNull(app);
|
||||
app.useReadThroughAndWriteBehindStrategy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheAsideStrategy() {
|
||||
void testCacheAsideStrategy() {
|
||||
assertNotNull(app);
|
||||
app.useCacheAsideStategy();
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
||||
package com.iluwatar.spatialpartition;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Hashtable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Random;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -58,13 +58,13 @@ public class Bubble extends Point<Bubble> {
|
||||
<= (this.radius + b.radius) * (this.radius + b.radius);
|
||||
}
|
||||
|
||||
void pop(Hashtable<Integer, Bubble> allBubbles) {
|
||||
LOGGER.info("Bubble " + this.id
|
||||
+ " popped at (" + this.coordinateX + "," + this.coordinateY + ")!");
|
||||
void pop(HashMap<Integer, Bubble> allBubbles) {
|
||||
LOGGER.info("Bubble ", this.id,
|
||||
" popped at (", this.coordinateX, ",", this.coordinateY, ")!");
|
||||
allBubbles.remove(this.id);
|
||||
}
|
||||
|
||||
void handleCollision(Collection<? extends Point> toCheck, Hashtable<Integer, Bubble> allBubbles) {
|
||||
void handleCollision(Collection<? extends Point> toCheck, HashMap<Integer, Bubble> allBubbles) {
|
||||
var toBePopped = false; //if any other bubble collides with it, made true
|
||||
for (var point : toCheck) {
|
||||
var otherId = point.id;
|
||||
|
@ -24,7 +24,7 @@
|
||||
package com.iluwatar.spatialpartition;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Hashtable;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* The abstract Point class which will be extended by any object in the field whose location has to
|
||||
@ -64,5 +64,5 @@ public abstract class Point<T> {
|
||||
* @param toCheck contains the objects which need to be checked
|
||||
* @param all contains hashtable of all points on field at this time
|
||||
*/
|
||||
abstract void handleCollision(Collection<? extends Point> toCheck, Hashtable<Integer, T> all);
|
||||
abstract void handleCollision(Collection<? extends Point> toCheck, HashMap<Integer, T> all);
|
||||
}
|
||||
|
@ -24,7 +24,7 @@
|
||||
package com.iluwatar.spatialpartition;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* This class extends the generic SpatialPartition abstract class and is used in our example to keep
|
||||
@ -33,10 +33,10 @@ import java.util.Hashtable;
|
||||
|
||||
public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> {
|
||||
|
||||
private final Hashtable<Integer, Bubble> bubbles;
|
||||
private final HashMap<Integer, Bubble> bubbles;
|
||||
private final QuadTree quadTree;
|
||||
|
||||
SpatialPartitionBubbles(Hashtable<Integer, Bubble> bubbles, QuadTree quadTree) {
|
||||
SpatialPartitionBubbles(HashMap<Integer, Bubble> bubbles, QuadTree quadTree) {
|
||||
this.bubbles = bubbles;
|
||||
this.quadTree = quadTree;
|
||||
}
|
||||
@ -44,7 +44,7 @@ public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> {
|
||||
void handleCollisionsUsingQt(Bubble b) {
|
||||
// finding points within area of a square drawn with centre same as
|
||||
// centre of bubble and length = radius of bubble
|
||||
var rect = new Rect(b.coordinateX, b.coordinateY, 2 * b.radius, 2 * b.radius);
|
||||
var rect = new Rect(b.coordinateX, b.coordinateY, 2D * b.radius, 2D * b.radius);
|
||||
var quadTreeQueryResult = new ArrayList<Point>();
|
||||
this.quadTree.query(rect, quadTreeQueryResult);
|
||||
//handling these collisions
|
||||
|
@ -29,7 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.HashMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
@ -63,11 +63,11 @@ class BubbleTest {
|
||||
void popTest() {
|
||||
var b1 = new Bubble(10, 10, 1, 2);
|
||||
var b2 = new Bubble(0, 0, 2, 2);
|
||||
var bubbles = new Hashtable<Integer, Bubble>();
|
||||
var bubbles = new HashMap<Integer, Bubble>();
|
||||
bubbles.put(1, b1);
|
||||
bubbles.put(2, b2);
|
||||
b1.pop(bubbles);
|
||||
//after popping, bubble no longer in hashtable containing all bubbles
|
||||
//after popping, bubble no longer in hashMap containing all bubbles
|
||||
assertNull(bubbles.get(1));
|
||||
assertNotNull(bubbles.get(2));
|
||||
}
|
||||
@ -77,7 +77,7 @@ class BubbleTest {
|
||||
var b1 = new Bubble(0, 0, 1, 2);
|
||||
var b2 = new Bubble(1, 1, 2, 1);
|
||||
var b3 = new Bubble(10, 10, 3, 1);
|
||||
var bubbles = new Hashtable<Integer, Bubble>();
|
||||
var bubbles = new HashMap<Integer, Bubble>();
|
||||
bubbles.put(1, b1);
|
||||
bubbles.put(2, b2);
|
||||
bubbles.put(3, b3);
|
||||
|
@ -26,7 +26,7 @@ package com.iluwatar.spatialpartition;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.HashMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
@ -41,7 +41,7 @@ class SpatialPartitionBubblesTest {
|
||||
var b2 = new Bubble(5, 5, 2, 1);
|
||||
var b3 = new Bubble(9, 9, 3, 1);
|
||||
var b4 = new Bubble(8, 8, 4, 2);
|
||||
var bubbles = new Hashtable<Integer, Bubble>();
|
||||
var bubbles = new HashMap<Integer, Bubble>();
|
||||
bubbles.put(1, b1);
|
||||
bubbles.put(2, b2);
|
||||
bubbles.put(3, b3);
|
||||
|
Loading…
x
Reference in New Issue
Block a user