Skip to content

Commit 2154e77

Browse files
authored
[Spatial Partition] (Fix) Issue #2544: Can not run App.java & some logs are wrong (#2545)
* [Spatial Partition] (Fix) Issue #2544 - ConcurrentModificationException - Wrong log - Log using formatting anchor * [Spatial Partition] (Change) Hashtable to Map, (Remove) unused variable BUBBLE
1 parent c769c73 commit 2154e77

File tree

6 files changed

+27
-28
lines changed

6 files changed

+27
-28
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
package com.iluwatar.spatialpartition;
2626

2727
import java.security.SecureRandom;
28-
import java.util.HashMap;
28+
import java.util.Map;
29+
import java.util.concurrent.ConcurrentHashMap;
2930
import lombok.extern.slf4j.Slf4j;
3031

3132
/**
@@ -59,9 +60,8 @@
5960

6061
@Slf4j
6162
public class App {
62-
private static final String BUBBLE = "Bubble ";
6363

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

@@ -77,11 +77,11 @@ static void noSpatialPartition(int numOfMovements, HashMap<Integer, Bubble> bubb
7777
numOfMovements--;
7878
}
7979
//bubbles not popped
80-
bubbles.keySet().stream().map(key -> BUBBLE + key + " not popped").forEach(LOGGER::info);
80+
bubbles.keySet().forEach(key -> LOGGER.info("Bubble {} not popped", key));
8181
}
8282

8383
static void withSpatialPartition(
84-
int height, int width, int numOfMovements, HashMap<Integer, Bubble> bubbles) {
84+
int height, int width, int numOfMovements, Map<Integer, Bubble> bubbles) {
8585
//creating quadtree
8686
var rect = new Rect(width / 2D, height / 2D, width, height);
8787
var quadTree = new QuadTree(rect, 4);
@@ -100,7 +100,7 @@ static void withSpatialPartition(
100100
numOfMovements--;
101101
}
102102
//bubbles not popped
103-
bubbles.keySet().stream().map(key -> BUBBLE + key + " not popped").forEach(LOGGER::info);
103+
bubbles.keySet().forEach(key -> LOGGER.info("Bubble {} not popped", key));
104104
}
105105

106106
/**
@@ -110,15 +110,15 @@ static void withSpatialPartition(
110110
*/
111111

112112
public static void main(String[] args) {
113-
var bubbles1 = new HashMap<Integer, Bubble>();
114-
var bubbles2 = new HashMap<Integer, Bubble>();
113+
var bubbles1 = new ConcurrentHashMap<Integer, Bubble>();
114+
var bubbles2 = new ConcurrentHashMap<Integer, Bubble>();
115115
var rand = new SecureRandom();
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 {} with radius {} added at ({},{})",
121+
i, b.radius, b.coordinateX, b.coordinateY);
122122
}
123123

124124
var start1 = System.currentTimeMillis();
@@ -127,8 +127,7 @@ 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 {} ms", (end1 - start1));
131+
LOGGER.info("With spatial partition takes {} ms", (end2 - start2));
132132
}
133133
}
134-

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import java.security.SecureRandom;
2828
import java.util.Collection;
29-
import java.util.HashMap;
29+
import java.util.Map;
3030
import lombok.extern.slf4j.Slf4j;
3131

3232
/**
@@ -58,13 +58,12 @@ boolean touches(Bubble b) {
5858
<= (this.radius + b.radius) * (this.radius + b.radius);
5959
}
6060

61-
void pop(HashMap<Integer, Bubble> allBubbles) {
62-
LOGGER.info("Bubble ", this.id,
63-
" popped at (", this.coordinateX, ",", this.coordinateY, ")!");
61+
void pop(Map<Integer, Bubble> allBubbles) {
62+
LOGGER.info("Bubble {} popped at ({},{})!", this.id, this.coordinateX, this.coordinateY);
6463
allBubbles.remove(this.id);
6564
}
6665

67-
void handleCollision(Collection<? extends Point> toCheck, HashMap<Integer, Bubble> allBubbles) {
66+
void handleCollision(Collection<? extends Point> toCheck, Map<Integer, Bubble> allBubbles) {
6867
var toBePopped = false; //if any other bubble collides with it, made true
6968
for (var point : toCheck) {
7069
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
@@ -25,7 +25,7 @@
2525
package com.iluwatar.spatialpartition;
2626

2727
import java.util.Collection;
28-
import java.util.HashMap;
28+
import java.util.Map;
2929

3030
/**
3131
* The abstract Point class which will be extended by any object in the field whose location has to
@@ -65,5 +65,5 @@ public abstract class Point<T> {
6565
* @param toCheck contains the objects which need to be checked
6666
* @param all contains hashtable of all points on field at this time
6767
*/
68-
abstract void handleCollision(Collection<? extends Point> toCheck, HashMap<Integer, T> all);
68+
abstract void handleCollision(Collection<? extends Point> toCheck, Map<Integer, T> all);
6969
}

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

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

2727
import java.util.Collection;
28-
import java.util.Hashtable;
28+
import java.util.HashMap;
29+
import java.util.Map;
2930

3031
/**
3132
* The quadtree data structure is being used to keep track of the objects' locations. It has the
@@ -37,7 +38,7 @@ public class QuadTree {
3738
Rect boundary;
3839
int capacity;
3940
boolean divided;
40-
Hashtable<Integer, Point> points;
41+
Map<Integer, Point> points;
4142
QuadTree northwest;
4243
QuadTree northeast;
4344
QuadTree southwest;
@@ -47,7 +48,7 @@ public class QuadTree {
4748
this.boundary = boundary;
4849
this.capacity = capacity;
4950
this.divided = false;
50-
this.points = new Hashtable<>();
51+
this.points = new HashMap<>();
5152
this.northwest = null;
5253
this.northeast = null;
5354
this.southwest = null;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
package com.iluwatar.spatialpartition;
2626

2727
import java.util.ArrayList;
28-
import java.util.HashMap;
28+
import java.util.Map;
2929

3030
/**
3131
* This class extends the generic SpatialPartition abstract class and is used in our example to keep
@@ -34,10 +34,10 @@
3434

3535
public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> {
3636

37-
private final HashMap<Integer, Bubble> bubbles;
37+
private final Map<Integer, Bubble> bubbles;
3838
private final QuadTree bubblesQuadTree;
3939

40-
SpatialPartitionBubbles(HashMap<Integer, Bubble> bubbles, QuadTree bubblesQuadTree) {
40+
SpatialPartitionBubbles(Map<Integer, Bubble> bubbles, QuadTree bubblesQuadTree) {
4141
this.bubbles = bubbles;
4242
this.bubblesQuadTree = bubblesQuadTree;
4343
}

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

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

27-
import java.util.Hashtable;
27+
import java.util.Map;
2828

2929
/**
3030
* This abstract class has 2 fields, one of which is a hashtable containing all objects that
@@ -35,7 +35,7 @@
3535

3636
public abstract class SpatialPartitionGeneric<T> {
3737

38-
Hashtable<Integer, T> playerPositions;
38+
Map<Integer, T> playerPositions;
3939
QuadTree quadTree;
4040

4141
/**

0 commit comments

Comments
 (0)