Skip to content

Commit 2c042a3

Browse files
committed
fixed checkstyle warnings
1 parent ede5f1d commit 2c042a3

File tree

6 files changed

+141
-31
lines changed

6 files changed

+141
-31
lines changed

bloC/Readme.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ The `State` class holds the representation of the state of the application that
5454
```java
5555
package com.iluwatar.bloc;
5656

57+
import lombok.Getter;
58+
59+
@Getter
5760
public class State {
5861
private final int value;
5962

6063
public State(int value) {
6164
this.value = value;
6265
}
6366

64-
public int getValue() {
65-
return value;
66-
}
6767
}
6868
```
6969
The `ListenerManager` interface manages the basic operations for the listeners and is implemented by bloc class

bloC/src/main/java/com/iluwatar/bloc/Bloc.java

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,80 @@
11
package com.iluwatar.bloc;
2+
23
import java.util.ArrayList;
34
import java.util.Collections;
45
import java.util.List;
56

7+
/**
8+
* The Bloc class is responsible for managing the current state and notifying registered listeners
9+
* whenever the state changes. It implements the ListenerManager interface, allowing listeners
10+
* to be added, removed, and notified of state changes.
11+
*/
612
public class Bloc implements ListenerManager<State> {
13+
714
private State currentState;
815
private final List<StateListener<State>> listeners = new ArrayList<>();
916

17+
/**
18+
* Constructs a new Bloc instance with an initial state of value 0.
19+
*/
1020
public Bloc() {
1121
this.currentState = new State(0);
1222
}
1323

24+
/**
25+
* Adds a listener to receive state change notifications.
26+
*
27+
* @param listener the listener to add
28+
*/
1429
@Override
1530
public void addListener(StateListener<State> listener) {
1631
listeners.add(listener);
1732
listener.onStateChange(currentState);
1833
}
1934

35+
/**
36+
* Removes a listener from receiving state change notifications.
37+
*
38+
* @param listener the listener to remove
39+
*/
2040
@Override
2141
public void removeListener(StateListener<State> listener) {
2242
listeners.remove(listener);
2343
}
2444

45+
/**
46+
* Returns an unmodifiable list of all registered listeners.
47+
*
48+
* @return an unmodifiable list of listeners
49+
*/
2550
@Override
2651
public List<StateListener<State>> getListeners() {
2752
return Collections.unmodifiableList(listeners);
2853
}
2954

55+
/**
56+
* Emits a new state and notifies all registered listeners of the change.
57+
*
58+
* @param newState the new state to emit
59+
*/
3060
private void emitState(State newState) {
3161
currentState = newState;
3262
for (StateListener<State> listener : listeners) {
3363
listener.onStateChange(currentState);
3464
}
3565
}
3666

67+
/**
68+
* Increments the current state value by 1 and notifies listeners of the change.
69+
*/
3770
public void increment() {
3871
emitState(new State(currentState.getValue() + 1));
3972
}
4073

74+
/**
75+
* Decrements the current state value by 1 and notifies listeners of the change.
76+
*/
4177
public void decrement() {
4278
emitState(new State(currentState.getValue() - 1));
4379
}
4480
}
45-

bloC/src/main/java/com/iluwatar/bloc/ListenerManager.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
package com.iluwatar.bloc;
22
import java.util.List;
33

4+
/**
5+
* Interface for managing listeners for state changes.
6+
*
7+
* @param <T> The type of state to be handled by the listeners.
8+
*/
9+
410
public interface ListenerManager<T> {
11+
12+
/**
13+
* Adds a listener that will be notified of state changes.
14+
*
15+
* @param listener the listener to be added
16+
*/
517
void addListener(StateListener<T> listener);
18+
19+
/**
20+
* Removes a listener so that it no longer receives state change notifications.
21+
*
22+
* @param listener the listener to be removed
23+
*/
624
void removeListener(StateListener<T> listener);
25+
26+
/**
27+
* Returns a list of all listeners currently registered for state changes.
28+
*
29+
* @return a list of registered listeners
30+
*/
731
List<StateListener<T>> getListeners();
832
}
9-
+45-16
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,74 @@
11
package com.iluwatar.bloc;
2-
import javax.swing.*;
3-
import java.awt.*;
2+
import java.awt.BorderLayout;
3+
import java.awt.Font;
4+
import javax.swing.JButton;
5+
import javax.swing.JFrame;
6+
import javax.swing.JLabel;
7+
import javax.swing.SwingConstants;
8+
import javax.swing.WindowConstants;
49

