Merge branch 'eugenp:master' into deepShallowCopy
This commit is contained in:
-15
@@ -1,15 +0,0 @@
|
||||
package reminderapplication;
|
||||
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.Insets;
|
||||
|
||||
public class ConstraintsBuilder {
|
||||
|
||||
static GridBagConstraints constraint(int x, int y) {
|
||||
final GridBagConstraints gridBagConstraints = new GridBagConstraints();
|
||||
gridBagConstraints.gridx = x;
|
||||
gridBagConstraints.gridy = y;
|
||||
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
|
||||
return gridBagConstraints;
|
||||
}
|
||||
}
|
||||
-194
@@ -1,194 +0,0 @@
|
||||
package reminderapplication;
|
||||
|
||||
import static reminderapplication.ConstraintsBuilder.*;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.HeadlessException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.swing.DefaultComboBoxModel;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
public class EditReminderFrame extends JFrame {
|
||||
|
||||
private static Timer TIMER = new Timer();
|
||||
|
||||
private final TimeReminderApplication reminderApplication;
|
||||
private final JLabel reminderTextLabel;
|
||||
private final JLabel repeatPeriodLabel;
|
||||
private final JLabel setDelayLabel;
|
||||
private final JComboBox<Integer> delay;
|
||||
private final JComboBox<Integer> period;
|
||||
private final JButton cancelButton;
|
||||
private final JButton okButton;
|
||||
private final JTextField textField;
|
||||
private final JLabel delaysLabel;
|
||||
private final JLabel periodLabel;
|
||||
|
||||
private final int reminderIndex;
|
||||
|
||||
public EditReminderFrame(TimeReminderApplication reminderApp, String reminderText, int delayInSeconds, int periodInSeconds, int index) throws HeadlessException {
|
||||
this.reminderApplication = reminderApp;
|
||||
reminderIndex = index;
|
||||
textField = createTextField(reminderText);
|
||||
delay = createDelayComboBox(delayInSeconds);
|
||||
period = createPeriodComboBox(periodInSeconds);
|
||||
cancelButton = createCancelButton();
|
||||
okButton = createOkButton();
|
||||
reminderTextLabel = createReminderTextLabel();
|
||||
repeatPeriodLabel = createRepeatPeriodLabel();
|
||||
setDelayLabel = createSetDelayLabel();
|
||||
delaysLabel = createDelaysLabel();
|
||||
periodLabel = createPeriodLabel();
|
||||
configureVisualRepresentation();
|
||||
configureActions();
|
||||
}
|
||||
|
||||
private void configureActions() {
|
||||
updateReminder();
|
||||
}
|
||||
|
||||
private void configureVisualRepresentation() {
|
||||
configureFrame();
|
||||
setLocationRelativeTo(null);
|
||||
setLayout(new GridBagLayout());
|
||||
add(reminderTextLabel, constraint(0,0));
|
||||
add(repeatPeriodLabel, constraint(1,0));
|
||||
add(setDelayLabel, constraint(2,0));
|
||||
add(textField, constraint(0, 1));
|
||||
add(delay, constraint(1, 1));
|
||||
add(period, constraint(2, 1));
|
||||
add(delaysLabel, constraint(1,3));
|
||||
add(periodLabel, constraint(2,3));
|
||||
add(okButton, constraint(1, 4));
|
||||
add(cancelButton, constraint(2, 4));
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void configureFrame() {
|
||||
setTitle("Set Reminder");
|
||||
setName("Set Reminder");
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
}
|
||||
|
||||
private static JLabel createSetDelayLabel() {
|
||||
return createLabel("Set Delay", "Set Delay Label");
|
||||
}
|
||||
|
||||
private static JLabel createRepeatPeriodLabel() {
|
||||
return createLabel("Set Period", "Set Repeat Period Label");
|
||||
}
|
||||
|
||||
private static JLabel createReminderTextLabel() {
|
||||
return createLabel("Reminder Text", "Reminder Text Label");
|
||||
}
|
||||
|
||||
private JLabel createPeriodLabel() {
|
||||
return createLabel("0", "Period label");
|
||||
}
|
||||
|
||||
private JLabel createDelaysLabel() {
|
||||
return createLabel("30", "Delays Label");
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createPeriodComboBox(final int periodInSeconds) {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{0, 5, 10, 20}));
|
||||
comboBox.setSelectedItem(periodInSeconds);
|
||||
comboBox.setName("set Period");
|
||||
comboBox.addActionListener(e -> periodLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createDelayComboBox(final int delayInSeconds) {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{30, 25, 15, 5}));
|
||||
comboBox.setSelectedItem(delayInSeconds);
|
||||
comboBox.setName("set Delay");
|
||||
comboBox.addActionListener(e -> delaysLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JTextField createTextField(final String reminderText) {
|
||||
final JTextField textField = new JTextField(20);
|
||||
textField.setName("Field");
|
||||
textField.setText(reminderText);
|
||||
return textField;
|
||||
}
|
||||
|
||||
private JButton createOkButton() {
|
||||
final JButton button = new JButton("ok");
|
||||
button.setName("OK");
|
||||
return button;
|
||||
}
|
||||
|
||||
private void updateReminder() {
|
||||
okButton.addActionListener(e -> this.dispose());
|
||||
okButton.addActionListener(e -> {
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
final int delayInSeconds = getTimeInSeconds(delay);
|
||||
final Reminder reminder = new Reminder(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
((DefaultListModel<Reminder>) reminderApplication.getReminders()).set(reminderIndex, reminder);
|
||||
});
|
||||
okButton.addActionListener(e -> scheduleReminder(textField, delay, period));
|
||||
}
|
||||
|
||||
private void scheduleReminder(final JTextField textField, final JComboBox<Integer> delay, final JComboBox<Integer> period) {
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
if (periodInSeconds == 0)
|
||||
scheduleNonRepeatedReminder(textField, delay);
|
||||
else
|
||||
scheduleRepeatedReminder(textField, delay, period);
|
||||
}
|
||||
|
||||
private void scheduleRepeatedReminder(final JTextField textField, final JComboBox<Integer> delay, final JComboBox<Integer> period) {
|
||||
final int delayInSeconds = getTimeInSeconds(delay);
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds), TimeUnit.SECONDS.toMillis(periodInSeconds));
|
||||
}
|
||||
|
||||
private void scheduleNonRepeatedReminder(final JTextField textField, final JComboBox<Integer> delay) {
|
||||
final int delayInSeconds = getTimeInSeconds(delay);
|
||||
final int periodInSeconds = 0;
|
||||
final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds));
|
||||
|
||||
}
|
||||
|
||||
private int getTimeInSeconds(final JComboBox<Integer> comboBox) {
|
||||
if (comboBox != null && comboBox.getSelectedItem() != null)
|
||||
return ((Integer) comboBox.getSelectedItem());
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
private TimerTask getTimerTask(final String reminderText, final Integer delayInSeconds, final Integer periodInSeconds) {
|
||||
return new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
new ReminderPopupFrame(reminderApplication, reminderText, delayInSeconds, periodInSeconds);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private JButton createCancelButton() {
|
||||
final JButton button = new JButton("cancel");
|
||||
button.setName("Cancel");
|
||||
button.addActionListener(e -> this.dispose());
|
||||
return button;
|
||||
}
|
||||
|
||||
private static JLabel createLabel(final String text, final String name) {
|
||||
JLabel label = new JLabel(text);
|
||||
label.setName(name);
|
||||
return label;
|
||||
}
|
||||
}
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
package reminderapplication;
|
||||
|
||||
public class Reminder {
|
||||
|
||||
private static String REMINDER_FORMAT = "Reminder Text: %s; Delay: %d; Period: %d;";
|
||||
|
||||
private final String name;
|
||||
private final int delay;
|
||||
private final int period;
|
||||
|
||||
public Reminder(final String name, final int delay, final int period) {
|
||||
this.name = name;
|
||||
this.delay = delay;
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getDelay() {
|
||||
return delay;
|
||||
}
|
||||
|
||||
public int getPeriod() {
|
||||
return period;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return REMINDER_FORMAT.formatted(name, delay, period);
|
||||
}
|
||||
}
|
||||
-186
@@ -1,186 +0,0 @@
|
||||
package reminderapplication;
|
||||
|
||||
import static reminderapplication.ConstraintsBuilder.*;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.HeadlessException;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.swing.DefaultComboBoxModel;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class ReminderFrame extends JFrame {
|
||||
|
||||
private static Timer TIMER = new Timer();
|
||||
private final TimeReminderApplication reminderApplication;
|
||||
private final JLabel reminderTextLabel;
|
||||
private final JLabel repeatPeriodLabel;
|
||||
private final JLabel setDelayLabel;
|
||||
private final JComboBox<Integer> delay;
|
||||
private final JComboBox<Integer> period;
|
||||
private final JButton cancelButton;
|
||||
private final JButton okButton;
|
||||
private final JTextField textField;
|
||||
private final JLabel delaysLabel;
|
||||
private final JLabel periodLabel;
|
||||
|
||||
public ReminderFrame(TimeReminderApplication reminderApp) throws HeadlessException {
|
||||
this.reminderApplication = reminderApp;
|
||||
textField = createTextField();
|
||||
delay = createDelayComboBox();
|
||||
period = createPeriodComboBox();
|
||||
cancelButton = createCancelButton();
|
||||
okButton = createOkButton();
|
||||
reminderTextLabel = createReminderTextLabel();
|
||||
repeatPeriodLabel = createRepeatPeriodLabel();
|
||||
setDelayLabel = createSetDelayLabel();
|
||||
delaysLabel = createDelaysLabel();
|
||||
periodLabel = createPeriodLabel();
|
||||
configureVisualRepresentation();
|
||||
configureActions();
|
||||
}
|
||||
|
||||
private void configureActions() {
|
||||
createNewReminder();
|
||||
}
|
||||
|
||||
private void configureVisualRepresentation() {
|
||||
configureFrame();
|
||||
setLocationRelativeTo(null);
|
||||
setLayout(new GridBagLayout());
|
||||
add(reminderTextLabel, constraint(0,0));
|
||||
add(repeatPeriodLabel, constraint(1,0));
|
||||
add(setDelayLabel, constraint(2,0));
|
||||
add(textField, constraint(0, 1));
|
||||
add(delay, constraint(1, 1));
|
||||
add(period, constraint(2, 1));
|
||||
add(delaysLabel, constraint(1,3));
|
||||
add(periodLabel, constraint(2,3));
|
||||
add(okButton, constraint(1, 4));
|
||||
add(cancelButton, constraint(2, 4));
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void configureFrame() {
|
||||
setTitle("Set Reminder");
|
||||
setName("Set Reminder");
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
}
|
||||
|
||||
private static JLabel createSetDelayLabel() {
|
||||
return createLabel("Set Delay", "Set Delay Label");
|
||||
}
|
||||
|
||||
private static JLabel createRepeatPeriodLabel() {
|
||||
return createLabel("Set Period", "Set Repeat Period Label");
|
||||
}
|
||||
|
||||
private static JLabel createReminderTextLabel() {
|
||||
return createLabel("Reminder Text", "Reminder Text Label");
|
||||
}
|
||||
|
||||
private JLabel createPeriodLabel() {
|
||||
return createLabel("0", "Period label");
|
||||
}
|
||||
|
||||
private JLabel createDelaysLabel() {
|
||||
return createLabel("30", "Delays Label");
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createPeriodComboBox() {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{0, 5, 10, 20}));
|
||||
comboBox.setName("set Period");
|
||||
comboBox.addActionListener(e -> periodLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createDelayComboBox() {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{30, 25, 15, 5}));
|
||||
comboBox.setName("set Delay");
|
||||
comboBox.addActionListener(e -> delaysLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JTextField createTextField() {
|
||||
final JTextField textField = new JTextField(20);
|
||||
textField.setName("Field");
|
||||
return textField;
|
||||
}
|
||||
|
||||
private JButton createOkButton() {
|
||||
final JButton button = new JButton("ok");
|
||||
button.setName("OK");
|
||||
return button;
|
||||
}
|
||||
|
||||
private void createNewReminder() {
|
||||
|
||||
okButton.addActionListener(e -> this.dispose());
|
||||
okButton.addActionListener(e -> {
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
final int delayInSeconds = getTimeInSeconds(delay);
|
||||
final Reminder reminder = new Reminder(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
((DefaultListModel<Reminder>) reminderApplication.getReminders()).addElement(reminder);
|
||||
});
|
||||
okButton.addActionListener(e -> scheduleReminder(textField, delay, period));
|
||||
}
|
||||
|
||||
private void scheduleReminder(final JTextField textField, final JComboBox<Integer> delay, final JComboBox<Integer> period) {
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
if (periodInSeconds == 0)
|
||||
scheduleNonRepeatedReminder(textField, delay);
|
||||
else
|
||||
scheduleRepeatedReminder(textField, delay, period);
|
||||
}
|
||||
|
||||
private void scheduleRepeatedReminder(final JTextField textField, final JComboBox<Integer> delay, final JComboBox<Integer> period) {
|
||||
final int delayInSeconds = getTimeInSeconds(delay) + 200;
|
||||
final int periodInSeconds = getTimeInSeconds(period);
|
||||
final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds), TimeUnit.SECONDS.toMillis(periodInSeconds));
|
||||
}
|
||||
|
||||
private void scheduleNonRepeatedReminder(final JTextField textField, final JComboBox<Integer> delay) {
|
||||
final int delayInSeconds = getTimeInSeconds(delay);
|
||||
final int periodInSeconds = 0;
|
||||
final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds);
|
||||
TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds));
|
||||
|
||||
}
|
||||
|
||||
private int getTimeInSeconds(final JComboBox<Integer> comboBox) {
|
||||
if (comboBox != null && comboBox.getSelectedItem() != null)
|
||||
return ((Integer) comboBox.getSelectedItem());
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
private TimerTask getTimerTask(final String reminderText, final Integer delayInSeconds, final Integer periodInSeconds) {
|
||||
return new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
new ReminderPopupFrame(reminderApplication, reminderText, delayInSeconds, periodInSeconds);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private JButton createCancelButton() {
|
||||
final JButton button = new JButton("cancel");
|
||||
button.setName("Cancel");
|
||||
button.addActionListener(e -> this.dispose());
|
||||
return button;
|
||||
}
|
||||
|
||||
private static JLabel createLabel(final String text, final String name) {
|
||||
JLabel label = new JLabel(text);
|
||||
label.setName(name);
|
||||
return label;
|
||||
}
|
||||
}
|
||||
-151
@@ -1,151 +0,0 @@
|
||||
package reminderapplication;
|
||||
|
||||
import static reminderapplication.ConstraintsBuilder.*;
|
||||
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.HeadlessException;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.swing.DefaultComboBoxModel;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class ReminderPopupFrame extends JFrame {
|
||||
|
||||
private static final Timer TIMER = new Timer();
|
||||
private final int AUTOMATIC_CLOSE_TIME_IN_SECONDS = 10;
|
||||
private final TimeReminderApplication reminderApplication;
|
||||
private final JLabel reminderTextLabel;
|
||||
private final JLabel repeatPeriodLabel;
|
||||
private final JLabel setDelayLabel;
|
||||
private final JComboBox<Integer> delay;
|
||||
private final JComboBox<Integer> period;
|
||||
private final JButton cancelButton;
|
||||
private final JButton okButton;
|
||||
private final JTextField textField;
|
||||
private final JLabel delaysLabel;
|
||||
private final JLabel periodLabel;
|
||||
|
||||
public ReminderPopupFrame(TimeReminderApplication reminderApp, final String text, final Integer delayInSeconds, final Integer periodInSeconds) throws HeadlessException {
|
||||
this.reminderApplication = reminderApp;
|
||||
textField = createTextField(text);
|
||||
delay = createDelayComboBox(delayInSeconds);
|
||||
period = createPeriodComboBox(periodInSeconds);
|
||||
cancelButton = createCancelButton();
|
||||
okButton = createDisabledOkButton();
|
||||
reminderTextLabel = createReminderTextLabel();
|
||||
repeatPeriodLabel = createRepeatPeriodLabel();
|
||||
setDelayLabel = createSetDelayLabel();
|
||||
delaysLabel = createDelaysLabel();
|
||||
periodLabel = createPeriodLabel();
|
||||
configureVisualRepresentation();
|
||||
configureActions();
|
||||
}
|
||||
|
||||
private void configureActions() {
|
||||
scheduleClosing();
|
||||
}
|
||||
|
||||
private void scheduleClosing() {
|
||||
final TimerTask timerTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
ReminderPopupFrame.this.dispose();
|
||||
}
|
||||
};
|
||||
TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(AUTOMATIC_CLOSE_TIME_IN_SECONDS));
|
||||
}
|
||||
|
||||
private void configureVisualRepresentation() {
|
||||
configureFrame();
|
||||
setLocationRelativeTo(null);
|
||||
setLayout(new GridBagLayout());
|
||||
add(reminderTextLabel, constraint(0,0));
|
||||
add(repeatPeriodLabel, constraint(1,0));
|
||||
add(setDelayLabel, constraint(2,0));
|
||||
add(textField, constraint(0, 1));
|
||||
add(delay, constraint(1, 1));
|
||||
add(period, constraint(2, 1));
|
||||
add(delaysLabel, constraint(1,3));
|
||||
add(periodLabel, constraint(2,3));
|
||||
add(okButton, constraint(1, 4));
|
||||
add(cancelButton, constraint(2, 4));
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void configureFrame() {
|
||||
setTitle("Set Reminder");
|
||||
setName("Set Reminder");
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
}
|
||||
|
||||
private static JLabel createSetDelayLabel() {
|
||||
return createLabel("Set Delay", "Set Delay Label");
|
||||
}
|
||||
|
||||
private static JLabel createRepeatPeriodLabel() {
|
||||
return createLabel("Set Period", "Set Repeat Period Label");
|
||||
}
|
||||
|
||||
private static JLabel createReminderTextLabel() {
|
||||
return createLabel("Reminder Text", "Reminder Text Label");
|
||||
}
|
||||
|
||||
private JLabel createPeriodLabel() {
|
||||
return createLabel("0", "Period label");
|
||||
}
|
||||
|
||||
private JLabel createDelaysLabel() {
|
||||
return createLabel("30", "Delays Label");
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createPeriodComboBox(final Integer periodInSeconds) {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{0, 5, 10, 20}));
|
||||
comboBox.setName("set Period");
|
||||
comboBox.setSelectedItem(periodInSeconds);
|
||||
comboBox.addActionListener(e -> periodLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JComboBox<Integer> createDelayComboBox(Integer delay) {
|
||||
final JComboBox<Integer> comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{30, 25, 15, 5}));
|
||||
comboBox.setSelectedItem(delay);
|
||||
comboBox.setName("set Delay");
|
||||
comboBox.addActionListener(e -> delaysLabel.setText(comboBox.getSelectedItem().toString()));
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
private JTextField createTextField(final String text) {
|
||||
final JTextField textField = new JTextField(20);
|
||||
textField.setName("Field");
|
||||
textField.setText(text);
|
||||
return textField;
|
||||
}
|
||||
|
||||
private JButton createDisabledOkButton() {
|
||||
final JButton button = new JButton("ok");
|
||||
button.setName("OK");
|
||||
button.setEnabled(false);
|
||||
return button;
|
||||
}
|
||||
|
||||
private JButton createCancelButton() {
|
||||
final JButton button = new JButton("cancel");
|
||||
button.setName("Cancel");
|
||||
button.addActionListener(e -> this.dispose());
|
||||
return button;
|
||||
}
|
||||
|
||||
private static JLabel createLabel(final String text, final String name) {
|
||||
JLabel label = new JLabel(text);
|
||||
label.setName(name);
|
||||
return label;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,7 +5,6 @@ This module contains articles about Java 11 core features
|
||||
### Relevant articles
|
||||
- [Guide To Java 8 Optional](https://www.baeldung.com/java-optional)
|
||||
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
|
||||
- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors)
|
||||
- [New Features in Java 11](https://www.baeldung.com/java-11-new-features)
|
||||
- [Getting the Java Version at Runtime](https://www.baeldung.com/get-java-version-runtime)
|
||||
- [Invoking a SOAP Web Service in Java](https://www.baeldung.com/java-soap-web-service)
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -21,30 +21,8 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>12</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>12</maven.compiler.target.version>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
-13
@@ -19,19 +19,6 @@ public class SwitchUnitTest {
|
||||
Assert.assertEquals(value, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void switchLocalVariable(){
|
||||
var month = Month.AUG;
|
||||
int i = switch (month){
|
||||
case JAN,JUN, JUL -> 3;
|
||||
case FEB,SEP, OCT, NOV, DEC -> 1;
|
||||
case MAR,MAY, APR, AUG -> {
|
||||
int j = month.toString().length() * 4;
|
||||
break j;
|
||||
}
|
||||
};
|
||||
Assert.assertEquals(12, i);
|
||||
}
|
||||
|
||||
enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}
|
||||
}
|
||||
|
||||
@@ -8,39 +8,13 @@
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
<release>13</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>13</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>13</maven.compiler.target.version>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
-1
@@ -7,7 +7,6 @@ import org.junit.Test;
|
||||
public class SwitchExpressionsWithYieldUnitTest {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("preview")
|
||||
public void whenSwitchingOnOperationSquareMe_thenWillReturnSquare() {
|
||||
var me = 4;
|
||||
var operation = "squareMe";
|
||||
|
||||
-2
@@ -8,7 +8,6 @@ public class TextBlocksUnitTest {
|
||||
|
||||
private static final String JSON_STRING = "{\r\n" + "\"name\" : \"Baeldung\",\r\n" + "\"website\" : \"https://www.%s.com/\"\r\n" + "}";
|
||||
|
||||
@SuppressWarnings("preview")
|
||||
private static final String TEXT_BLOCK_JSON = """
|
||||
{
|
||||
"name" : "Baeldung",
|
||||
@@ -25,7 +24,6 @@ public class TextBlocksUnitTest {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@Test
|
||||
public void whenTextBlocks_thenFormattedWorksAsFormat() {
|
||||
assertThat(TEXT_BLOCK_JSON.formatted("baeldung")
|
||||
|
||||
-4
@@ -13,7 +13,6 @@ import org.junit.Test;
|
||||
public class SwitchExpressionsUnitTest {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthJune_thenWillReturn3() {
|
||||
|
||||
var month = JUNE;
|
||||
@@ -29,7 +28,6 @@ public class SwitchExpressionsUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthAugust_thenWillReturn24() {
|
||||
var month = AUGUST;
|
||||
|
||||
@@ -47,7 +45,6 @@ public class SwitchExpressionsUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthJanuary_thenWillReturn3() {
|
||||
|
||||
Function<Month, Integer> func = (month) -> {
|
||||
@@ -61,7 +58,6 @@ public class SwitchExpressionsUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings ("preview")
|
||||
public void whenSwitchingOverMonthAugust_thenWillReturn2() {
|
||||
var month = AUGUST;
|
||||
|
||||
|
||||
@@ -48,7 +48,6 @@
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>14</maven.compiler.release>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -8,10 +8,9 @@
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -27,34 +26,8 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<release>${maven.compiler.release}</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
<source>14</source>
|
||||
<target>14</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>15</maven.compiler.release>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -4,4 +4,4 @@
|
||||
- [Guide to mapMulti in Stream API](https://www.baeldung.com/java-mapmulti)
|
||||
- [Collecting Stream Elements into a List in Java](https://www.baeldung.com/java-stream-to-list-collecting)
|
||||
- [New Features in Java 16](https://www.baeldung.com/java-16-new-features)
|
||||
- [Guide to Java 8 groupingBy Collector](https://www.baeldung.com/java-groupingby-collector)
|
||||
- [Value-Based Classes in Java](https://www.baeldung.com/java-value-based-classes)
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.value_based_class;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import jdk.internal.ValueBased;
|
||||
|
||||
/**
|
||||
* This class is written with the intention that it can serve as an example of
|
||||
* what a Value-based class could be.
|
||||
*/
|
||||
|
||||
@ValueBased
|
||||
public final class Point {
|
||||
private final int x;
|
||||
private final int y;
|
||||
private final int z;
|
||||
|
||||
private static Point ORIGIN = new Point(0, 0, 0);
|
||||
|
||||
private Point(int x, int y, int z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public static Point valueOfPoint(int x, int y, int z) {
|
||||
// returns a cached instance if it is origin, or a new instance
|
||||
if (isOrigin(x, y, z))
|
||||
return ORIGIN;
|
||||
return new Point(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Point{" + "x=" + x + ", y=" + y + ", z=" + z + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == null || getClass() != other.getClass())
|
||||
return false;
|
||||
Point point = (Point) other;
|
||||
return x == point.x && y == point.y && z == point.z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(x, y, z);
|
||||
}
|
||||
|
||||
private static boolean isOrigin(int x, int y, int z) {
|
||||
return x == 0 && y == 0 && z == 0;
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.value_based_class;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ValueBasedClassUnitTest {
|
||||
@Test
|
||||
public void givenAutoboxedAndPrimitive_whenCompared_thenReturnEquals() {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
list.add(1); // this is autoboxed
|
||||
Assert.assertEquals(list.get(0), Integer.valueOf(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValueBasedPoint_whenCreated_thenReturnsObjects() {
|
||||
Point p1 = Point.valueOfPoint(1, 2, 3);
|
||||
Point p2 = Point.valueOfPoint(2, 3, 4);
|
||||
|
||||
Assert.assertNotEquals(p1, p2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValueBasedPoint_whenCompared_thenReturnEquals() {
|
||||
Point p1 = Point.valueOfPoint(1, 2, 3);
|
||||
Point p2 = Point.valueOfPoint(1, 2, 3);
|
||||
|
||||
Assert.assertEquals(p1, p2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValueBasedPoint_whenOrigin_thenReturnCachedInstance() {
|
||||
Point p1 = Point.valueOfPoint(0, 0, 0);
|
||||
Point p2 = Point.valueOfPoint(0, 0, 0);
|
||||
Point p3 = Point.valueOfPoint(1, 2, 3);
|
||||
|
||||
// the following should not be assumed for value-based classes
|
||||
|
||||
Assert.assertTrue(p1 == p2);
|
||||
Assert.assertFalse(p1 == p3);
|
||||
}
|
||||
}
|
||||
@@ -7,3 +7,4 @@
|
||||
- [Random Number Generators in Java 17](https://www.baeldung.com/java-17-random-number-generators)
|
||||
- [Sealed Classes and Interfaces in Java](https://www.baeldung.com/java-sealed-classes-interfaces)
|
||||
- [Migrate From Java 8 to Java 17](https://www.baeldung.com/java-migrate-8-to-17)
|
||||
- [Format Multiple ‘or’ Conditions in an If Statement in Java](https://www.baeldung.com/java-multiple-or-conditions-if-statement)
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
package com.baeldung.multipleorwithif;
|
||||
|
||||
import static java.time.Month.DECEMBER;
|
||||
import static java.time.Month.NOVEMBER;
|
||||
import static java.time.Month.OCTOBER;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.in;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.Month;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MultipleOrWithIfUnitTest {
|
||||
|
||||
private final Random rand = new Random();
|
||||
|
||||
final Set<Month> months = Set.of(OCTOBER, NOVEMBER, DECEMBER);
|
||||
|
||||
@Test
|
||||
public void givenIfStatement_whenMultipleOrOperator_thenAssert() {
|
||||
assertTrue(multipleOrOperatorIf(monthIn()));
|
||||
assertFalse(multipleOrOperatorIf(monthNotIn()));
|
||||
}
|
||||
|
||||
boolean multipleOrOperatorIf(Month month) {
|
||||
if (month == OCTOBER || month == NOVEMBER || month == DECEMBER) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSwitch_whenMultipleCase_thenBreakAndAssert() {
|
||||
assertTrue(switchMonth(monthIn()));
|
||||
assertFalse(switchMonth(monthNotIn()));
|
||||
}
|
||||
|
||||
boolean switchMonth(Month month) {
|
||||
return switch (month) {
|
||||
case OCTOBER, NOVEMBER, DECEMBER -> true;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllowedValuesList_whenContains_thenAssert() {
|
||||
assertTrue(contains(monthIn()));
|
||||
assertFalse(contains(monthNotIn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPredicates_whenTestMultipleOr_thenAssert() {
|
||||
assertTrue(predicateWithIf(monthIn()));
|
||||
assertFalse(predicateWithIf(monthNotIn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInputList_whenFilterWithPredicate_thenAssert() {
|
||||
|
||||
List<Month> list = List.of(monthIn(), monthIn(), monthNotIn());
|
||||
|
||||
list.stream()
|
||||
.filter(this::predicateWithIf)
|
||||
.forEach(m -> assertThat(m, is(in(months))));
|
||||
}
|
||||
|
||||
Predicate<Month> orPredicate() {
|
||||
Predicate<Month> predicate = x -> x == OCTOBER;
|
||||
Predicate<Month> predicate1 = x -> x == NOVEMBER;
|
||||
Predicate<Month> predicate2 = x -> x == DECEMBER;
|
||||
|
||||
return predicate.or(predicate1)
|
||||
.or(predicate2);
|
||||
}
|
||||
|
||||
boolean predicateWithIf(Month month) {
|
||||
if (orPredicate().test(month)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenContainsInSetPredicate_whenTestPredicate_thenAssert() {
|
||||
Predicate<Month> collectionPredicate = this::contains;
|
||||
|
||||
assertTrue(collectionPredicate.test(monthIn()));
|
||||
assertFalse(collectionPredicate.test(monthNotIn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInputList_whenFilterWithContains_thenAssert() {
|
||||
|
||||
List<Month> monthList = List.of(monthIn(), monthIn(), monthNotIn());
|
||||
|
||||
monthList.stream()
|
||||
.filter(this::contains)
|
||||
.forEach(m -> assertThat(m, is(in(months))));
|
||||
}
|
||||
|
||||
private boolean contains(Month month) {
|
||||
if (months.contains(month)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Month monthIn() {
|
||||
return Month.of(rand.ints(10, 13)
|
||||
.findFirst()
|
||||
.orElse(10));
|
||||
}
|
||||
|
||||
private Month monthNotIn() {
|
||||
return Month.of(rand.ints(1, 10)
|
||||
.findFirst()
|
||||
.orElse(1));
|
||||
}
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package com.baeldung.recordproperties;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.RecordComponent;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
record Player(String name, int age, Long score) {
|
||||
}
|
||||
|
||||
public class ReadRecordPropertiesByReflectionUnitTest {
|
||||
private static final Player ERIC = new Player("Eric", 28, 4242L);
|
||||
|
||||
@Test
|
||||
void whenUsingRecordComponent_thenGetExpectedResult() {
|
||||
var fields = new ArrayList<Field>();
|
||||
RecordComponent[] components = Player.class.getRecordComponents();
|
||||
for (var comp : components) {
|
||||
try {
|
||||
Field field = ERIC.getClass()
|
||||
.getDeclaredField(comp.getName());
|
||||
field.setAccessible(true);
|
||||
fields.add(field);
|
||||
} catch (NoSuchFieldException e) {
|
||||
// for simplicity, error handling is skipped
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(3, fields.size());
|
||||
|
||||
var nameField = fields.get(0);
|
||||
var ageField = fields.get(1);
|
||||
var scoreField = fields.get(2);
|
||||
try {
|
||||
assertEquals("name", nameField.getName());
|
||||
assertEquals(String.class, nameField.getType());
|
||||
assertEquals("Eric", nameField.get(ERIC));
|
||||
|
||||
assertEquals("age", ageField.getName());
|
||||
assertEquals(int.class, ageField.getType());
|
||||
assertEquals(28, ageField.get(ERIC));
|
||||
|
||||
assertEquals("score", scoreField.getName());
|
||||
assertEquals(Long.class, scoreField.getType());
|
||||
assertEquals(4242L, scoreField.get(ERIC));
|
||||
} catch (IllegalAccessException exception) {
|
||||
// for simplicity, error handling is skipped
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingClassGetDeclaredField_thenGetExpectedResult() {
|
||||
// record has no public fields
|
||||
assertEquals(0, Player.class.getFields().length);
|
||||
|
||||
var fields = new ArrayList<Field>();
|
||||
for (var field : Player.class.getDeclaredFields()) {
|
||||
field.setAccessible(true);
|
||||
fields.add(field);
|
||||
}
|
||||
|
||||
assertEquals(3, fields.size());
|
||||
var nameField = fields.get(0);
|
||||
var ageField = fields.get(1);
|
||||
var scoreField = fields.get(2);
|
||||
|
||||
try {
|
||||
assertEquals("name", nameField.getName());
|
||||
assertEquals(String.class, nameField.getType());
|
||||
assertEquals("Eric", nameField.get(ERIC));
|
||||
|
||||
assertEquals("age", ageField.getName());
|
||||
assertEquals(int.class, ageField.getType());
|
||||
assertEquals(28, ageField.get(ERIC));
|
||||
|
||||
assertEquals("score", scoreField.getName());
|
||||
assertEquals(Long.class, scoreField.getType());
|
||||
assertEquals(4242L, scoreField.get(ERIC));
|
||||
} catch (IllegalAccessException ex) {
|
||||
// for simplicity, error handling is skipped
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-18</artifactId>
|
||||
<name>core-java-18</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<release>${maven.compiler.release}</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<forkCount>1</forkCount>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-api</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>18</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>18</maven.compiler.target.version>
|
||||
<maven.compiler.release>18</maven.compiler.release>
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FinalizationExamples {
|
||||
FileInputStream fis = null;
|
||||
|
||||
public void readFileOperationWithFinalization() throws IOException {
|
||||
try {
|
||||
fis = new FileInputStream("input.txt");
|
||||
// perform operation on the file
|
||||
System.out.println(fis.readAllBytes().length);
|
||||
|
||||
} finally {
|
||||
if (fis != null)
|
||||
fis.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void readFileOperationWithTryWith() throws IOException {
|
||||
try (FileInputStream fis = new FileInputStream("input.txt")) {
|
||||
// perform operations
|
||||
System.out.println(fis.readAllBytes().length);
|
||||
}
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.lang.ref.Cleaner;
|
||||
|
||||
public class MyCleanerResourceClass implements AutoCloseable {
|
||||
private static Resource resource;
|
||||
|
||||
private static final Cleaner cleaner = Cleaner.create();
|
||||
private final Cleaner.Cleanable cleanable;
|
||||
|
||||
public MyCleanerResourceClass() {
|
||||
resource = new Resource();
|
||||
this.cleanable = cleaner.register(this, new CleaningState());
|
||||
}
|
||||
|
||||
public void useResource() {
|
||||
// using the resource here
|
||||
resource.use();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// perform actions to close all underlying resources
|
||||
this.cleanable.clean();
|
||||
}
|
||||
|
||||
static class CleaningState implements Runnable {
|
||||
CleaningState() {
|
||||
// constructor
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// some cleanup action
|
||||
System.out.println("Cleanup done");
|
||||
}
|
||||
}
|
||||
|
||||
static class Resource {
|
||||
void use() {
|
||||
System.out.println("Using the resource");
|
||||
}
|
||||
|
||||
void close() {
|
||||
System.out.println("Cleanup done");
|
||||
}
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MyCloseableResourceClass implements AutoCloseable {
|
||||
|
||||
private final FileInputStream fis;
|
||||
|
||||
public MyCloseableResourceClass() throws FileNotFoundException {
|
||||
this.fis = new FileInputStream("src/main/resources/file.txt");
|
||||
|
||||
}
|
||||
|
||||
public int getByteLength() throws IOException {
|
||||
System.out.println("Some operation");
|
||||
return this.fis.readAllBytes().length;
|
||||
}
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
System.out.println("Finalized object");
|
||||
this.fis.close();
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MyFinalizableResourceClass {
|
||||
private FileInputStream fis;
|
||||
|
||||
public MyFinalizableResourceClass() throws FileNotFoundException {
|
||||
this.fis = new FileInputStream("src/main/resources/file.txt");
|
||||
}
|
||||
|
||||
public int getByteLength() throws IOException {
|
||||
System.out.println("Some operation");
|
||||
return this.fis.readAllBytes().length;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
System.out.println("Finalized object");
|
||||
this.fis.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
This is a test file.
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.finalization_closeable_cleaner;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FinalizationCloseableCleanerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMyFinalizationResource_whenUsingFinalize_thenShouldClean() {
|
||||
assertDoesNotThrow(() -> {
|
||||
MyFinalizableResourceClass mfr = new MyFinalizableResourceClass();
|
||||
mfr.getByteLength();
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenMyCleanerResource_whenUsingCleanerAPI_thenShouldClean() {
|
||||
assertDoesNotThrow(() -> {
|
||||
try (MyCleanerResourceClass myCleanerResourceClass = new MyCleanerResourceClass()) {
|
||||
myCleanerResourceClass.useResource();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCloseableResource_whenUsingTryWith_thenShouldClose() throws IOException {
|
||||
int length = 0;
|
||||
try (MyCloseableResourceClass mcr = new MyCloseableResourceClass()) {
|
||||
length = mcr.getByteLength();
|
||||
}
|
||||
Assert.assertEquals(20, length);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
## Relevant Articles
|
||||
- [Sequenced Collections in Java 21](https://www.baeldung.com/java-21-sequenced-collections)
|
||||
- [String Templates in Java 21](https://www.baeldung.com/java-21-string-templates)
|
||||
- [Unnamed Classes and Instance Main Methods in Java 21](https://www.baeldung.com/java-21-unnamed-class-instance-main)
|
||||
- [Unnamed Patterns and Variables in Java 21](https://www.baeldung.com/java-unnamed-patterns-variables)
|
||||
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-21</artifactId>
|
||||
<name>core-java-21</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>21</source>
|
||||
<target>21</target>
|
||||
<!-- Needed due to a bug with JDK 21, described here: https://issues.apache.org/jira/browse/MCOMPILER-546?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel&focusedCommentId=17767513 -->
|
||||
<debug>false</debug>
|
||||
<compilerArgs>
|
||||
<arg>--enable-preview</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>21</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>21</maven.compiler.target.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user