Skip to content

Commit 7e6cb22

Browse files
Nam Thai NguyenNam Thai Nguyen
authored andcommitted
Merge branch 'master' into BAEL-1479
2 parents 30321eb + 6923d3a commit 7e6cb22

31 files changed

Lines changed: 1046 additions & 187 deletions

File tree

‎core-java-concurrency/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
- [Daemon Threads in Java](http://www.baeldung.com/java-daemon-thread)
3434
- [Implementing a Runnable vs Extending a Thread](http://www.baeldung.com/java-runnable-vs-extending-thread)
3535
- [How to Kill a Java Thread](http://www.baeldung.com/java-thread-stop)
36-
- [How to Wait for Threads to Finish in the ExecutorService](http://www.baeldung.com/java-executor-wait-for-threads)
36+
- [ExecutorService - Waiting for Threads to Finish](http://www.baeldung.com/java-executor-wait-for-threads)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.trie;
2+
3+
public class Trie {
4+
private TrieNode root;
5+
6+
Trie() {
7+
root = new TrieNode();
8+
}
9+
10+
public void insert(String word) {
11+
TrieNode current = root;
12+
13+
for (int i = 0; i < word.length(); i++) {
14+
current = current.getChildren().computeIfAbsent(word.charAt(i), c -> new TrieNode());
15+
}
16+
current.setEndOfWord(true);
17+
}
18+
19+
public boolean delete(String word) {
20+
return delete(root, word, 0);
21+
}
22+
23+
public boolean containsNode(String word) {
24+
TrieNode current = root;
25+
26+
for (int i = 0; i < word.length(); i++) {
27+
char ch = word.charAt(i);
28+
TrieNode node = current.getChildren().get(ch);
29+
if (node == null) {
30+
return false;
31+
}
32+
current = node;
33+
}
34+
return current.isEndOfWord();
35+
}
36+
37+
public boolean isEmpty() {
38+
return root == null;
39+
}
40+
41+
private boolean delete(TrieNode current, String word, int index) {
42+
if (index == word.length()) {
43+
if (!current.isEndOfWord()) {
44+
return false;
45+
}
46+
current.setEndOfWord(false);
47+
return current.getChildren().isEmpty();
48+
}
49+
char ch = word.charAt(index);
50+
TrieNode node = current.getChildren().get(ch);
51+
if (node == null) {
52+
return false;
53+
}
54+
boolean shouldDeleteCurrentNode = delete(node, word, index + 1);
55+
56+
if (shouldDeleteCurrentNode) {
57+
current.getChildren().remove(ch);
58+
return current.getChildren().isEmpty();
59+
}
60+
return false;
61+
}
62+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.trie;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
class TrieNode {
7+
private Map<Character, TrieNode> children;
8+
private boolean endOfWord;
9+
10+
public TrieNode() {
11+
children = new HashMap<>();
12+
endOfWord = false;
13+
}
14+
15+
public Map<Character, TrieNode> getChildren() {
16+
return children;
17+
}
18+
19+
public void setChildren(Map<Character, TrieNode> children) {
20+
this.children = children;
21+
}
22+
23+
public boolean isEndOfWord() {
24+
return endOfWord;
25+
}
26+
27+
public void setEndOfWord(boolean endOfWord) {
28+
this.endOfWord = endOfWord;
29+
}
30+
31+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.trie;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertTrue;
7+
8+
public class TrieTest {
9+
10+
@Test
11+
public void whenEmptyTrie_thenNoElements() {
12+
Trie trie = new Trie();
13+
14+
assertFalse(trie.isEmpty());
15+
}
16+
17+
@Test
18+
public void givenATrie_whenAddingElements_thenTrieNotEmpty() {
19+
Trie trie = createExampleTrie();
20+
21+
assertFalse(trie.isEmpty());
22+
}
23+
24+
@Test
25+
public void givenATrie_whenAddingElements_thenTrieHasThoseElements() {
26+
Trie trie = createExampleTrie();
27+
28+
assertFalse(trie.containsNode("3"));
29+
assertFalse(trie.containsNode("vida"));
30+
31+
assertTrue(trie.containsNode("Programming"));
32+
assertTrue(trie.containsNode("is"));
33+
assertTrue(trie.containsNode("a"));
34+
assertTrue(trie.containsNode("way"));
35+
assertTrue(trie.containsNode("of"));
36+
assertTrue(trie.containsNode("life"));
37+
}
38+
39+
@Test
40+
public void givenATrie_whenLookingForNonExistingElement_thenReturnsFalse() {
41+
Trie trie = createExampleTrie();
42+
43+
assertFalse(trie.containsNode("99"));
44+
}
45+
46+
@Test
47+
public void givenATrie_whenDeletingElements_thenTreeDoesNotContainThoseElements() {
48+
49+
Trie trie = createExampleTrie();
50+
51+
assertTrue(trie.containsNode("Programming"));
52+
trie.delete("Programming");
53+
assertFalse(trie.containsNode("Programming"));
54+
}
55+
56+
private Trie createExampleTrie() {
57+
Trie trie = new Trie();
58+
59+
trie.insert("Programming");
60+
trie.insert("is");
61+
trie.insert("a");
62+
trie.insert("way");
63+
trie.insert("of");
64+
trie.insert("life");
65+
66+
return trie;
67+
}
68+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.netty;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelInboundHandlerAdapter;
5+
6+
import java.util.logging.Logger;
7+
8+
public class ChannelHandlerA extends ChannelInboundHandlerAdapter {
9+
10+
private Logger logger = Logger.getLogger(getClass().getName());
11+
12+
@Override
13+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
14+
throw new Exception("Ooops");
15+
}
16+
17+
@Override
18+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
19+
logger.info("Exception Occurred in ChannelHandler A");
20+
ctx.fireExceptionCaught(cause);
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.netty;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelInboundHandlerAdapter;
5+
6+
import java.util.logging.Logger;
7+
8+
9+
public class ChannelHandlerB extends ChannelInboundHandlerAdapter {
10+
11+
private Logger logger = Logger.getLogger(getClass().getName());
12+
13+
@Override
14+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
15+
logger.info("Exception Handled in ChannelHandler B");
16+
logger.info(cause.getLocalizedMessage());
17+
//do more exception handling
18+
ctx.close();
19+
}
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.netty;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelInitializer;
6+
import io.netty.channel.ChannelOption;
7+
import io.netty.channel.EventLoopGroup;
8+
import io.netty.channel.nio.NioEventLoopGroup;
9+
import io.netty.channel.socket.SocketChannel;
10+
import io.netty.channel.socket.nio.NioServerSocketChannel;
11+
12+
public class NettyServerB {
13+
14+
private int port;
15+
16+
private NettyServerB(int port) {
17+
this.port = port;
18+
}
19+
20+
private void run() throws Exception {
21+
22+
EventLoopGroup bossGroup = new NioEventLoopGroup();
23+
EventLoopGroup workerGroup = new NioEventLoopGroup();
24+
25+
try {
26+
ServerBootstrap b = new ServerBootstrap();
27+
b.group(bossGroup, workerGroup)
28+
.channel(NioServerSocketChannel.class)
29+
.childHandler(new ChannelInitializer<SocketChannel>() {
30+
public void initChannel(SocketChannel ch) throws Exception {
31+
ch.pipeline().addLast(new ChannelHandlerA(), new ChannelHandlerB());
32+
}
33+
})
34+
.option(ChannelOption.SO_BACKLOG, 128)
35+
.childOption(ChannelOption.SO_KEEPALIVE, true);
36+
ChannelFuture f = b.bind(port).sync(); // (7)
37+
f.channel().closeFuture().sync();
38+
} finally {
39+
workerGroup.shutdownGracefully();
40+
bossGroup.shutdownGracefully();
41+
}
42+
}
43+
44+
public static void main(String[] args) throws Exception {
45+
new NettyServerB(8080).run();
46+
}
47+
}

‎logging-modules/log4j2/pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@
5959

6060

6161
<properties>
62-
<jackson.version>2.8.5</jackson.version>
62+
<jackson.version>2.9.3</jackson.version>
6363
<h2.version>1.4.193</h2.version>
6464
<commons-dbcp2.version>2.1.1</commons-dbcp2.version>
65-
<log4j-core.version>2.7</log4j-core.version>
65+
<log4j-core.version>2.10.0</log4j-core.version>
6666
</properties>
6767

6868
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.logging.log4j2.tests;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.IOException;
7+
import java.io.PrintStream;
8+
9+
import org.apache.logging.log4j.LogManager;
10+
import org.apache.logging.log4j.Logger;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
import com.fasterxml.jackson.databind.ObjectMapper;
15+
16+
public class JSONLayoutTest {
17+
18+
private static Logger logger;
19+
private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream();
20+
private PrintStream ps = new PrintStream(consoleOutput);
21+
22+
@Before
23+
public void setUp() {
24+
// Redirect console output to our stream
25+
System.setOut(ps);
26+
logger = LogManager.getLogger("CONSOLE_JSON_APPENDER");
27+
}
28+
29+
@Test
30+
public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() {
31+
logger.debug("Debug message");
32+
String currentLog = consoleOutput.toString();
33+
assertTrue(!currentLog.isEmpty() && isValidJSON(currentLog));
34+
}
35+
36+
public static boolean isValidJSON(String jsonInString) {
37+
try {
38+
final ObjectMapper mapper = new ObjectMapper();
39+
mapper.readTree(jsonInString);
40+
return true;
41+
} catch (IOException e) {
42+
return false;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)