Skip to content

Commit 237ea1b

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents 1d42f7c + 00f5648 commit 237ea1b

79 files changed

Lines changed: 1931 additions & 429 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.

‎apache-zookeeper/pom.xml‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>apache-zookeeper</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.apache.zookeeper</groupId>
12+
<artifactId>zookeeper</artifactId>
13+
<version>3.3.2</version>
14+
<exclusions>
15+
<exclusion>
16+
<groupId>com.sun.jmx</groupId>
17+
<artifactId>jmxri</artifactId>
18+
</exclusion>
19+
<exclusion>
20+
<groupId>com.sun.jdmk</groupId>
21+
<artifactId>jmxtools</artifactId>
22+
</exclusion>
23+
<exclusion>
24+
<groupId>javax.jms</groupId>
25+
<artifactId>jms</artifactId>
26+
</exclusion>
27+
</exclusions>
28+
</dependency>
29+
</dependencies>
30+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.zookeeper.connection;
2+
3+
import java.io.IOException;
4+
import java.util.concurrent.CountDownLatch;
5+
6+
import org.apache.zookeeper.WatchedEvent;
7+
import org.apache.zookeeper.Watcher;
8+
import org.apache.zookeeper.Watcher.Event.KeeperState;
9+
import org.apache.zookeeper.ZooKeeper;
10+
11+
public class ZKConnection {
12+
private ZooKeeper zoo;
13+
final CountDownLatch connectionLatch = new CountDownLatch(1);
14+
15+
public ZKConnection() {
16+
}
17+
18+
public ZooKeeper connect(String host) throws IOException, InterruptedException {
19+
zoo = new ZooKeeper(host, 2000, new Watcher() {
20+
public void process(WatchedEvent we) {
21+
if (we.getState() == KeeperState.SyncConnected) {
22+
connectionLatch.countDown();
23+
}
24+
}
25+
});
26+
connectionLatch.await();
27+
return zoo;
28+
}
29+
30+
public void close() throws InterruptedException {
31+
zoo.close();
32+
}
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.zookeeper.manager;
2+
3+
import org.apache.zookeeper.KeeperException;
4+
5+
public interface ZKManager {
6+
/**
7+
* Create a Znode and save some data
8+
*
9+
* @param path
10+
* @param data
11+
* @throws KeeperException
12+
* @throws InterruptedException
13+
*/
14+
public void create(String path, byte[] data) throws KeeperException, InterruptedException;
15+
16+
/**
17+
* Get ZNode Data
18+
*
19+
* @param path
20+
* @param boolean watchFlag
21+
* @throws KeeperException
22+
* @throws InterruptedException
23+
*/
24+
public Object getZNodeData(String path, boolean watchFlag);
25+
26+
/**
27+
* Update the ZNode Data
28+
*
29+
* @param path
30+
* @param data
31+
* @throws KeeperException
32+
* @throws InterruptedException
33+
*/
34+
public void update(String path, byte[] data) throws KeeperException, InterruptedException, KeeperException;
35+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.zookeeper.manager;
2+
3+
import org.apache.zookeeper.CreateMode;
4+
import org.apache.zookeeper.KeeperException;
5+
import org.apache.zookeeper.ZooDefs;
6+
import org.apache.zookeeper.ZooKeeper;
7+
8+
import com.baeldung.zookeeper.connection.ZKConnection;
9+
10+
public class ZKManagerImpl implements ZKManager {
11+
private static ZooKeeper zkeeper;
12+
private static ZKConnection zkConnection;
13+
14+
public ZKManagerImpl() {
15+
initialize();
16+
}
17+
18+
/** * Initialize connection */
19+
private void initialize() {
20+
try {
21+
zkConnection = new ZKConnection();
22+
zkeeper = zkConnection.connect("localhost");
23+
} catch (Exception e) {
24+
System.out.println(e.getMessage());
25+
}
26+
}
27+
28+
public void closeConnection() {
29+
try {
30+
zkConnection.close();
31+
} catch (InterruptedException e) {
32+
System.out.println(e.getMessage());
33+
}
34+
}
35+
36+
public void create(String path, byte[] data) throws KeeperException, InterruptedException {
37+
zkeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
38+
}
39+
40+
public Object getZNodeData(String path, boolean watchFlag) {
41+
try {
42+
byte[] b = null;
43+
b = zkeeper.getData(path, null, null);
44+
String data = new String(b, "UTF-8");
45+
System.out.println(data);
46+
return data;
47+
} catch (Exception e) {
48+
System.out.println(e.getMessage());
49+
}
50+
return null;
51+
}
52+
53+
public void update(String path, byte[] data) throws KeeperException, InterruptedException {
54+
int version = zkeeper.exists(path, true)
55+
.getVersion();
56+
zkeeper.setData(path, data, version);
57+
}
58+
}

‎core-groovy/build.gradle‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
group 'com.baeldung'
2+
version '1.0-SNAPSHOT'
3+
4+
apply plugin: 'groovy'
5+
6+
repositories {
7+
mavenCentral()
8+
}
9+
10+
dependencies {
11+
compile 'org.codehaus.groovy:groovy-all:2.5.0-alpha-1'
12+
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
13+
}

‎core-groovy/pom.xml‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<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">
43
<modelVersion>4.0.0</modelVersion>
54

65
<artifactId>core-groovy</artifactId>
@@ -24,12 +23,17 @@
2423
<dependency>
2524
<groupId>org.codehaus.groovy</groupId>
2625
<artifactId>groovy</artifactId>
27-
<version>2.4.13</version>
26+
<version>2.5.0-alpha-1</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.codehaus.groovy</groupId>
30+
<artifactId>groovy-all</artifactId>
31+
<version>2.5.0-alpha-1</version>
2832
</dependency>
2933
<dependency>
3034
<groupId>org.codehaus.groovy</groupId>
3135
<artifactId>groovy-sql</artifactId>
32-
<version>2.4.13</version>
36+
<version>2.5.0-alpha-1</version>
3337
</dependency>
3438
<dependency>
3539
<groupId>org.junit.jupiter</groupId>
@@ -49,6 +53,12 @@
4953
<version>2.4.0</version>
5054
<scope>test</scope>
5155
</dependency>
56+
<dependency>
57+
<groupId>org.spockframework</groupId>
58+
<artifactId>spock-core</artifactId>
59+
<version>1.1-groovy-2.4</version>
60+
<scope>test</scope>
61+
</dependency>
5262
</dependencies>
5363

5464
<build>
@@ -114,5 +124,5 @@
114124
<junit.vintage.version>4.12.0</junit.vintage.version>
115125
<junit4.version>4.12</junit4.version>
116126
</properties>
117-
118127
</project>
128+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.json
2+
3+
class Account {
4+
String id
5+
BigDecimal value
6+
Date createdAt
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.json
2+
3+
import groovy.json.JsonGenerator
4+
import groovy.json.JsonOutput
5+
import groovy.json.JsonParserType
6+
import groovy.json.JsonSlurper
7+
8+
class JsonParser {
9+
10+
Account toObject(String json) {
11+
JsonSlurper jsonSlurper = new JsonSlurper()
12+
jsonSlurper.parseText(json) as Account
13+
}
14+
15+
Account toObjectWithIndexOverlay(String json) {
16+
JsonSlurper jsonSlurper = new JsonSlurper(type: JsonParserType.INDEX_OVERLAY)
17+
jsonSlurper.parseText(json) as Account
18+
}
19+
20+
String toJson(Account account) {
21+
JsonOutput.toJson(account)
22+
}
23+
24+
String toJson(Account account, String dateFormat, String... fieldsToExclude) {
25+
JsonGenerator generator = new JsonGenerator.Options()
26+
.dateFormat(dateFormat)
27+
.excludeFieldsByName(fieldsToExclude)
28+
.build()
29+
generator.toJson(account)
30+
}
31+
32+
String prettyfy(String json) {
33+
JsonOutput.prettyPrint(json)
34+
}
35+
36+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.json
2+
3+
import spock.lang.Specification
4+
5+
import java.text.SimpleDateFormat
6+
7+
class JsonParserTest extends Specification {
8+
9+
JsonParser jsonParser
10+
11+
void setup () {
12+
jsonParser = new JsonParser()
13+
}
14+
15+
def 'Should parse to Account given Json String' () {
16+
given:
17+
def json = '{"id":"1234","value":15.6}'
18+
when:
19+
def account = jsonParser.toObject(json)
20+
then:
21+
account
22+
account instanceof Account
23+
account.id == '1234'
24+
account.value == 15.6
25+
}
26+
27+
def 'Should parse to Account given Json String with date property' () {
28+
given:
29+
def json = '{"id":"1234","value":15.6,"createdAt":"2018-01-01T00:00:00+0000"}'
30+
when:
31+
def account = jsonParser.toObjectWithIndexOverlay(json)
32+
then:
33+
account
34+
account instanceof Account
35+
account.id == '1234'
36+
account.value == 15.6
37+
println account.createdAt
38+
account.createdAt == Date.parse('yyyy-MM-dd', '2018-01-01')
39+
}
40+
41+
def 'Should parse to Json given an Account object' () {
42+
given:
43+
Account account = new Account(
44+
id: '123',
45+
value: 15.6,
46+
createdAt: new SimpleDateFormat('MM/dd/yyyy').parse('01/01/2018')
47+
)
48+
when:
49+
def json = jsonParser.toJson(account)
50+
then:
51+
json
52+
json == '{"value":15.6,"createdAt":"2018-01-01T00:00:00+0000","id":"123"}'
53+
}
54+
55+
def 'Should parse to Json given an Account object, a date format and fields to exclude' () {
56+
given:
57+
Account account = new Account(
58+
id: '123',
59+
value: 15.6,
60+
createdAt: new SimpleDateFormat('MM/dd/yyyy').parse('01/01/2018')
61+
)
62+
when:
63+
def json = jsonParser.toJson(account, 'MM/dd/yyyy', 'value')
64+
then:
65+
json
66+
json == '{"createdAt":"01/01/2018","id":"123"}'
67+
}
68+
69+
def 'Should prettify given a json string' () {
70+
given:
71+
String json = '{"value":15.6,"createdAt":"01/01/2018","id":"123456"}'
72+
when:
73+
def jsonPretty = jsonParser.prettyfy(json)
74+
then:
75+
jsonPretty
76+
jsonPretty == '{\n "value": 15.6,\n "createdAt": "01/01/2018",\n "id": "123456"\n}'
77+
}
78+
79+
80+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.java9.methodhandles;
2+
3+
public class Book {
4+
5+
String id;
6+
String title;
7+
8+
public Book(String id, String title) {
9+
this.id = id;
10+
this.title = title;
11+
}
12+
13+
@SuppressWarnings("unused")
14+
private String formatBook() {
15+
return id + " > " + title;
16+
}
17+
}

0 commit comments

Comments
 (0)