Skip to content

Commit 1fca4ae

Browse files
Jose CarvajalJose Carvajal
authored andcommitted
Merge remote-tracking branch 'upstream/master'
2 parents 2a922a2 + 2ad2d4d commit 1fca4ae

64 files changed

Lines changed: 963 additions & 132 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎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+
}

‎core-java/src/main/java/com/baeldung/nestedclass/Enclosing.java‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎core-java/src/main/java/com/baeldung/nestedclass/Outer.java‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎core-java/src/main/java/com/baeldung/nestedclass/SimpleAbstractClass.java‎

Lines changed: 0 additions & 5 deletions
This file was deleted.
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+
}

0 commit comments

Comments
 (0)