Skip to content

Commit 6d36470

Browse files
author
Dassi Orleando
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents 235ea3e + d45a891 commit 6d36470

12 files changed

Lines changed: 394 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11

22
The "REST with Spring" Classes
33
==============================
4+
45
After 5 months of work, here's the Master Class of REST With Spring: <br/>
56
**[>> THE REST WITH SPRING MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
67

8+
And here's the Master Class of Learn Spring Security: <br/>
9+
**[>> LEARN SPRING SECURITY MASTER CLASS](http://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)**
10+
11+
712

813
Spring Tutorials
914
================
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.concurrent.waitandnotify;
2+
3+
public class Data {
4+
private String packet;
5+
6+
// True if receiver should wait
7+
// False if sender should wait
8+
private boolean transfer = true;
9+
10+
public synchronized String receive() {
11+
while (transfer) {
12+
try {
13+
wait();
14+
} catch (InterruptedException e) {}
15+
}
16+
transfer = true;
17+
18+
notifyAll();
19+
return packet;
20+
}
21+
22+
public synchronized void send(String packet) {
23+
while (!transfer) {
24+
try {
25+
wait();
26+
} catch (InterruptedException e) {}
27+
}
28+
transfer = false;
29+
30+
this.packet = packet;
31+
notifyAll();
32+
}
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.concurrent.waitandnotify;
2+
3+
public class NetworkDriver {
4+
public static void main(String[] args) {
5+
Data data = new Data();
6+
Thread sender = new Thread(new Sender(data));
7+
Thread receiver = new Thread(new Receiver(data));
8+
9+
sender.start();
10+
receiver.start();
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.concurrent.waitandnotify;
2+
3+
import java.util.concurrent.ThreadLocalRandom;
4+
5+
public class Receiver implements Runnable {
6+
private Data load;
7+
8+
public Receiver(Data load) {
9+
this.load = load;
10+
}
11+
12+
public void run() {
13+
for(String receivedMessage = load.receive();
14+
!"End".equals(receivedMessage) ;
15+
receivedMessage = load.receive()) {
16+
17+
System.out.println(receivedMessage);
18+
19+
//Thread.sleep() to mimic heavy server-side processing
20+
try {
21+
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
22+
} catch (InterruptedException e) {}
23+
}
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.concurrent.waitandnotify;
2+
3+
import java.util.concurrent.ThreadLocalRandom;
4+
5+
public class Sender implements Runnable {
6+
private Data data;
7+
8+
public Sender(Data data) {
9+
this.data = data;
10+
}
11+
12+
public void run() {
13+
String packets[] = {
14+
"First packet",
15+
"Second packet",
16+
"Third packet",
17+
"Fourth packet",
18+
"End"
19+
};
20+
21+
for (String packet : packets) {
22+
data.send(packet);
23+
24+
//Thread.sleep() to mimic heavy server-side processing
25+
try {
26+
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
27+
} catch (InterruptedException e) {}
28+
}
29+
}
30+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.concurrent.waitandnotify;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.PrintStream;
7+
import java.io.PrintWriter;
8+
import java.io.StringWriter;
9+
10+
import org.junit.After;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
public class NetworkIntegrationTest {
15+
16+
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
17+
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
18+
private String expected;
19+
20+
@Before
21+
public void setUpStreams() {
22+
System.setOut(new PrintStream(outContent));
23+
System.setErr(new PrintStream(errContent));
24+
}
25+
26+
@Before
27+
public void setUpExpectedOutput() {
28+
StringWriter expectedStringWriter = new StringWriter();
29+
30+
PrintWriter printWriter = new PrintWriter(expectedStringWriter);
31+
printWriter.println("First packet");
32+
printWriter.println("Second packet");
33+
printWriter.println("Third packet");
34+
printWriter.println("Fourth packet");
35+
printWriter.close();
36+
37+
expected = expectedStringWriter.toString();
38+
}
39+
40+
@After
41+
public void cleanUpStreams() {
42+
System.setOut(null);
43+
System.setErr(null);
44+
}
45+
46+
@Test
47+
public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() {
48+
Data data = new Data();
49+
Thread sender = new Thread(new Sender(data));
50+
Thread receiver = new Thread(new Receiver(data));
51+
52+
sender.start();
53+
receiver.start();
54+
55+
//wait for sender and receiver to finish before we test against expected
56+
try {
57+
sender.join();
58+
receiver.join();
59+
} catch (InterruptedException e) {
60+
e.printStackTrace();
61+
}
62+
63+
assertEquals(expected, outContent.toString());
64+
}
65+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.polymorphism;
2+
3+
import java.awt.image.BufferedImage;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
public class FileManager {
9+
10+
final static Logger logger = LoggerFactory.getLogger(FileManager.class);
11+
12+
public static void main(String[] args) {
13+
GenericFile file1 = new TextFile("SampleTextFile", "This is a sample text content", "v1.0.0");
14+
logger.info("File Info: \n" + file1.getFileInfo() + "\n");
15+
ImageFile imageFile = new ImageFile("SampleImageFile", 200, 100, new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB).toString()
16+
.getBytes(), "v1.0.0");
17+
logger.info("File Info: \n" + imageFile.getFileInfo());
18+
}
19+
20+
public static ImageFile createImageFile(String name, int height, int width, byte[] content, String version) {
21+
ImageFile imageFile = new ImageFile(name, height, width, content, version);
22+
logger.info("File 2 Info: \n" + imageFile.getFileInfo());
23+
return imageFile;
24+
}
25+
26+
public static GenericFile createTextFile(String name, String content, String version) {
27+
GenericFile file1 = new TextFile(name, content, version);
28+
logger.info("File 1 Info: \n" + file1.getFileInfo() + "\n");
29+
return file1;
30+
}
31+
32+
public static TextFile createTextFile2(String name, String content, String version) {
33+
TextFile file1 = new TextFile(name, content, version);
34+
logger.info("File 1 Info: \n" + file1.getFileInfo() + "\n");
35+
return file1;
36+
}
37+
38+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.polymorphism;
2+
3+
import java.util.Date;
4+
5+
public class GenericFile {
6+
private String name;
7+
private String extension;
8+
private Date dateCreated;
9+
private String version;
10+
private byte[] content;
11+
12+
public GenericFile() {
13+
this.setDateCreated(new Date());
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getExtension() {
25+
return extension;
26+
}
27+
28+
public void setExtension(String extension) {
29+
this.extension = extension;
30+
}
31+
32+
public Date getDateCreated() {
33+
return dateCreated;
34+
}
35+
36+
public void setDateCreated(Date dateCreated) {
37+
this.dateCreated = dateCreated;
38+
}
39+
40+
public String getVersion() {
41+
return version;
42+
}
43+
44+
public void setVersion(String version) {
45+
this.version = version;
46+
}
47+
48+
public byte[] getContent() {
49+
return content;
50+
}
51+
52+
public void setContent(byte[] content) {
53+
this.content = content;
54+
}
55+
56+
public String getFileInfo() {
57+
return "File Name: " + this.getName() + "\n" + "Extension: " + this.getExtension() + "\n" + "Date Created: " + this.getDateCreated() + "\n" + "Version: " + this.getVersion() + "\n";
58+
}
59+
60+
public Object read() {
61+
return content;
62+
}
63+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.polymorphism;
2+
3+
public class ImageFile extends GenericFile {
4+
private int height;
5+
private int width;
6+
7+
public ImageFile(String name, int height, int width, byte[] content, String version) {
8+
this.setHeight(height);
9+
this.setWidth(width);
10+
this.setContent(content);
11+
this.setName(name);
12+
this.setVersion(version);
13+
this.setExtension(".jpg");
14+
}
15+
16+
public int getHeight() {
17+
return height;
18+
}
19+
20+
public void setHeight(int height) {
21+
this.height = height;
22+
}
23+
24+
public int getWidth() {
25+
return width;
26+
}
27+
28+
public void setWidth(int width) {
29+
this.width = width;
30+
}
31+
32+
public String getFileInfo() {
33+
return super.getFileInfo() + "Height: " + this.getHeight() + "\n" + "Width: " + this.getWidth();
34+
}
35+
36+
public String read() {
37+
return this.getContent()
38+
.toString();
39+
}
40+
41+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.polymorphism;
2+
3+
public class TextFile extends GenericFile {
4+
private int wordCount;
5+
6+
public TextFile(String name, String content, String version) {
7+
String[] words = content.split(" ");
8+
this.setWordCount(words.length > 0 ? words.length : 1);
9+
this.setContent(content.getBytes());
10+
this.setName(name);
11+
this.setVersion(version);
12+
this.setExtension(".txt");
13+
}
14+
15+
public int getWordCount() {
16+
return wordCount;
17+
}
18+
19+
public void setWordCount(int wordCount) {
20+
this.wordCount = wordCount;
21+
}
22+
23+
public String getFileInfo() {
24+
return super.getFileInfo() + "Word Count: " + wordCount;
25+
}
26+
27+
public String read() {
28+
return this.getContent()
29+
.toString();
30+
}
31+
32+
public String read(int limit) {
33+
return this.getContent()
34+
.toString()
35+
.substring(0, limit);
36+
}
37+
38+
public String read(int start, int stop) {
39+
return this.getContent()
40+
.toString()
41+
.substring(start, stop);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)