diff --git a/caching/src/test/java/com/iluwatar/caching/CachingTest.java b/caching/src/test/java/com/iluwatar/caching/CachingTest.java index 4db4085d468f15b664b18c9d207454882f0e1142..22130951a0e65f44ce0bdf90d42cc8fdf22f6032 100644 --- a/caching/src/test/java/com/iluwatar/caching/CachingTest.java +++ b/caching/src/test/java/com/iluwatar/caching/CachingTest.java @@ -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(); } } diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java index 083d6a0199741ad415f1efb29524c1c03930bd00..69a2852d74a7578c5f1632711bc5b028c8f53e6a 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java @@ -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; * {@link Rect} class to define the boundary of the quadtree. We use an abstract class * {@link Point} * 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 {@link SpatialPartitionGeneric} 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.

*

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 bubbles) { + static void noSpatialPartition(int numOfMovements, HashMap 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 bubbles) { + int height, int width, int numOfMovements, HashMap 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(); - var bubbles2 = new Hashtable(); + var bubbles1 = new HashMap(); + var bubbles2 = new HashMap(); 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"); } } diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java index 9c39c29c85424a3125714d5c70a4acf37cc5ce4a..4ff1664206f36164ceec4fc3a1b9941de1adde7b 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java @@ -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 { <= (this.radius + b.radius) * (this.radius + b.radius); } - void pop(Hashtable allBubbles) { - LOGGER.info("Bubble " + this.id - + " popped at (" + this.coordinateX + "," + this.coordinateY + ")!"); + void pop(HashMap allBubbles) { + LOGGER.info("Bubble ", this.id, + " popped at (", this.coordinateX, ",", this.coordinateY, ")!"); allBubbles.remove(this.id); } - void handleCollision(Collection toCheck, Hashtable allBubbles) { + void handleCollision(Collection toCheck, HashMap allBubbles) { var toBePopped = false; //if any other bubble collides with it, made true for (var point : toCheck) { var otherId = point.id; diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Point.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Point.java index 410beae349a7fb97acb86e000848b892efb241b4..a2547b7a10e8bbc59b09b110d4bed4f02141dded 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Point.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Point.java @@ -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 { * @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 toCheck, Hashtable all); + abstract void handleCollision(Collection toCheck, HashMap all); } diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionBubbles.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionBubbles.java index c543da12ca6e4d5cb933dff0c6993e74aa722a02..1a9a0d91e52b1cd606c50a34d0b7990677e8bfb4 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionBubbles.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/SpatialPartitionBubbles.java @@ -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 { - private final Hashtable bubbles; + private final HashMap bubbles; private final QuadTree quadTree; - SpatialPartitionBubbles(Hashtable bubbles, QuadTree quadTree) { + SpatialPartitionBubbles(HashMap bubbles, QuadTree quadTree) { this.bubbles = bubbles; this.quadTree = quadTree; } @@ -44,7 +44,7 @@ public class SpatialPartitionBubbles extends SpatialPartitionGeneric { 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(); this.quadTree.query(rect, quadTreeQueryResult); //handling these collisions diff --git a/spatial-partition/src/test/java/com/iluwatar/spatialpartition/BubbleTest.java b/spatial-partition/src/test/java/com/iluwatar/spatialpartition/BubbleTest.java index 957a36ce37f0c6ed99e302a5785c7a31c74bede6..f02b5e961eff259cbea443d3a98b177fe3c0fc2d 100644 --- a/spatial-partition/src/test/java/com/iluwatar/spatialpartition/BubbleTest.java +++ b/spatial-partition/src/test/java/com/iluwatar/spatialpartition/BubbleTest.java @@ -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(); + var bubbles = new HashMap(); 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(); + var bubbles = new HashMap(); bubbles.put(1, b1); bubbles.put(2, b2); bubbles.put(3, b3); diff --git a/spatial-partition/src/test/java/com/iluwatar/spatialpartition/SpatialPartitionBubblesTest.java b/spatial-partition/src/test/java/com/iluwatar/spatialpartition/SpatialPartitionBubblesTest.java index 3470b27ccba0e63ce3306da0e3fbaf24576ec452..f36995938fff748218a6bd980d633278c3cf869c 100644 --- a/spatial-partition/src/test/java/com/iluwatar/spatialpartition/SpatialPartitionBubblesTest.java +++ b/spatial-partition/src/test/java/com/iluwatar/spatialpartition/SpatialPartitionBubblesTest.java @@ -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(); + var bubbles = new HashMap(); bubbles.put(1, b1); bubbles.put(2, b2); bubbles.put(3, b3);