Skip to content

Commit 3f09fb7

Browse files
authored
Clear Sonar Blockers (iluwatar#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>
1 parent 663dbd2 commit 3f09fb7

File tree

7 files changed

+40
-34
lines changed

7 files changed

+40
-34
lines changed

caching/src/test/java/com/iluwatar/caching/CachingTest.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323

2424
package com.iluwatar.caching;
2525

26+
import static org.junit.jupiter.api.Assertions.assertNotNull;
27+
2628
import org.junit.jupiter.api.BeforeEach;
2729
import org.junit.jupiter.api.Test;
2830

2931
/**
3032
* Application test
3133
*/
32-
public class CachingTest {
34+
class CachingTest {
3335
private App app;
3436

3537
/**
@@ -47,22 +49,26 @@ public void setUp() {
4749
}
4850

4951
@Test
50-
public void testReadAndWriteThroughStrategy() {
52+
void testReadAndWriteThroughStrategy() {
53+
assertNotNull(app);
5154
app.useReadAndWriteThroughStrategy();
5255
}
5356

5457
@Test
55-
public void testReadThroughAndWriteAroundStrategy() {
58+
void testReadThroughAndWriteAroundStrategy() {
59+
assertNotNull(app);
5660
app.useReadThroughAndWriteAroundStrategy();
5761
}
5862

5963
@Test
60-
public void testReadThroughAndWriteBehindStrategy() {
64+
void testReadThroughAndWriteBehindStrategy() {
65+
assertNotNull(app);
6166
app.useReadThroughAndWriteBehindStrategy();
6267
}
6368

6469
@Test
65-
public void testCacheAsideStrategy() {
70+
void testCacheAsideStrategy() {
71+
assertNotNull(app);
6672
app.useCacheAsideStategy();
6773
}
6874
}

spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
package com.iluwatar.spatialpartition;
2525

26-
import java.util.Hashtable;
26+
import java.util.HashMap;
2727
import java.util.Random;
2828
import org.slf4j.Logger;
2929
import org.slf4j.LoggerFactory;
@@ -44,11 +44,11 @@
4444
* <b>{@link Rect}</b> class to define the boundary of the quadtree. We use an abstract class
4545
* <b>{@link Point}</b>
4646
* with x and y coordinate fields and also an id field so that it can easily be put and looked up in
47-
* the hashtable. This class has abstract methods to define how the object moves (move()), when to
47+
* the hashmap. This class has abstract methods to define how the object moves (move()), when to
4848
* check for collision with any object (touches(obj)) and how to handle collision
4949
* (handleCollision(obj)), and will be extended by any object whose position has to be kept track of
5050
* in the quadtree. The <b>{@link SpatialPartitionGeneric}</b> abstract class has 2 fields - a
51-
* hashtable containing all objects (we use hashtable for faster lookups, insertion and deletion)
51+
* hashmap containing all objects (we use hashmap for faster lookups, insertion and deletion)
5252
* and a quadtree, and contains an abstract method which defines how to handle interactions between
5353
* objects using the quadtree.</p>
5454
* <p>Using the quadtree data structure will reduce the time complexity of finding the objects
@@ -61,7 +61,7 @@ public class App {
6161
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
6262
private static final String BUBBLE = "Bubble ";
6363

64-
static void noSpatialPartition(int numOfMovements, Hashtable<Integer, Bubble> bubbles) {
64+
static void noSpatialPartition(int numOfMovements, HashMap<Integer, Bubble> bubbles) {
6565
//all bubbles have to be checked for collision for all bubbles
6666
var bubblesToCheck = bubbles.values();
6767

@@ -81,9 +81,9 @@ static void noSpatialPartition(int numOfMovements, Hashtable<Integer, Bubble> bu
8181
}
8282

8383
static void withSpatialPartition(
84-
int height, int width, int numOfMovements, Hashtable<Integer, Bubble> bubbles) {
84+
int height, int width, int numOfMovements, HashMap<Integer, Bubble> bubbles) {
8585
//creating quadtree
86-
var rect = new Rect(width / 2, height / 2, width, height);
86+
var rect = new Rect(width / 2D, height / 2D, width, height);
8787
var quadTree = new QuadTree(rect, 4);
8888

8989
//will run numOfMovement times or till all bubbles have popped
@@ -110,15 +110,15 @@ static void withSpatialPartition(
110110
*/
111111

112112
public static void main(String[] args) {
113-
var bubbles1 = new Hashtable<Integer, Bubble>();
114-
var bubbles2 = new Hashtable<Integer, Bubble>();
113+
var bubbles1 = new HashMap<Integer, Bubble>();
114+
var bubbles2 = new HashMap<Integer, Bubble>();
115115
var rand = new Random();
116116
for (int i = 0; i < 10000; i++) {
117117
var b = new Bubble(rand.nextInt(300), rand.nextInt(300), i, rand.nextInt(2) + 1);
118118
bubbles1.put(i, b);
119119
bubbles2.put(i, b);
120-
LOGGER.info(BUBBLE + i + " with radius " + b.radius
121-
+ " added at (" + b.coordinateX + "," + b.coordinateY + ")");
120+
LOGGER.info(BUBBLE, i, " with radius ", b.radius,
121+
" added at (", b.coordinateX, ",", b.coordinateY + ")");
122122
}
123123

124124
var start1 = System.currentTimeMillis();
@@ -127,8 +127,8 @@ public static void main(String[] args) {
127127
var start2 = System.currentTimeMillis();
128128
App.withSpatialPartition(300, 300, 20, bubbles2);
129129
var end2 = System.currentTimeMillis();
130-
LOGGER.info("Without spatial partition takes " + (end1 - start1) + "ms");
131-
LOGGER.info("With spatial partition takes " + (end2 - start2) + "ms");
130+
LOGGER.info("Without spatial partition takes ", (end1 - start1), "ms");
131+
LOGGER.info("With spatial partition takes ", (end2 - start2), "ms");
132132
}
133133
}
134134

spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
package com.iluwatar.spatialpartition;
2525

2626
import java.util.Collection;
27-
import java.util.Hashtable;
27+
import java.util.HashMap;
2828
import java.util.Random;
2929
import org.slf4j.Logger;
3030
import org.slf4j.LoggerFactory;
@@ -58,13 +58,13 @@ boolean touches(Bubble b) {
5858
<= (this.radius + b.radius) * (this.radius + b.radius);
5959
}
6060

61-
void pop(Hashtable<Integer, Bubble> allBubbles) {
62-
LOGGER.info("Bubble " + this.id
63-
+ " popped at (" + this.coordinateX + "," + this.coordinateY + ")!");
61+
void pop(HashMap<Integer, Bubble> allBubbles) {
62+
LOGGER.info("Bubble ", this.id,
63+
" popped at (", this.coordinateX, ",", this.coordinateY, ")!");
6464
allBubbles.remove(this.id);
6565
}
6666

67-
void handleCollision(Collection<? extends Point> toCheck, Hashtable<Integer, Bubble> allBubbles) {
67+
void handleCollision(Collection<? extends Point> toCheck, HashMap<Integer, Bubble> allBubbles) {
6868
var toBePopped = false; //if any other bubble collides with it, made true
6969
for (var point : toCheck) {
7070
var otherId = point.id;

spatial-partition/src/main/java/com/iluwatar/spatialpartition/Point.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
package com.iluwatar.spatialpartition;
2525

2626
import java.util.Collection;
27-
import java.util.Hashtable;
27+
import java.util.HashMap;
2828

2929
/**
3030
* 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> {
6464
* @param toCheck contains the objects which need to be checked
6565
* @param all contains hashtable of all points on field at this time
6666
*/
67-
abstract void handleCollision(Collection<? extends Point> toCheck, Hashtable<Integer, T> all);
67+
abstract void handleCollision(Collection<? extends Point> toCheck, HashMap<Integer, T> all);
6868
}

spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionBubbles.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
package com.iluwatar.spatialpartition;
2525

2626
import java.util.ArrayList;
27-
import java.util.Hashtable;
27+
import java.util.HashMap;
2828

2929
/**
3030
* This class extends the generic SpatialPartition abstract class and is used in our example to keep
@@ -33,18 +33,18 @@
3333

3434
public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> {
3535

36-
private final Hashtable<Integer, Bubble> bubbles;
36+
private final HashMap<Integer, Bubble> bubbles;
3737
private final QuadTree quadTree;
3838

39-
SpatialPartitionBubbles(Hashtable<Integer, Bubble> bubbles, QuadTree quadTree) {
39+
SpatialPartitionBubbles(HashMap<Integer, Bubble> bubbles, QuadTree quadTree) {
4040
this.bubbles = bubbles;
4141
this.quadTree = quadTree;
4242
}
4343

4444
void handleCollisionsUsingQt(Bubble b) {
4545
// finding points within area of a square drawn with centre same as
4646
// centre of bubble and length = radius of bubble
47-
var rect = new Rect(b.coordinateX, b.coordinateY, 2 * b.radius, 2 * b.radius);
47+
var rect = new Rect(b.coordinateX, b.coordinateY, 2D * b.radius, 2D * b.radius);
4848
var quadTreeQueryResult = new ArrayList<Point>();
4949
this.quadTree.query(rect, quadTreeQueryResult);
5050
//handling these collisions

spatial-partition/src/test/java/com/iluwatar/spatialpartition/BubbleTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import static org.junit.jupiter.api.Assertions.assertTrue;
3030

3131
import java.util.ArrayList;
32-
import java.util.Hashtable;
32+
import java.util.HashMap;
3333
import org.junit.jupiter.api.Test;
3434

3535
/**
@@ -63,11 +63,11 @@ void touchesTest() {
6363
void popTest() {
6464
var b1 = new Bubble(10, 10, 1, 2);
6565
var b2 = new Bubble(0, 0, 2, 2);
66-
var bubbles = new Hashtable<Integer, Bubble>();
66+
var bubbles = new HashMap<Integer, Bubble>();
6767
bubbles.put(1, b1);
6868
bubbles.put(2, b2);
6969
b1.pop(bubbles);
70-
//after popping, bubble no longer in hashtable containing all bubbles
70+
//after popping, bubble no longer in hashMap containing all bubbles
7171
assertNull(bubbles.get(1));
7272
assertNotNull(bubbles.get(2));
7373
}
@@ -77,7 +77,7 @@ void handleCollisionTest() {
7777
var b1 = new Bubble(0, 0, 1, 2);
7878
var b2 = new Bubble(1, 1, 2, 1);
7979
var b3 = new Bubble(10, 10, 3, 1);
80-
var bubbles = new Hashtable<Integer, Bubble>();
80+
var bubbles = new HashMap<Integer, Bubble>();
8181
bubbles.put(1, b1);
8282
bubbles.put(2, b2);
8383
bubbles.put(3, b3);

spatial-partition/src/test/java/com/iluwatar/spatialpartition/SpatialPartitionBubblesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import static org.junit.jupiter.api.Assertions.assertNotNull;
2727
import static org.junit.jupiter.api.Assertions.assertNull;
2828

29-
import java.util.Hashtable;
29+
import java.util.HashMap;
3030
import org.junit.jupiter.api.Test;
3131

3232
/**
@@ -41,7 +41,7 @@ void handleCollisionsUsingQtTest() {
4141
var b2 = new Bubble(5, 5, 2, 1);
4242
var b3 = new Bubble(9, 9, 3, 1);
4343
var b4 = new Bubble(8, 8, 4, 2);
44-
var bubbles = new Hashtable<Integer, Bubble>();
44+
var bubbles = new HashMap<Integer, Bubble>();
4545
bubbles.put(1, b1);
4646
bubbles.put(2, b2);
4747
bubbles.put(3, b3);

0 commit comments

Comments
 (0)