Skip to content

Commit b51bca1

Browse files
authored
fix: monitor pattern is not being built (#1956)
* fix: update the version in pom.xml * fixes the checksyle error and adds javadoc * fix: bugs and code-smells in sonarcloud * replaced logger library with Slf4j * fix tests and add a previously dropped method * adds license * fix: codesmells and bug * replace Random with SecureRandom * test: add tests for Main to improve coverage
1 parent 3b87565 commit b51bca1

File tree

7 files changed

+245
-123
lines changed

7 files changed

+245
-123
lines changed

��monitor/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2222
<modelVersion>4.0.0</modelVersion>
2323
<parent>
24-
<artifactId>java-design-patterns</artifactId>
2524
<groupId>com.iluwatar</groupId>
26-
<version>1.24.0-SNAPSHOT</version>
25+
<artifactId>java-design-patterns</artifactId>
26+
<version>1.26.0-SNAPSHOT</version>
2727
</parent>
2828
<artifactId>monitor</artifactId>
2929
<dependencies>
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,87 @@
1+
/*
2+
*The MIT License
3+
*Copyright © 2014-2021 Ilkka Seppälä
4+
*
5+
*Permission is hereby granted, free of charge, to any person obtaining a copy
6+
*of this software and associated documentation files (the "Software"), to deal
7+
*in the Software without restriction, including without limitation the rights
8+
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
*copies of the Software, and to permit persons to whom the Software is
10+
*furnished to do so, subject to the following conditions:
11+
*
12+
*The above copyright notice and this permission notice shall be included in
13+
*all copies or substantial portions of the Software.
14+
*
15+
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
*THE SOFTWARE.
22+
*/
23+
124
package com.iluwatar.monitor;
225

326
import java.util.Arrays;
4-
import java.util.logging.Logger;
27+
import lombok.extern.slf4j.Slf4j;
528

6-
// Bank class implements the Monitor pattern
29+
/** Bank Definition. */
30+
@Slf4j
731
public class Bank {
832

9-
private int[] accounts;
10-
Logger logger;
33+
private final int[] accounts;
1134

12-
public Bank(int accountNum, int baseAmount, Logger logger) {
13-
this.logger = logger;
14-
accounts = new int[accountNum];
15-
Arrays.fill(accounts, baseAmount);
16-
}
35+
/**
36+
* Constructor.
37+
*
38+
* @param accountNum - account number
39+
* @param baseAmount - base amount
40+
*/
41+
public Bank(int accountNum, int baseAmount) {
42+
accounts = new int[accountNum];
43+
Arrays.fill(accounts, baseAmount);
44+
}
1745

18-
public synchronized void transfer(int accountA, int accountB, int amount) {
19-
if (accounts[accountA] >= amount) {
20-
accounts[accountB] += amount;
21-
accounts[accountA] -= amount;
22-
logger.info("Transferred from account :" + accountA + " to account :" + accountB + " , amount :" + amount + " . balance :" + getBalance());
23-
}
46+
/**
47+
* Transfer amounts from one account to another.
48+
*
49+
* @param accountA - source account
50+
* @param accountB - destination account
51+
* @param amount - amount to be transferred
52+
*/
53+
public synchronized void transfer(int accountA, int accountB, int amount) {
54+
if (accounts[accountA] >= amount) {
55+
accounts[accountB] += amount;
56+
accounts[accountA] -= amount;
57+
LOGGER.info(
58+
"Transferred from account: {} to account: {} , amount: {} , balance: {}",
59+
accountA,
60+
accountB,
61+
amount,
62+
getBalance());
2463
}
64+
}
2565

26-
public synchronized int getBalance() {
27-
int balance = 0;
28-
for (int account : accounts) {
29-
balance += account;
30-
}
31-
return balance;
66+
/**
67+
* Calculates the total balance.
68+
*
69+
* @return balance
70+
*/
71+
public synchronized int getBalance() {
72+
int balance = 0;
73+
for (int account : accounts) {
74+
balance += account;
3275
}
76+
return balance;
77+
}
3378

34-
public int[] getAccounts() {
35-
return accounts;
36-
}
79+
/**
80+
* Get all accounts.
81+
*
82+
* @return accounts
83+
*/
84+
public int[] getAccounts() {
85+
return accounts;
86+
}
3787
}
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,73 @@
11
/*
2-
* The MIT License
3-
* Copyright © 2014-2021 Ilkka Seppälä
2+
*The MIT License
3+
*Copyright © 2014-2021 Ilkka Seppälä
44
*
5-
* Permission is hereby granted, free of charge, to any person obtaining a copy
6-
* of this software and associated documentation files (the "Software"), to deal
7-
* in the Software without restriction, including without limitation the rights
8-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
* copies of the Software, and to permit persons to whom the Software is
10-
* furnished to do so, subject to the following conditions:
5+
*Permission is hereby granted, free of charge, to any person obtaining a copy
6+
*of this software and associated documentation files (the "Software"), to deal
7+
*in the Software without restriction, including without limitation the rights
8+
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
*copies of the Software, and to permit persons to whom the Software is
10+
*furnished to do so, subject to the following conditions:
1111
*
12-
* The above copyright notice and this permission notice shall be included in
13-
* all copies or substantial portions of the Software.
12+
*The above copyright notice and this permission notice shall be included in
13+
*all copies or substantial portions of the Software.
1414
*
15-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21-
* THE SOFTWARE.
15+
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
*THE SOFTWARE.
2222
*/
2323

2424
package com.iluwatar.monitor;
2525

26-
import java.util.*;
26+
import java.security.SecureRandom;
2727
import java.util.concurrent.ExecutorService;
2828
import java.util.concurrent.Executors;
29-
import java.util.logging.Logger;
29+
import lombok.extern.slf4j.Slf4j;
3030

3131
/**
32-
* <p>The Monitor pattern is used in concurrent algorithms to achieve mutual exclusion.</p>
32+
* The Monitor pattern is used in concurrent algorithms to achieve mutual exclusion.
3333
*
34-
* <p>Bank is a simple class that transfers money from an account to another account using
35-
* {@link Bank#transfer}. It can also return the balance of the bank account stored in the bank.</p>
34+
* <p>Bank is a simple class that transfers money from an account to another account using {@link
35+
* Bank#transfer}. It can also return the balance of the bank account stored in the bank.
3636
*
37-
* <p>Main class uses ThreadPool to run threads that do transactions on the bank accounts.</p>
37+
* <p>Main class uses ThreadPool to run threads that do transactions on the bank accounts.
3838
*/
39-
39+
@Slf4j
4040
public class Main {
4141

42-
public static void main(String[] args) {
43-
Logger logger = Logger.getLogger("monitor");
44-
var bank = new Bank(4, 1000, logger);
45-
Runnable runnable = () -> {
46-
try {
47-
Thread.sleep((long) (Math.random() * 1000));
48-
Random random = new Random();
49-
for (int i = 0; i < 1000000; i++)
50-
bank.transfer(random.nextInt(4), random.nextInt(4), (int) (Math.random() * 1000));
51-
} catch (InterruptedException e) {
52-
logger.info(e.getMessage());
53-
}
54-
};
55-
ExecutorService executorService = Executors.newFixedThreadPool(5);
56-
for (int i = 0; i < 5; i++) {
57-
executorService.execute(runnable);
58-
}
42+
/**
43+
* Runner to perform a bunch of transfers and handle exception.
44+
*
45+
* @param bank bank object
46+
*/
47+
public static void runner(Bank bank) {
48+
try {
49+
SecureRandom random = new SecureRandom();
50+
Thread.sleep(random.nextInt(1000));
51+
for (int i = 0; i < 1000000; i++) {
52+
bank.transfer(random.nextInt(4), random.nextInt(4), random.nextInt());
53+
}
54+
} catch (InterruptedException e) {
55+
LOGGER.info(e.getMessage());
56+
Thread.currentThread().interrupt();
57+
}
58+
}
59+
60+
/**
61+
* Program entry point.
62+
*
63+
* @param args command line args
64+
*/
65+
public static void main(String[] args) {
66+
var bank = new Bank(4, 1000);
67+
Runnable runnable = () -> runner(bank);
68+
ExecutorService executorService = Executors.newFixedThreadPool(5);
69+
for (int i = 0; i < 5; i++) {
70+
executorService.execute(runnable);
5971
}
60-
}
72+
}
73+
}

‎monitor/src/main/test/java/com/iluwater/java/BankTest.java

-55
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
*The MIT License
3+
*Copyright © 2014-2021 Ilkka Seppälä
4+
*
5+
*Permission is hereby granted, free of charge, to any person obtaining a copy
6+
*of this software and associated documentation files (the "Software"), to deal
7+
*in the Software without restriction, including without limitation the rights
8+
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
*copies of the Software, and to permit persons to whom the Software is
10+
*furnished to do so, subject to the following conditions:
11+
*
12+
*The above copyright notice and this permission notice shall be included in
13+
*all copies or substantial portions of the Software.
14+
*
15+
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
*THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.monitor;
25+
26+
import org.junit.jupiter.api.AfterAll;
27+
import org.junit.jupiter.api.BeforeAll;
28+
import org.junit.jupiter.api.Test;
29+
import static org.junit.jupiter.api.Assertions.*;
30+
import static org.junit.jupiter.api.Assumptions.*;
31+
32+
public class BankTest {
33+
34+
private static final int ACCOUNT_NUM = 4;
35+
private static final int BASE_AMOUNT = 1000;
36+
private static Bank bank;
37+
38+
@BeforeAll
39+
public static void Setup() {
40+
bank = new Bank(ACCOUNT_NUM, BASE_AMOUNT);
41+
}
42+
43+
@AfterAll
44+
public static void TearDown() {
45+
bank = null;
46+
}
47+
48+
@Test
49+
void GetAccountHaveNotBeNull() {
50+
assertNotNull(bank.getAccounts());
51+
}
52+
53+
@Test
54+
void LengthOfAccountsHaveToEqualsToAccountNumConstant() {
55+
assumeTrue(bank.getAccounts() != null);
56+
assertEquals(ACCOUNT_NUM, bank.getAccounts().length);
57+
}
58+
59+
@Test
60+
void TransferMethodHaveToTransferAmountFromAnAccountToOtherAccount() {
61+
bank.transfer(0, 1, 1000);
62+
int[] accounts = bank.getAccounts();
63+
assertEquals(0, accounts[0]);
64+
assertEquals(2000, accounts[1]);
65+
}
66+
67+
@Test
68+
void BalanceHaveToBeOK() {
69+
assertEquals(4000, bank.getBalance());
70+
}
71+
}

0 commit comments

Comments
 (0)