Skip to content

Commit 99c70af

Browse files
Java 11 migraiton: model-view-presenter
1 parent edcb520 commit 99c70af

File tree

7 files changed

+34
-58
lines changed

7 files changed

+34
-58
lines changed

model-view-presenter/src/main/java/com/iluwatar/model/view/presenter/App.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public class App {
4444
* @param args command line args
4545
*/
4646
public static void main(String[] args) {
47-
FileLoader loader = new FileLoader();
48-
FileSelectorJFrame frame = new FileSelectorJFrame();
49-
FileSelectorPresenter presenter = new FileSelectorPresenter(frame);
47+
var loader = new FileLoader();
48+
var frame = new FileSelectorJFrame();
49+
var presenter = new FileSelectorPresenter(frame);
5050
presenter.setLoader(loader);
5151
presenter.start();
5252
}

model-view-presenter/src/main/java/com/iluwatar/model/view/presenter/FileLoader.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.File;
2828
import java.io.FileReader;
2929
import java.io.Serializable;
30+
import java.util.stream.Collectors;
3031
import org.slf4j.Logger;
3132
import org.slf4j.LoggerFactory;
3233

@@ -59,18 +60,11 @@ public class FileLoader implements Serializable {
5960
* Loads the data of the file specified.
6061
*/
6162
public String loadData() {
62-
String dataFileName = this.fileName;
63-
try (BufferedReader br = new BufferedReader(new FileReader(new File(dataFileName)))) {
64-
StringBuilder sb = new StringBuilder();
65-
String line;
66-
67-
while ((line = br.readLine()) != null) {
68-
sb.append(line).append('\n');
69-
}
70-
63+
var dataFileName = this.fileName;
64+
try (var br = new BufferedReader(new FileReader(new File(dataFileName)))) {
65+
var result = br.lines().collect(Collectors.joining("\n"));
7166
this.loaded = true;
72-
73-
return sb.toString();
67+
return result;
7468
} catch (Exception e) {
7569
LOGGER.error("File {} does not exist", dataFileName);
7670
}

model-view-presenter/src/main/java/com/iluwatar/model/view/presenter/FileSelectorJFrame.java

+15-30
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,22 @@ public class FileSelectorJFrame extends JFrame implements FileSelectorView, Acti
4848
/**
4949
* The "OK" button for loading the file.
5050
*/
51-
private JButton ok;
51+
private final JButton ok;
5252

5353
/**
5454
* The cancel button.
5555
*/
56-
private JButton cancel;
57-
58-
/**
59-
* The information label.
60-
*/
61-
private JLabel info;
62-
63-
/**
64-
* The contents label.
65-
*/
66-
private JLabel contents;
56+
private final JButton cancel;
6757

6858
/**
6959
* The text field for giving the name of the file that we want to open.
7060
*/
71-
private JTextField input;
61+
private final JTextField input;
7262

7363
/**
7464
* A text area that will keep the contents of the file opened.
7565
*/
76-
private JTextArea area;
77-
78-
/**
79-
* The panel that will hold our widgets.
80-
*/
81-
private JPanel panel;
66+
private final JTextArea area;
8267

8368
/**
8469
* The Presenter component that the frame will interact with.
@@ -102,7 +87,7 @@ public FileSelectorJFrame() {
10287
/*
10388
* Add the panel.
10489
*/
105-
this.panel = new JPanel();
90+
var panel = new JPanel();
10691
panel.setLayout(null);
10792
this.add(panel);
10893
panel.setBounds(0, 0, 500, 200);
@@ -111,48 +96,48 @@ public FileSelectorJFrame() {
11196
/*
11297
* Add the info label.
11398
*/
114-
this.info = new JLabel("File Name :");
115-
this.panel.add(info);
99+
var info = new JLabel("File Name :");
100+
panel.add(info);
116101
info.setBounds(30, 10, 100, 30);
117102

118103
/*
119104
* Add the contents label.
120105
*/
121-
this.contents = new JLabel("File contents :");
122-
this.panel.add(contents);
123-
this.contents.setBounds(30, 100, 120, 30);
106+
var contents = new JLabel("File contents :");
107+
panel.add(contents);
108+
contents.setBounds(30, 100, 120, 30);
124109

125110
/*
126111
* Add the text field.
127112
*/
128113
this.input = new JTextField(100);
129-
this.panel.add(input);
114+
panel.add(input);
130115
this.input.setBounds(150, 15, 200, 20);
131116

132117
/*
133118
* Add the text area.
134119
*/
135120
this.area = new JTextArea(100, 100);
136-
JScrollPane pane = new JScrollPane(area);
121+
var pane = new JScrollPane(area);
137122
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
138123
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
139-
this.panel.add(pane);
124+
panel.add(pane);
140125
this.area.setEditable(false);
141126
pane.setBounds(150, 100, 250, 80);
142127

143128
/*
144129
* Add the OK button.
145130
*/
146131
this.ok = new JButton("OK");
147-
this.panel.add(ok);
132+
panel.add(ok);
148133
this.ok.setBounds(250, 50, 100, 25);
149134
this.ok.addActionListener(this);
150135

151136
/*
152137
* Add the cancel button.
153138
*/
154139
this.cancel = new JButton("Cancel");
155-
this.panel.add(this.cancel);
140+
panel.add(this.cancel);
156141
this.cancel.setBounds(380, 50, 100, 25);
157142
this.cancel.addActionListener(this);
158143

model-view-presenter/src/main/java/com/iluwatar/model/view/presenter/FileSelectorPresenter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class FileSelectorPresenter implements Serializable {
4141
/**
4242
* The View component that the presenter interacts with.
4343
*/
44-
private FileSelectorView view;
44+
private final FileSelectorView view;
4545

4646
/**
4747
* The Model component that the presenter interacts with.
@@ -91,7 +91,7 @@ public void confirmed() {
9191
}
9292

9393
if (loader.fileExists()) {
94-
String data = loader.loadData();
94+
var data = loader.loadData();
9595
view.displayData(data);
9696
} else {
9797
view.showMessage("The file specified does not exist.");

model-view-presenter/src/test/java/com/iluwatar/model/view/presenter/AppTest.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@
2626
import org.junit.jupiter.api.Test;
2727

2828
/**
29-
*
3029
* Application test
31-
*
3230
*/
3331
public class AppTest {
3432

3533
@Test
3634
public void test() {
37-
String[] args = {};
38-
App.main(args);
35+
App.main(new String[]{});
3936
}
4037

4138
}

model-view-presenter/src/test/java/com/iluwatar/model/view/presenter/FileLoaderTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323

2424
package com.iluwatar.model.view.presenter;
2525

26-
import org.junit.jupiter.api.Test;
27-
2826
import static org.junit.jupiter.api.Assertions.assertNull;
2927

28+
import org.junit.jupiter.api.Test;
29+
3030
/**
3131
* Date: 12/21/15 - 12:12 PM
3232
*
@@ -35,8 +35,8 @@
3535
public class FileLoaderTest {
3636

3737
@Test
38-
public void testLoadData() throws Exception {
39-
final FileLoader fileLoader = new FileLoader();
38+
public void testLoadData() {
39+
final var fileLoader = new FileLoader();
4040
fileLoader.setFileName("non-existing-file");
4141
assertNull(fileLoader.loadData());
4242
}

model-view-presenter/src/test/java/com/iluwatar/model/view/presenter/FileSelectorPresenterTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323

2424
package com.iluwatar.model.view.presenter;
2525

26-
import org.junit.jupiter.api.BeforeEach;
27-
import org.junit.jupiter.api.Test;
28-
2926
import static org.junit.jupiter.api.Assertions.assertEquals;
3027
import static org.junit.jupiter.api.Assertions.assertFalse;
3128
import static org.junit.jupiter.api.Assertions.assertNotNull;
3229
import static org.junit.jupiter.api.Assertions.assertTrue;
3330

31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Test;
33+
3434
/**
3535
* This test case is responsible for testing our application by taking advantage of the
3636
* Model-View-Controller architectural pattern.
@@ -79,7 +79,7 @@ public void wiring() {
7979
*/
8080
@Test
8181
public void updateFileNameToLoader() {
82-
String expectedFile = "Stamatis";
82+
var expectedFile = "Stamatis";
8383
stub.setFileName(expectedFile);
8484

8585
presenter.start();

0 commit comments

Comments
 (0)