File tree Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Original file line number Diff line number Diff line change
1
+ import com .google .common .base .Preconditions ;
1
2
import lombok .Value ;
2
3
3
4
/**
@@ -8,11 +9,25 @@ class User {
8
9
int id ;
9
10
int balance ;
10
11
11
- User income (int value ) {
12
- return new User (id , balance + value );
12
+ User income (PositiveInt value ) {
13
+ return new User (id , balance + value . amount );
13
14
}
14
15
15
- User outcome (int value ) {
16
- return new User (id , balance - value );
16
+ User outcome (PositiveInt value ) {
17
+ return new User (id , balance - value .amount );
18
+ }
19
+ }
20
+
21
+ class PositiveInt {
22
+ final int amount ;
23
+
24
+ private PositiveInt (int amount ) {
25
+ this .amount = amount ;
26
+ }
27
+
28
+ static PositiveInt of (int amount ) {
29
+ Preconditions .checkArgument (amount > 0 );
30
+
31
+ return new PositiveInt (amount );
17
32
}
18
33
}
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ public void transfer() {
23
23
LockExecutor executor = new LockExecutor (lock );
24
24
25
25
executor .write (() -> {
26
- int transfer = 15 ;
26
+ var transfer = PositiveInt . of ( 15 ) ;
27
27
userMap .replace (1 , userMap .get (1 ).outcome (transfer ));
28
28
userMap .replace (2 , userMap .get (2 ).income (transfer ));
29
29
});
You can’t perform that action at this time.
0 commit comments