File tree 6 files changed +19
-23
lines changed
main/java/com/iluwatar/mutex
test/java/com/iluwatar/mutex
6 files changed +19
-23
lines changed Original file line number Diff line number Diff line change @@ -38,10 +38,10 @@ public class App {
38
38
* main method.
39
39
*/
40
40
public static void main (String [] args ) {
41
- Mutex mutex = new Mutex ();
42
- Jar jar = new Jar (1000 , mutex );
43
- Thief peter = new Thief ("Peter" , jar );
44
- Thief john = new Thief ("John" , jar );
41
+ var mutex = new Mutex ();
42
+ var jar = new Jar (1000 , mutex );
43
+ var peter = new Thief ("Peter" , jar );
44
+ var john = new Thief ("John" , jar );
45
45
peter .start ();
46
46
john .start ();
47
47
}
Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ public Jar(int beans, Lock lock) {
48
48
* Method for a thief to take a bean.
49
49
*/
50
50
public boolean takeBean () {
51
- boolean success = false ;
51
+ var success = false ;
52
52
try {
53
53
lock .acquire ();
54
54
success = beans > 0 ;
Original file line number Diff line number Diff line change @@ -54,7 +54,7 @@ public Thief(String name, Jar jar) {
54
54
*/
55
55
@ Override
56
56
public void run () {
57
- int beans = 0 ;
57
+ var beans = 0 ;
58
58
59
59
while (jar .takeBean ()) {
60
60
beans = beans + 1 ;
Original file line number Diff line number Diff line change 25
25
26
26
import org .junit .jupiter .api .Test ;
27
27
28
- import java .io .IOException ;
29
-
30
28
/**
31
29
* Application Test Entrypoint
32
30
*/
33
31
public class AppTest {
34
32
@ Test
35
- public void test () throws IOException {
36
- String [] args = {};
37
- App .main (args );
33
+ public void test () {
34
+ App .main (new String []{});
38
35
}
39
36
}
Original file line number Diff line number Diff line change 23
23
24
24
package com .iluwatar .mutex ;
25
25
26
- import org .junit .jupiter .api .Test ;
27
-
28
26
import static org .junit .jupiter .api .Assertions .assertFalse ;
29
- import static org .junit .jupiter .api .Assertions .assertTrue ;
27
+
28
+ import java .util .stream .IntStream ;
29
+ import org .junit .jupiter .api .Assertions ;
30
+ import org .junit .jupiter .api .Test ;
30
31
31
32
/**
32
33
* Test case for taking beans from a Jar
@@ -35,12 +36,10 @@ public class JarTest {
35
36
36
37
@ Test
37
38
public void testTakeBeans () {
38
- Mutex mutex = new Mutex ();
39
- Jar jar = new Jar (10 , mutex );
40
- for (int i = 0 ; i < 10 ; i ++) {
41
- assertTrue (jar .takeBean ());
42
- }
39
+ var mutex = new Mutex ();
40
+ var jar = new Jar (10 , mutex );
41
+ IntStream .range (0 , 10 ).mapToObj (i -> jar .takeBean ()).forEach (Assertions ::assertTrue );
43
42
assertFalse (jar .takeBean ());
44
43
}
45
44
46
- }
45
+ }
Original file line number Diff line number Diff line change 23
23
24
24
package com .iluwatar .mutex ;
25
25
26
- import org .junit .jupiter .api .Test ;
27
-
28
26
import static org .junit .jupiter .api .Assertions .assertEquals ;
29
27
import static org .junit .jupiter .api .Assertions .assertNull ;
30
28
import static org .junit .jupiter .api .Assertions .fail ;
31
29
30
+ import org .junit .jupiter .api .Test ;
31
+
32
32
/**
33
33
* Test case for acquiring and releasing a Mutex
34
34
*/
35
35
public class MutexTest {
36
36
37
37
@ Test
38
38
public void acquireReleaseTest () {
39
- Mutex mutex = new Mutex ();
39
+ var mutex = new Mutex ();
40
40
assertNull (mutex .getOwner ());
41
41
try {
42
42
mutex .acquire ();
You can’t perform that action at this time.
0 commit comments