mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-11 12:19:03 +00:00
* [Spatial Partition] (Fix) Issue #2544 - ConcurrentModificationException - Wrong log - Log using formatting anchor * [Spatial Partition] (Change) Hashtable to Map, (Remove) unused variable BUBBLE
This commit is contained in:
1 parent
c769c73e91
commit
2154e777b6
6 files changed
+27
-28
No files matched your search
@@ -25,7 +25,8 @@
|
|||||||
package com.iluwatar.spatialpartition;
|
package com.iluwatar.spatialpartition;
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.HashMap;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -59,9 +60,8 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class App {
|
public class App {
|
||||||
private static final String BUBBLE = "Bubble ";
|
|
||||||
|
|
||||||
static void noSpatialPartition(int numOfMovements, HashMap<Integer, Bubble> bubbles) {
|
static void noSpatialPartition(int numOfMovements, Map<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();
|
||||||
|
|
||||||
@@ -77,11 +77,11 @@ public class App {
|
|||||||
numOfMovements--;
|
numOfMovements--;
|
||||||
}
|
}
|
||||||
//bubbles not popped
|
//bubbles not popped
|
||||||
bubbles.keySet().stream().map(key -> BUBBLE + key + " not popped").forEach(LOGGER::info);
|
bubbles.keySet().forEach(key -> LOGGER.info("Bubble {} not popped", key));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void withSpatialPartition(
|
static void withSpatialPartition(
|
||||||
int height, int width, int numOfMovements, HashMap<Integer, Bubble> bubbles) {
|
int height, int width, int numOfMovements, Map<Integer, Bubble> bubbles) {
|
||||||
//creating quadtree
|
//creating quadtree
|
||||||
var rect = new Rect(width / 2D, height / 2D, width, height);
|
var rect = new Rect(width / 2D, height / 2D, width, height);
|
||||||
var quadTree = new QuadTree(rect, 4);
|
var quadTree = new QuadTree(rect, 4);
|
||||||
@@ -100,7 +100,7 @@ public class App {
|
|||||||
numOfMovements--;
|
numOfMovements--;
|
||||||
}
|
}
|
||||||
//bubbles not popped
|
//bubbles not popped
|
||||||
bubbles.keySet().stream().map(key -> BUBBLE + key + " not popped").forEach(LOGGER::info);
|
bubbles.keySet().forEach(key -> LOGGER.info("Bubble {} not popped", key));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -110,15 +110,15 @@ public class App {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
var bubbles1 = new HashMap<Integer, Bubble>();
|
var bubbles1 = new ConcurrentHashMap<Integer, Bubble>();
|
||||||
var bubbles2 = new HashMap<Integer, Bubble>();
|
var bubbles2 = new ConcurrentHashMap<Integer, Bubble>();
|
||||||
var rand = new SecureRandom();
|
var rand = new SecureRandom();
|
||||||
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 {} with radius {} added at ({},{})",
|
||||||
" added at (", b.coordinateX, ",", b.coordinateY + ")");
|
i, b.radius, b.coordinateX, b.coordinateY);
|
||||||
}
|
}
|
||||||
|
|
||||||
var start1 = System.currentTimeMillis();
|
var start1 = System.currentTimeMillis();
|
||||||
@@ -127,8 +127,7 @@ 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 {} ms", (end1 - start1));
|
||||||
LOGGER.info("With spatial partition takes ", (end2 - start2), "ms");
|
LOGGER.info("With spatial partition takes {} ms", (end2 - start2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ package com.iluwatar.spatialpartition;
|
|||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.Map;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,13 +58,12 @@ public class Bubble extends Point<Bubble> {
|
|||||||
<= (this.radius + b.radius) * (this.radius + b.radius);
|
<= (this.radius + b.radius) * (this.radius + b.radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pop(HashMap<Integer, Bubble> allBubbles) {
|
void pop(Map<Integer, Bubble> allBubbles) {
|
||||||
LOGGER.info("Bubble ", this.id,
|
LOGGER.info("Bubble {} popped at ({},{})!", this.id, this.coordinateX, this.coordinateY);
|
||||||
" popped at (", this.coordinateX, ",", this.coordinateY, ")!");
|
|
||||||
allBubbles.remove(this.id);
|
allBubbles.remove(this.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleCollision(Collection<? extends Point> toCheck, HashMap<Integer, Bubble> allBubbles) {
|
void handleCollision(Collection<? extends Point> toCheck, Map<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;
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
package com.iluwatar.spatialpartition;
|
package com.iluwatar.spatialpartition;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
@@ -65,5 +65,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, HashMap<Integer, T> all);
|
abstract void handleCollision(Collection<? extends Point> toCheck, Map<Integer, T> all);
|
||||||
}
|
}
|
||||||
@@ -25,7 +25,8 @@
|
|||||||
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.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The quadtree data structure is being used to keep track of the objects' locations. It has the
|
* The quadtree data structure is being used to keep track of the objects' locations. It has the
|
||||||
@@ -37,7 +38,7 @@ public class QuadTree {
|
|||||||
Rect boundary;
|
Rect boundary;
|
||||||
int capacity;
|
int capacity;
|
||||||
boolean divided;
|
boolean divided;
|
||||||
Hashtable<Integer, Point> points;
|
Map<Integer, Point> points;
|
||||||
QuadTree northwest;
|
QuadTree northwest;
|
||||||
QuadTree northeast;
|
QuadTree northeast;
|
||||||
QuadTree southwest;
|
QuadTree southwest;
|
||||||
@@ -47,7 +48,7 @@ public class QuadTree {
|
|||||||
this.boundary = boundary;
|
this.boundary = boundary;
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
this.divided = false;
|
this.divided = false;
|
||||||
this.points = new Hashtable<>();
|
this.points = new HashMap<>();
|
||||||
this.northwest = null;
|
this.northwest = null;
|
||||||
this.northeast = null;
|
this.northeast = null;
|
||||||
this.southwest = null;
|
this.southwest = null;
|
||||||
|
|||||||
+3
-3
@@ -25,7 +25,7 @@
|
|||||||
package com.iluwatar.spatialpartition;
|
package com.iluwatar.spatialpartition;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
@@ -34,10 +34,10 @@ import java.util.HashMap;
|
|||||||
|
|
||||||
public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> {
|
public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> {
|
||||||
|
|
||||||
private final HashMap<Integer, Bubble> bubbles;
|
private final Map<Integer, Bubble> bubbles;
|
||||||
private final QuadTree bubblesQuadTree;
|
private final QuadTree bubblesQuadTree;
|
||||||
|
|
||||||
SpatialPartitionBubbles(HashMap<Integer, Bubble> bubbles, QuadTree bubblesQuadTree) {
|
SpatialPartitionBubbles(Map<Integer, Bubble> bubbles, QuadTree bubblesQuadTree) {
|
||||||
this.bubbles = bubbles;
|
this.bubbles = bubbles;
|
||||||
this.bubblesQuadTree = bubblesQuadTree;
|
this.bubblesQuadTree = bubblesQuadTree;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -24,7 +24,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.spatialpartition;
|
package com.iluwatar.spatialpartition;
|
||||||
|
|
||||||
import java.util.Hashtable;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This abstract class has 2 fields, one of which is a hashtable containing all objects that
|
* This abstract class has 2 fields, one of which is a hashtable containing all objects that
|
||||||
@@ -35,7 +35,7 @@ import java.util.Hashtable;
|
|||||||
|
|
||||||
public abstract class SpatialPartitionGeneric<T> {
|
public abstract class SpatialPartitionGeneric<T> {
|
||||||
|
|
||||||
Hashtable<Integer, T> playerPositions;
|
Map<Integer, T> playerPositions;
|
||||||
QuadTree quadTree;
|
QuadTree quadTree;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user