Skip to content

Commit

Permalink
style: fix concurrency solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
DanySK committed Dec 3, 2024
1 parent 0d56f40 commit 6aa7374
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package it.unibo.oop;

import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;

/**
* Utility class for JFrame.
*/
public final class JFrameUtil {

private static final double WIDTH_PERC = 0.2;
private static final double HEIGHT_PERC = 0.1;

private JFrameUtil() {
}

/**
* Dimensions a JFrame to a percentage of the screen size.
*
* @param target the JFrame to dimension
*/
public static void dimensionJFrame(final JFrame target) {
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
target.setSize((int) (screenSize.getWidth() * WIDTH_PERC), (int) (screenSize.getHeight() * HEIGHT_PERC));
target.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package it.unibo.oop.reactivegui01;


import java.awt.Dimension;
import java.awt.Toolkit;
import it.unibo.oop.JFrameUtil;

import java.io.Serial;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
Expand All @@ -18,22 +19,19 @@
@SuppressWarnings("PMD.AvoidPrintStackTrace")
public final class ConcurrentGUI extends JFrame {

@Serial
private static final long serialVersionUID = 1L;
private static final double WIDTH_PERC = 0.2;
private static final double HEIGHT_PERC = 0.1;
private final JLabel display = new JLabel();
private final JButton stop = new JButton("stop");

/**
* Builds a new CGUI.
*/
public ConcurrentGUI() {
super();
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize((int) (screenSize.getWidth() * WIDTH_PERC), (int) (screenSize.getHeight() * HEIGHT_PERC));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JFrameUtil.dimensionJFrame(this);
final JPanel panel = new JPanel();
panel.add(display);
final JButton stop = new JButton("stop");
panel.add(stop);
this.getContentPane().add(panel);
this.setVisible(true);
Expand All @@ -54,7 +52,7 @@ public ConcurrentGUI() {
* The counter agent is implemented as a nested class. This makes it
* invisible outside and encapsulated.
*/
private class Agent implements Runnable {
private final class Agent implements Runnable {
/*
* Stop is volatile to ensure visibility. Look at:
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package it.unibo.oop.reactivegui02;

import java.awt.Dimension;
import java.awt.Toolkit;
import it.unibo.oop.JFrameUtil;

import java.io.Serial;

import javax.swing.JButton;
import javax.swing.JFrame;
Expand All @@ -15,9 +16,8 @@
@SuppressWarnings("PMD.AvoidPrintStackTrace")
public final class ConcurrentGUI extends JFrame {

@Serial
private static final long serialVersionUID = -6218820567019985015L;
private static final double WIDTH_PERC = 0.2;
private static final double HEIGHT_PERC = 0.1;

private final JLabel display = new JLabel();

Expand All @@ -26,9 +26,7 @@ public final class ConcurrentGUI extends JFrame {
*/
public ConcurrentGUI() {
super();
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize((int) (screenSize.getWidth() * WIDTH_PERC), (int) (screenSize.getHeight() * HEIGHT_PERC));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JFrameUtil.dimensionJFrame(this);
final JPanel panel = new JPanel();
panel.add(display);
final JButton stop = new JButton("stop");
Expand All @@ -51,7 +49,7 @@ public ConcurrentGUI() {
new Thread(agent).start();
}

private class Agent implements Runnable {
private final class Agent implements Runnable {
private volatile boolean stop;
private volatile boolean up = true;
private int counter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package it.unibo.oop.reactivegui03;

import java.awt.Dimension;
import java.awt.Toolkit;
import it.unibo.oop.JFrameUtil;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serial;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.TimeUnit;

Expand All @@ -20,8 +22,6 @@
public final class AnotherConcurrentGUI extends JFrame {

private static final long serialVersionUID = 1L;
private static final double WIDTH_PERC = 0.2;
private static final double HEIGHT_PERC = 0.1;
private static final long WAITING_TIME = TimeUnit.SECONDS.toMillis(10);

private final JLabel display = new JLabel();
Expand All @@ -35,9 +35,7 @@ public final class AnotherConcurrentGUI extends JFrame {
* Builds a C3GUI.
*/
public AnotherConcurrentGUI() {
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize((int) (screenSize.getWidth() * WIDTH_PERC), (int) (screenSize.getHeight() * HEIGHT_PERC));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JFrameUtil.dimensionJFrame(this);
final JPanel panel = new JPanel();
panel.add(display);
panel.add(up);
Expand Down Expand Up @@ -89,7 +87,9 @@ public void run() {
});
}

private class CounterAgent implements Runnable, java.io.Serializable {
private final class CounterAgent implements Runnable, Serializable {

@Serial
private static final long serialVersionUID = 1L;
private volatile boolean stop;
private volatile boolean up = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package it.unibo.oop.reactivegui03;

import java.awt.Dimension;
import java.awt.Toolkit;
import it.unibo.oop.JFrameUtil;

import java.io.Serial;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.TimeUnit;

Expand All @@ -17,6 +19,7 @@
@SuppressWarnings("PMD.AvoidPrintStackTrace")
public final class AnotherConcurrentGUIWithLambdas extends JFrame {

@Serial
private static final long serialVersionUID = 1L;
private static final double WIDTH_PERC = 0.2;
private static final double HEIGHT_PERC = 0.1;
Expand All @@ -34,9 +37,7 @@ public final class AnotherConcurrentGUIWithLambdas extends JFrame {
*/
@SuppressWarnings("CPD-START")
public AnotherConcurrentGUIWithLambdas() {
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize((int) (screenSize.getWidth() * WIDTH_PERC), (int) (screenSize.getHeight() * HEIGHT_PERC));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JFrameUtil.dimensionJFrame(this);
final JPanel panel = new JPanel();
panel.add(display);
panel.add(up);
Expand Down Expand Up @@ -67,7 +68,9 @@ private void stopCounting() {
});
}

private class CounterAgent implements Runnable, java.io.Serializable {
private final class CounterAgent implements Runnable, Serializable {

@Serial
private static final long serialVersionUID = 1L;
private volatile boolean stop;
private volatile boolean up = true;
Expand Down
2 changes: 1 addition & 1 deletion tests.main.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ File("java/inheritance/").inAllDirectories {
gradle("compileJava")
}

listOf("collections", "generics", "exceptions", "lambdas", "streams").forEach {
listOf("collections", "generics", "exceptions", "lambdas", "streams", "concurrency").forEach {
File("java/$it").inAllDirectories {
val tasks = arrayOf("build") + when (git.currentBranch()) {
"exercises" -> arrayOf("-x", "test")
Expand Down

0 comments on commit 6aa7374

Please sign in to comment.