Compare commits

..

12 Commits

Author SHA1 Message Date
806862fec0 docs: update .all-contributorsrc [skip ci] 2021-02-07 15:03:17 +00:00
fef7207418 docs: update README.md [skip ci] 2021-02-07 15:03:16 +00:00
cdc2a154df Arabic Translation - Initialization part (#1648) 2021-02-07 17:00:48 +02:00
d620dbbc05 bump version to 3.8.0.2131 (#1647)
earlier version 3.7.0.1746
org.sonarsource.scanner.maven.sonar-maven-plugin
2021-02-07 16:55:52 +02:00
a86bdfd14d Update link title 2021-01-30 14:54:14 +02:00
7408620a71 Update image filenames 2021-01-30 14:44:41 +02:00
a94737075e Minor fixes to readmes 2021-01-30 14:16:35 +02:00
3f09fb70bb Clear Sonar Blockers (#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>
2021-01-30 13:37:52 +02:00
663dbd298e Update License year to 2021 (#1639)
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2021-01-30 12:53:28 +02:00
8a9e26e77a docs: add byoungju94 as a contributor (#1642)
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2021-01-30 12:46:40 +02:00
462b581b34 clearing Sonar Blockers (#1633)
* update gitignore

.checkstyle files are being tracked which should not be

* NOSONAR for statement

excluded from SONAR analysis as it is already dealt using functional approach

https://sonarcloud.io/project/issues?id=iluwatar_java-design-patterns&issues=AW8FwRBhm8eoEVQR-x0f&open=AW8FwRBhm8eoEVQR-x0f

* achieved thread safety with lazy initialization

https://sonarcloud.io/project/issues?fileUuids=AXb6t0PKusn4P8Tm-LmM&id=iluwatar_java-design-patterns&open=AXb6t19yusn4P8Tm-Lmo&resolved=false

* remove double checked locking and initialize before using

https://sonarcloud.io/project/issues?fileUuids=AXb6t0PKusn4P8Tm-LmK&id=iluwatar_java-design-patterns&open=AXb6t19qusn4P8Tm-Lmk&resolved=false

* NOSONAR for the line

https://sonarcloud.io/project/issues?id=iluwatar_java-design-patterns&issues=AXPd3iSe46HRSze7cz3D&open=AXPd3iSe46HRSze7cz3D

Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2021-01-27 16:24:06 +02:00
9abfd5777c docs: add rohit10000 as a contributor (#1637)
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2021-01-26 22:22:48 +02:00
23 changed files with 132 additions and 66 deletions

View File

@ -1386,6 +1386,24 @@
"contributions": [
"code"
]
},
{
"login": "byoungju94",
"name": "byoungju94",
"avatar_url": "https://avatars.githubusercontent.com/u/42516378?v=4",
"profile": "https://github.com/byoungju94",
"contributions": [
"code"
]
},
{
"login": "moustafafarhat",
"name": "Moustafa Farhat",
"avatar_url": "https://avatars.githubusercontent.com/u/38836727?v=4",
"profile": "https://github.com/moustafafarhat",
"contributions": [
"translation"
]
}
],
"contributorsPerLine": 4,

View File

@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) 2014-2020 Ilkka Seppälä
Copyright (c) 2014-2021 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -10,12 +10,12 @@
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-152-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-154-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
<br/>
Read in different language : [![CN](/assets/flags/CN.png)**CN**](/zh/README.md),[![KR](/assets/flags/KR.png)**KR**](/ko/README.md),[![FR](/assets/flags/FR.png)**FR**](/fr/README.md),[![TR](/assets/flags/TR.png)**TR**](/tr/README.md),
Read in different language : [![CN](/assets/flags/CN.png)**CN**](/zh/README.md),[![KR](/assets/flags/KR.png)**KR**](/ko/README.md),[![FR](/assets/flags/FR.png)**FR**](/fr/README.md),[![TR](/assets/flags/TR.png)**TR**](/tr/README.md),[![AR](/assets/flags/AR.png)**AR**](/ar/README.md),
<br/>
@ -303,6 +303,10 @@ This project is licensed under the terms of the MIT license.
<td align="center"><a href="https://github.com/demirhalil"><img src="https://avatars1.githubusercontent.com/u/22895118?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Halil Demir</b></sub></a><br /><a href="#translation-demirhalil" title="Translation">🌍</a></td>
<td align="center"><a href="https://github.com/rohit10000"><img src="https://avatars.githubusercontent.com/u/20845565?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Rohit Singh</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=rohit10000" title="Code">💻</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/byoungju94"><img src="https://avatars.githubusercontent.com/u/42516378?v=4?s=100" width="100px;" alt=""/><br /><sub><b>byoungju94</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=byoungju94" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/moustafafarhat"><img src="https://avatars.githubusercontent.com/u/38836727?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Moustafa Farhat</b></sub></a><br /><a href="#translation-moustafafarhat" title="Translation">🌍</a></td>
</tr>
</table>
<!-- markdownlint-restore -->

View File

@ -213,8 +213,8 @@ Example use cases
## Related patterns
[Factory Method](https://java-design-patterns.com/patterns/factory-method/)
[Factory Kit](https://java-design-patterns.com/patterns/factory-kit/)
* [Factory Method](https://java-design-patterns.com/patterns/factory-method/)
* [Factory Kit](https://java-design-patterns.com/patterns/factory-kit/)
## Credits

42
ar/README.md Normal file
View File

@ -0,0 +1,42 @@
# أنماط التصميم بلغة برمجة الجافا
![Java CI with Maven](https://github.com/iluwatar/java-design-patterns/workflows/Java%20CI%20with%20Maven/badge.svg)
[![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/LICENSE.md)
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=ncloc)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-148-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
# مقدمة
أنماط التصميم هي أفضل التدريبات التي يمكن للمبرمج استخدامها لحل المشكلات الشائعة عند تصميم تطبيق أو نظام.
يمكن لأنماط التصميم تسريع عملية التطوير من خلال توفير نماذج تطوير مجربة ومثبتة ومفيدة.
تساعد إعادة استخدام أنماط التصميم في منع المشكلات الدقيقة التي تسبب مشكلات كبيرة ، كما تعمل على تحسين قابلية قراءة الكود للمهندسين البرمجيات الذين هم على دراية بالأنماط.
# البدء في التعلم
يعرض هذا الموقع أنماط تصميم جافا. تم تطوير الحلول من قبل المبرمجين والمهندسين ذوي الخبرة من مجتمع المصادر المفتوحة. يمكن تصفح الأنماط من خلال أوصافها عالية المستوى أو بالنظر إلى كود المصدر الخاص بها. تم كتابة الشرح على أمثلة الكود المصدري جيدًا ويمكن اعتبارها برامج تعليمية حول كيفية تنفيذ نمط معين. نحن نستخدم تقنيات Java مفتوحة المصدر الأكثر شيوعًا والتي أثبتت جدارتها في مشاريع الجافا.
قبل الغوص في المادة ، يجب أن تكون على دراية [بمبادئ تصميم البرامج المختلفة](https://java-design-patterns.com/principles/).
يجب أن تكون جميع التصميمات بسيطة قدر الإمكان. يجب أن تبدأ بـ KISS و YAGNI وافعل أبسط شيء يمكن أن يعمل بهذه المبادىء. يجب إستخدام الأنماط فقط عند الحاجة إليها من أجل تمديد البرنامج بشكل عملي.
بمجرد أن تصبح معتادًا على هذه المفاهيم ، يمكنك البدء في التعمق في [أنماط التصميم المتاحة](https://java-design-patterns.com/patterns/) من خلال أي من الأساليب التالية
ابحث عن نمط محدد بالاسم. لا يمكنك العثور على واحد؟ الرجاء الإبلاغ عن نمط جديد [هنا](https://github.com/iluwatar/java-design-patterns/issues).
استخدام علامات مثل Performance أو Gang of Four أو الوصول إلى البيانات.
استخدام فئات الأنماط و Creational, Behavioral وغيرها.
نأمل أن تجد الحلول الموجهة للكائنات المعروضة على هذا الموقع مفيدة في مشاريعك البرمجية وأن تستمتع بتعلمها بقدر ما قمنا بتطويرها.
# كيف تساهم؟
إذا كنت على استعداد للمساهمة في المشروع ، فستجد المعلومات ذات الصلة في [ويكي المطور الخاص بنا](https://github.com/iluwatar/java-design-patterns/wiki). سنساعدك ونجيب على أسئلتك في [غرفة الدردشة Gitter](https://gitter.im/iluwatar/java-design-patterns).
# الترخيص
هذا المشروع مرخص بموجب شروط ترخيص MIT.

BIN
assets/flags/AR.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 252 B

View File

@ -20,12 +20,14 @@ callbacks or waiting until everything is done.
## Applicability
Use async method invocation pattern when
* you have multiple independent tasks that can run in parallel
* you need to improve the performance of a group of sequential tasks
* you have limited amount of processing capacity or long running tasks and the
* You have multiple independent tasks that can run in parallel
* You need to improve the performance of a group of sequential tasks
* You have limited amount of processing capacity or long running tasks and the
caller should not wait the tasks to be ready
## Real world examples
* [FutureTask](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/FutureTask.html), [CompletableFuture](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html) and [ExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html) (Java)
* [Task-based Asynchronous Pattern](https://msdn.microsoft.com/en-us/library/hh873175.aspx) (.NET)
* [FutureTask](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/FutureTask.html)
* [CompletableFuture](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html)
* [ExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html)
* [Task-based Asynchronous Pattern](https://msdn.microsoft.com/en-us/library/hh873175.aspx)

View File

@ -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();
}
}

View File

@ -49,7 +49,7 @@ remote calls.
The service architecture is as follows:
![alt text](./etc/ServiceDiagram.PNG "Service Diagram")
![alt text](./etc/ServiceDiagram.png "Service Diagram")
In terms of code, the end user application is:
@ -292,7 +292,7 @@ public class DefaultCircuitBreaker implements CircuitBreaker {
How does the above pattern prevent failures? Let's understand via this finite state machine
implemented by it.
![alt text](./etc/StateDiagram.PNG "State Diagram")
![alt text](./etc/StateDiagram.png "State Diagram")
- We initialize the Circuit Breaker object with certain parameters: `timeout`, `failureThreshold` and `retryTimePeriod` which help determine how resilient the API is.
- Initially, we are in the `closed` state and nos remote calls to the API have occurred.

View File

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 23 KiB

View File

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View File

@ -105,4 +105,4 @@ domain equivalence.
## Credits
* [Converter](http://www.xsolve.pl/blog/converter-pattern-in-java-8/)
* [Converter Pattern in Java 8](http://www.xsolve.pl/blog/converter-pattern-in-java-8/)

View File

@ -67,7 +67,7 @@ public class DbCustomerDao implements CustomerDao {
public Stream<Customer> getAll() throws Exception {
try {
var connection = getConnection();
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS");
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); // NOSONAR
var resultSet = statement.executeQuery(); // NOSONAR
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
Spliterator.ORDERED) {

View File

@ -25,6 +25,7 @@
<inceptionYear>2014-2019</inceptionYear>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonar-maven-plugin.version>3.8.0.2131</sonar-maven-plugin.version>
<hibernate.version>5.2.18.Final</hibernate.version>
<spring.version>5.0.13.RELEASE</spring.version>
<spring-boot.version>2.0.9.RELEASE</spring-boot.version>
@ -425,7 +426,7 @@
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.7.0.1746</version>
<version>${sonar-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>

View File

@ -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;
* <b>{@link Rect}</b> class to define the boundary of the quadtree. We use an abstract class
* <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
* 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 <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
* objects using the quadtree.</p>
* <p>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<Integer, Bubble> bubbles) {
static void noSpatialPartition(int numOfMovements, HashMap<Integer, Bubble> 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<Integer, Bubble> bubbles) {
int height, int width, int numOfMovements, HashMap<Integer, Bubble> 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<Integer, Bubble>();
var bubbles2 = new Hashtable<Integer, Bubble>();
var bubbles1 = new HashMap<Integer, Bubble>();
var bubbles2 = new HashMap<Integer, Bubble>();
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");
}
}

View File

@ -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<Bubble> {
<= (this.radius + b.radius) * (this.radius + b.radius);
}
void pop(Hashtable<Integer, Bubble> allBubbles) {
LOGGER.info("Bubble " + this.id
+ " popped at (" + this.coordinateX + "," + this.coordinateY + ")!");
void pop(HashMap<Integer, Bubble> allBubbles) {
LOGGER.info("Bubble ", this.id,
" popped at (", this.coordinateX, ",", this.coordinateY, ")!");
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
for (var point : toCheck) {
var otherId = point.id;

View File

@ -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<T> {
* @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<? extends Point> toCheck, Hashtable<Integer, T> all);
abstract void handleCollision(Collection<? extends Point> toCheck, HashMap<Integer, T> all);
}

View File

@ -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<Bubble> {
private final Hashtable<Integer, Bubble> bubbles;
private final HashMap<Integer, Bubble> bubbles;
private final QuadTree quadTree;
SpatialPartitionBubbles(Hashtable<Integer, Bubble> bubbles, QuadTree quadTree) {
SpatialPartitionBubbles(HashMap<Integer, Bubble> bubbles, QuadTree quadTree) {
this.bubbles = bubbles;
this.quadTree = quadTree;
}
@ -44,7 +44,7 @@ public class SpatialPartitionBubbles extends SpatialPartitionGeneric<Bubble> {
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<Point>();
this.quadTree.query(rect, quadTreeQueryResult);
//handling these collisions

View File

@ -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<Integer, Bubble>();
var bubbles = new HashMap<Integer, Bubble>();
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<Integer, Bubble>();
var bubbles = new HashMap<Integer, Bubble>();
bubbles.put(1, b1);
bubbles.put(2, b2);
bubbles.put(3, b3);

View File

@ -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<Integer, Bubble>();
var bubbles = new HashMap<Integer, Bubble>();
bubbles.put(1, b1);
bubbles.put(2, b2);
bubbles.put(3, b3);

View File

@ -38,16 +38,13 @@ public class Db {
*
* @return singleton instance of Db class
*/
public static Db getInstance() {
public static synchronized Db getInstance() {
if (instance == null) {
synchronized (Db.class) {
if (instance == null) {
instance = new Db();
instance.userName2User = new HashMap<>();
instance.user2Account = new HashMap<>();
instance.itemName2Product = new HashMap<>();
}
}
Db newInstance = new Db();
newInstance.userName2User = new HashMap<>();
newInstance.user2Account = new HashMap<>();
newInstance.itemName2Product = new HashMap<>();
instance = newInstance;
}
return instance;
}

View File

@ -38,13 +38,9 @@ public class MaintenanceLock {
*
* @return singleton instance of MaintenanceLock
*/
public static MaintenanceLock getInstance() {
public static synchronized MaintenanceLock getInstance() {
if (instance == null) {
synchronized (MaintenanceLock.class) {
if (instance == null) {
instance = new MaintenanceLock();
}
}
instance = new MaintenanceLock();
}
return instance;
}
@ -55,6 +51,6 @@ public class MaintenanceLock {
public void setLock(boolean lock) {
this.lock = lock;
LOGGER.info("Maintenance lock is set to: " + lock);
LOGGER.info("Maintenance lock is set to: ", lock);
}
}

View File

@ -49,7 +49,7 @@ public class HotelDaoImpl implements HotelDao {
public Stream<Room> getAll() throws Exception {
try {
var connection = getConnection();
var statement = connection.prepareStatement("SELECT * FROM ROOMS");
var statement = connection.prepareStatement("SELECT * FROM ROOMS"); // NOSONAR
var resultSet = statement.executeQuery(); // NOSONAR
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Room>(Long.MAX_VALUE,
Spliterator.ORDERED) {