未验证 提交 3f09fb70 编写于 作者: S Subhrodip Mohanta 提交者: GitHub

Clear Sonar Blockers (#1643)

* remove debt from CachingTest

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

* fixed few debts for Spatial Partition module

Mainly convertig Hashtable to HashMaps
Signed-off-by: NSubhrodip Mohanta <subhrodipmohanta@gmail.com>

* fixed some logger norms
Signed-off-by: NSubhrodip Mohanta <subhrodipmohanta@gmail.com>

* fixed few errors as it got mixed with the stash
Signed-off-by: NSubhrodip Mohanta <subhrodipmohanta@gmail.com>
上级 663dbd29
...@@ -23,13 +23,15 @@ ...@@ -23,13 +23,15 @@
package com.iluwatar.caching; package com.iluwatar.caching;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
* Application test * Application test
*/ */
public class CachingTest { class CachingTest {
private App app; private App app;
/** /**
...@@ -47,22 +49,26 @@ public class CachingTest { ...@@ -47,22 +49,26 @@ public class CachingTest {
} }
@Test @Test
public void testReadAndWriteThroughStrategy() { void testReadAndWriteThroughStrategy() {
assertNotNull(app);
app.useReadAndWriteThroughStrategy(); app.useReadAndWriteThroughStrategy();
} }
@Test @Test
public void testReadThroughAndWriteAroundStrategy() { void testReadThroughAndWriteAroundStrategy() {
assertNotNull(app);
app.useReadThroughAndWriteAroundStrategy(); app.useReadThroughAndWriteAroundStrategy();
} }
@Test @Test
public void testReadThroughAndWriteBehindStrategy() { void testReadThroughAndWriteBehindStrategy() {
assertNotNull(app);
app.useReadThroughAndWriteBehindStrategy(); app.useReadThroughAndWriteBehindStrategy();
} }
@Test @Test
public void testCacheAsideStrategy() { void testCacheAsideStrategy() {
assertNotNull(app);
app.useCacheAsideStategy(); app.useCacheAsideStategy();
} }
} }
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
package com.iluwatar.spatialpartition; package com.iluwatar.spatialpartition;
import java.util.Hashtable; import java.util.HashMap;
import java.util.Random; import java.util.Random;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -44,11 +44,11 @@ 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 Rect}</b> class to define the boundary of the quadtree. We use an abstract class
* <b>{@link Point}</b> * <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 * 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 * 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 * (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 * 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 * and a quadtree, and contains an abstract method which defines how to handle interactions between
* objects using the quadtree.</p> * objects using the quadtree.</p>
* <p>Using the quadtree data structure will reduce the time complexity of finding the objects * <p>Using the quadtree data structure will reduce the time complexity of finding the objects
...@@ -61,7 +61,7 @@ public class App { ...@@ -61,7 +61,7 @@ public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class); private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
private static final String BUBBLE = "Bubble "; 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 //all bubbles have to be checked for collision for all bubbles
var bubblesToCheck = bubbles.values(); var bubblesToCheck = bubbles.values();
...@@ -81,9 +81,9 @@ public class App { ...@@ -81,9 +81,9 @@ public class App {
} }
static void withSpatialPartition( 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 //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); var quadTree = new QuadTree(rect, 4);
//will run numOfMovement times or till all bubbles have popped //will run numOfMovement times or till all bubbles have popped
...@@ -110,15 +110,15 @@ public class App { ...@@ -110,15 +110,15 @@ public class App {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
var bubbles1 = new Hashtable<Integer, Bubble>(); var bubbles1 = new HashMap<Integer, Bubble>();
var bubbles2 = new Hashtable<Integer, Bubble>(); var bubbles2 = new HashMap<Integer, Bubble>();
var rand = new Random(); var rand = new Random();
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
var b = new Bubble(rand.nextInt(300), rand.nextInt(300), i, rand.nextInt(2) + 1); var b = new Bubble(rand.nextInt(300), rand.nextInt(300), i, rand.nextInt(2) + 1);
bubbles1.put(i, b); bubbles1.put(i, b);
bubbles2.put(i, b); bubbles2.put(i, b);
LOGGER.info(BUBBLE + i + " with radius " + b.radius LOGGER.info(BUBBLE, i, " with radius ", b.radius,
+ " added at (" + b.coordinateX + "," + b.coordinateY + ")"); " added at (", b.coordinateX, ",", b.coordinateY + ")");
} }
var start1 = System.currentTimeMillis(); var start1 = System.currentTimeMillis();
...@@ -127,8 +127,8 @@ public class App { ...@@ -127,8 +127,8 @@ public class App {
var start2 = System.currentTimeMillis(); var start2 = System.currentTimeMillis();
App.withSpatialPartition(300, 300, 20, bubbles2); App.withSpatialPartition(300, 300, 20, bubbles2);
var end2 = System.currentTimeMillis(); var end2 = System.currentTimeMillis();
LOGGER.info("Without spatial partition takes " + (end1 - start1) + "ms"); LOGGER.info("Without spatial partition takes ", (end1 - start1), "ms");
LOGGER.info("With spatial partition takes " + (end2 - start2) + "ms"); LOGGER.info("With spatial partition takes ", (end2 - start2), "ms");
} }
} }
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
package com.iluwatar.spatialpartition; package com.iluwatar.spatialpartition;
import java.util.Collection; import java.util.Collection;
import java.util.Hashtable; import java.util.HashMap;
import java.util.Random; import java.util.Random;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -58,13 +58,13 @@ public class Bubble extends Point<Bubble> { ...@@ -58,13 +58,13 @@ public class Bubble extends Point<Bubble> {
<= (this.radius + b.radius) * (this.radius + b.radius); <= (this.radius + b.radius) * (this.radius + b.radius);
} }
void pop(Hashtable<Integer, Bubble> allBubbles) { void pop(HashMap<Integer, Bubble> allBubbles) {
LOGGER.info("Bubble " + this.id LOGGER.info("Bubble ", this.id,
+ " popped at (" + this.coordinateX + "," + this.coordinateY + ")!"); " popped at (", this.coordinateX, ",", this.coordinateY, ")!");
allBubbles.remove(this.id); 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 var toBePopped = false; //if any other bubble collides with it, made true
for (var point : toCheck) { for (var point : toCheck) {
var otherId = point.id; var otherId = point.id;
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
package com.iluwatar.spatialpartition; package com.iluwatar.spatialpartition;
import java.util.Collection; 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 * 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> { ...@@ -64,5 +64,5 @@ public abstract class Point<T> {
* @param toCheck contains the objects which need to be checked * @param toCheck contains the objects which need to be checked
* @param all contains hashtable of all points on field at this time * @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 @@ ...@@ -24,7 +24,7 @@
package com.iluwatar.spatialpartition; package com.iluwatar.spatialpartition;
import java.util.ArrayList; 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 * This class extends the generic SpatialPartition abstract class and is used in our example to keep
...@@ -33,10 +33,10 @@ import java.util.Hashtable; ...@@ -33,10 +33,10 @@ import java.util.Hashtable;
public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> { public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> {
private final Hashtable<Integer, Bubble> bubbles; private final HashMap<Integer, Bubble> bubbles;
private final QuadTree quadTree; private final QuadTree quadTree;
SpatialPartitionBubbles(Hashtable<Integer, Bubble> bubbles, QuadTree quadTree) { SpatialPartitionBubbles(HashMap<Integer, Bubble> bubbles, QuadTree quadTree) {
this.bubbles = bubbles; this.bubbles = bubbles;
this.quadTree = quadTree; this.quadTree = quadTree;
} }
...@@ -44,7 +44,7 @@ public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> { ...@@ -44,7 +44,7 @@ public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> {
void handleCollisionsUsingQt(Bubble b) { void handleCollisionsUsingQt(Bubble b) {
// finding points within area of a square drawn with centre same as // finding points within area of a square drawn with centre same as
// centre of bubble and length = radius of bubble // 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>(); var quadTreeQueryResult = new ArrayList<Point>();
this.quadTree.query(rect, quadTreeQueryResult); this.quadTree.query(rect, quadTreeQueryResult);
//handling these collisions //handling these collisions
......
...@@ -29,7 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; ...@@ -29,7 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Hashtable; import java.util.HashMap;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
...@@ -63,11 +63,11 @@ class BubbleTest { ...@@ -63,11 +63,11 @@ class BubbleTest {
void popTest() { void popTest() {
var b1 = new Bubble(10, 10, 1, 2); var b1 = new Bubble(10, 10, 1, 2);
var b2 = new Bubble(0, 0, 2, 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(1, b1);
bubbles.put(2, b2); bubbles.put(2, b2);
b1.pop(bubbles); 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)); assertNull(bubbles.get(1));
assertNotNull(bubbles.get(2)); assertNotNull(bubbles.get(2));
} }
...@@ -77,7 +77,7 @@ class BubbleTest { ...@@ -77,7 +77,7 @@ class BubbleTest {
var b1 = new Bubble(0, 0, 1, 2); var b1 = new Bubble(0, 0, 1, 2);
var b2 = new Bubble(1, 1, 2, 1); var b2 = new Bubble(1, 1, 2, 1);
var b3 = new Bubble(10, 10, 3, 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(1, b1);
bubbles.put(2, b2); bubbles.put(2, b2);
bubbles.put(3, b3); bubbles.put(3, b3);
......
...@@ -26,7 +26,7 @@ package com.iluwatar.spatialpartition; ...@@ -26,7 +26,7 @@ package com.iluwatar.spatialpartition;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import java.util.Hashtable; import java.util.HashMap;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
...@@ -41,7 +41,7 @@ class SpatialPartitionBubblesTest { ...@@ -41,7 +41,7 @@ class SpatialPartitionBubblesTest {
var b2 = new Bubble(5, 5, 2, 1); var b2 = new Bubble(5, 5, 2, 1);
var b3 = new Bubble(9, 9, 3, 1); var b3 = new Bubble(9, 9, 3, 1);
var b4 = new Bubble(8, 8, 4, 2); 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(1, b1);
bubbles.put(2, b2); bubbles.put(2, b2);
bubbles.put(3, b3); bubbles.put(3, b3);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册