File tree Expand file tree Collapse file tree 5 files changed +10
-29
lines changed
main/java/com/iluwatar/bloc
test/java/com/iluwatar/bloc Expand file tree Collapse file tree 5 files changed +10
-29
lines changed Original file line number Diff line number Diff line change @@ -68,13 +68,13 @@ private void emitState(State newState) {
68
68
* Increments the current state value by 1 and notifies listeners of the change.
69
69
*/
70
70
public void increment () {
71
- emitState (new State (currentState .getValue () + 1 ));
71
+ emitState (new State (currentState .value () + 1 ));
72
72
}
73
73
74
74
/**
75
75
* Decrements the current state value by 1 and notifies listeners of the change.
76
76
*/
77
77
public void decrement () {
78
- emitState (new State (currentState .getValue () - 1 ));
78
+ emitState (new State (currentState .value () - 1 ));
79
79
}
80
80
}
Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ public void createAndShowUi() {
41
41
frame .add (toggleListenerButton , BorderLayout .EAST );
42
42
43
43
// making a state listener to update the counter label when the state changes
44
- StateListener <State > stateListener = state -> counterLabel .setText ("Counter: " + state .getValue ());
44
+ StateListener <State > stateListener = state -> counterLabel .setText ("Counter: " + state .value ());
45
45
46
46
// adding the listener to the Bloc instance
47
47
bloc .addListener (stateListener );
Original file line number Diff line number Diff line change 1
1
package com .iluwatar .bloc ;
2
2
3
- import lombok .Getter ;
4
-
5
3
/**
6
4
* The {@code State} class represents a state with an integer value.
7
5
* This class encapsulates the value and provides methods to retrieve it.
8
6
*/
9
- @ Getter
10
- public class State {
11
- /**
12
- * -- GETTER --
13
- * Returns the value of the state.
14
- *
15
- */
16
- private final int value ;
17
-
18
- /**
19
- * Constructs a {@code State} with the specified value.
20
- *
21
- * @param value the value of the state
22
- */
23
- public State (int value ) {
24
- this .value = value ;
25
- }
26
-
27
- }
7
+ public record State (int value ) {
8
+ }
Original file line number Diff line number Diff line change @@ -19,14 +19,14 @@ void initialState() {
19
19
20
20
@ Test
21
21
void IncrementUpdateState () {
22
- bloc .addListener (state -> stateValue .set (state .getValue ()));
22
+ bloc .addListener (state -> stateValue .set (state .value ()));
23
23
bloc .increment ();
24
24
assertEquals (1 , stateValue .get (), "State should increment to 1" );
25
25
}
26
26
27
27
@ Test
28
28
void DecrementUpdateState () {
29
- bloc .addListener (state -> stateValue .set (state .getValue ()));
29
+ bloc .addListener (state -> stateValue .set (state .value ()));
30
30
bloc .decrement ();
31
31
assertEquals (-1 , stateValue .get (), "State should decrement to -1" );
32
32
}
@@ -47,8 +47,8 @@ void removingListener() {
47
47
@ Test
48
48
void multipleListeners () {
49
49
AtomicInteger secondValue = new AtomicInteger ();
50
- bloc .addListener (state -> stateValue .set (state .getValue ()));
51
- bloc .addListener (state -> secondValue .set (state .getValue ()));
50
+ bloc .addListener (state -> stateValue .set (state .value ()));
51
+ bloc .addListener (state -> secondValue .set (state .value ()));
52
52
bloc .increment ();
53
53
assertEquals (1 , stateValue .get (), "First listener should receive state 1." );
54
54
assertEquals (1 , secondValue .get (), "Second listener should receive state 1." );
Original file line number Diff line number Diff line change @@ -40,7 +40,7 @@ public void setUp() {
40
40
frame .add (decrementButton , BorderLayout .SOUTH );
41
41
frame .add (toggleListenerButton , BorderLayout .EAST );
42
42
43
- stateListener = state -> counterLabel .setText ("Counter: " + state .getValue ());
43
+ stateListener = state -> counterLabel .setText ("Counter: " + state .value ());
44
44
bloc .addListener (stateListener );
45
45
46
46
incrementButton .addActionListener (e -> bloc .increment ());
You can’t perform that action at this time.
0 commit comments