5-
public class Main
6-
{
7-
public static void main(String[] args)
8-
{
9-
Bloc bloC = new Bloc();
10+
11+
12+
13+
/**
14+
* The Main class demonstrates the use of the Bloc pattern in a simple GUI application.
15+
* It creates a JFrame with buttons to increment, decrement, and toggle a listener
16+
* that updates the counter value on the screen.
17+
*/
18+
public class Main {
19+
20+
/**
21+
* The entry point of the application. Initializes the GUI and sets up actions
22+
* for the buttons and listener management.
23+
*
24+
* @param args command-line arguments (not used in this example)
25+
*/
26+
public static void main(String[] args) {
27+
// Create a Bloc instance to manage the state
28+
29+
30+
// Create and set up the JFrame
1031
JFrame frame = new JFrame("BloC example");
1132
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
1233
frame.setSize(400, 300);
34+
35+
// Create a label to display the counter value
1336
JLabel counterLabel = new JLabel("Counter: 0", SwingConstants.CENTER);
1437
counterLabel.setFont(new Font("Arial", Font.BOLD, 20));
38+
39+
// Create buttons for increment, decrement, and toggling listener
1540
JButton incrementButton = new JButton("Increment");
1641
JButton decrementButton = new JButton("Decrement");
1742
JButton toggleListenerButton = new JButton("Disable Listener");
43+
44+
// Set layout and add components to the frame
1845
frame.setLayout(new BorderLayout());
1946
frame.add(counterLabel, BorderLayout.CENTER);
2047
frame.add(incrementButton, BorderLayout.NORTH);
2148
frame.add(decrementButton, BorderLayout.SOUTH);
2249
frame.add(toggleListenerButton, BorderLayout.EAST);
23-
50+
Bloc bloC = new Bloc();
51+
// Create a state listener to update the counter label when the state changes
2452
StateListener<State> stateListener = state -> counterLabel.setText("Counter: " + state.getValue());
2553

54+
// Add the listener to the Bloc instance
2655
bloC.addListener(stateListener);
2756

28-
toggleListenerButton.addActionListener(e ->
29-
{
30-
if (bloC.getListeners().contains(stateListener))
31-
{
57+
// Set action listeners for buttons
58+
toggleListenerButton.addActionListener(e -> {
59+
if (bloC.getListeners().contains(stateListener)) {
3260
bloC.removeListener(stateListener);
3361
toggleListenerButton.setText("Enable Listener");
34-
} else
35-
{
62+
} else {
3663
bloC.addListener(stateListener);
3764
toggleListenerButton.setText("Disable Listener");
3865
}
39-
}
40-
);
66+
});
67+
4168
incrementButton.addActionListener(e -> bloC.increment());
4269
decrementButton.addActionListener(e -> bloC.decrement());
70+
71+
// Make the frame visible
4372
frame.setVisible(true);
4473
}
4574
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
package com.iluwatar.bloc;
22

3-
public class State
4-
{
3+
import lombok.Getter;
4+
5+
/**
6+
* The {@code State} class represents a state with an integer value.
7+
* This class encapsulates the value and provides methods to retrieve it.
8+
*/
9+
@Getter
10+
public class State {
11+
/**
12+
* -- GETTER --
13+
* Returns the value of the state.
14+
*
15+
*/
516
private final int value;
617

7-
public State(int value)
8-
{
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) {
924
this.value = value;
1025
}
11-
public int getValue()
12-
{
13-
return value;
14-
}
26+
1527
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
package com.iluwatar.bloc;
22

3-
public interface StateListener<T>
4-
{
3+
/**
4+
* The {@code StateListener} interface defines the contract for listening to state changes.
5+
* Implementations of this interface should handle state changes and define actions to take when the state changes.
6+
*
7+
* @param <T> the type of state that this listener will handle
8+
*/
9+
public interface StateListener<T> {
10+
11+
/**
12+
* This method is called when the state has changed.
13+
*
14+
* @param state the updated state
15+
*/
516
void onStateChange(T state);
617
}

0 commit comments

Comments
 (0)