Skip to content

Commit 99bad7d

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents 1053180 + dc3589a commit 99bad7d

19 files changed

Lines changed: 519 additions & 23 deletions

File tree

‎core-java-9/src/main/java/com/baeldung/java9/process/ServiceMain.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class ServiceMain {
66

77
public static void main(String[] args) throws InterruptedException {
88
ProcessHandle thisProcess = ProcessHandle.current();
9-
long pid = thisProcess.getPid();
9+
long pid = thisProcess.pid();
1010

1111
Optional<String[]> opArgs = Optional.ofNullable(args);
1212
String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command"));

‎core-java-9/src/test/java/com/baeldung/java9/language/Java9ObjectsAPIUnitTest.java‎

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import static org.hamcrest.MatcherAssert.assertThat;
1111

1212
public class Java9ObjectsAPIUnitTest {
13-
13+
1414
@Test
1515
public void givenNullObject_whenRequireNonNullElse_thenElse(){
1616
assertThat(Objects.<List>requireNonNullElse(null, Collections.EMPTY_LIST),
@@ -23,6 +23,11 @@ public void givenObject_whenRequireNonNullElse_thenObject(){
2323
Collections.EMPTY_LIST), is(List.of("item1", "item2")));
2424
}
2525

26+
@Test(expected = NullPointerException.class)
27+
public void givenNull_whenRequireNonNullElse_thenException(){
28+
Objects.<List>requireNonNullElse(null, null);
29+
}
30+
2631
@Test
2732
public void givenObject_whenRequireNonNullElseGet_thenObject(){
2833
assertThat(Objects.<List>requireNonNullElseGet(null, List::of),
@@ -32,40 +37,39 @@ public void givenObject_whenRequireNonNullElseGet_thenObject(){
3237
@Test
3338
public void givenNumber_whenInvokeCheckIndex_thenNumber(){
3439
int length = 5;
35-
3640
assertThat(Objects.checkIndex(4, length), is(4));
41+
}
3742

38-
try{
39-
Objects.checkIndex(5, length);
40-
}catch(Exception ex){
41-
assertThat(ex, instanceOf(IndexOutOfBoundsException.class));
42-
}
43+
@Test(expected = IndexOutOfBoundsException.class)
44+
public void givenOutOfRangeNumber_whenInvokeCheckIndex_thenException(){
45+
int length = 5;
46+
Objects.checkIndex(5, length);
4347
}
4448

49+
4550
@Test
4651
public void givenSubRange_whenCheckFromToIndex_thenNumber(){
4752
int length = 6;
48-
4953
assertThat(Objects.checkFromToIndex(2,length,length), is(2));
54+
}
5055

51-
try{
52-
Objects.checkFromToIndex(2,7,length);
53-
}catch(Exception ex){
54-
assertThat(ex, instanceOf(IndexOutOfBoundsException.class));
55-
}
56+
@Test(expected = IndexOutOfBoundsException.class)
57+
public void givenInvalidSubRange_whenCheckFromToIndex_thenException(){
58+
int length = 6;
59+
Objects.checkFromToIndex(2,7,length);
5660
}
5761

62+
5863
@Test
59-
public void giveSubRange_whenCheckFromIndexSize_thenNumber(){
64+
public void givenSubRange_whenCheckFromIndexSize_thenNumber(){
6065
int length = 6;
66+
assertThat(Objects.checkFromIndexSize(2,3,length), is(2));
67+
}
6168

62-
assertThat(Objects.checkFromToIndex(2,5,length), is(2));
63-
64-
try{
65-
Objects.checkFromToIndex(2,6,length);
66-
}catch(Exception ex){
67-
assertThat(ex, instanceOf(IndexOutOfBoundsException.class));
68-
}
69+
@Test(expected = IndexOutOfBoundsException.class)
70+
public void givenInvalidSubRange_whenCheckFromIndexSize_thenException(){
71+
int length = 6;
72+
Objects.checkFromIndexSize(2, 6, length);
6973
}
7074

7175

‎libraries/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
- [Introduction to Neuroph](http://www.baeldung.com/intro-to-neuroph)
3030
- [Guide to Apache Commons CircularFifoQueue](http://www.baeldung.com/commons-circular-fifo-queue)
3131
- [Quick Guide to RSS with Rome](http://www.baeldung.com/rome-rss)
32+
- [Introduction to NoException](http://www.baeldung.com/intrduction-to-noexception)
33+
3234

3335
The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own.
3436

‎libraries/pom.xml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@
462462
<artifactId>pcollections</artifactId>
463463
<version>${pcollections.version}</version>
464464
</dependency>
465+
<dependency>
466+
<groupId>com.machinezoo.noexception</groupId>
467+
<artifactId>noexception</artifactId>
468+
<version>1.1.0</version>
469+
</dependency>
465470
<dependency>
466471
<groupId>org.eclipse.collections</groupId>
467472
<artifactId>eclipse-collections</artifactId>
@@ -510,4 +515,4 @@
510515
<rome.version>1.0</rome.version>
511516
<eclipse-collections.version>8.2.0</eclipse-collections.version>
512517
</properties>
513-
</project>
518+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.noexception;
2+
3+
import com.machinezoo.noexception.ExceptionHandler;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
public class CustomExceptionHandler extends ExceptionHandler {
8+
9+
private Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);
10+
11+
@Override
12+
public boolean handle(Throwable throwable) {
13+
14+
if (throwable.getClass()
15+
.isAssignableFrom(RuntimeException.class)
16+
|| throwable.getClass()
17+
.isAssignableFrom(Error.class)) {
18+
return false;
19+
} else {
20+
logger.error("Caught Exception ", throwable);
21+
return true;
22+
}
23+
}
24+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.noexception;
2+
3+
import com.machinezoo.noexception.Exceptions;
4+
import org.junit.Test;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
public class NoExceptionUnitTest {
9+
10+
private static Logger logger = LoggerFactory.getLogger(NoExceptionUnitTest.class);
11+
12+
@Test
13+
public void whenStdExceptionHandling_thenCatchAndLog() {
14+
try {
15+
System.out.println("Result is " + Integer.parseInt("foobar"));
16+
} catch (Throwable exception) {
17+
logger.error("Caught exception:", exception);
18+
}
19+
}
20+
21+
@Test
22+
public void whenDefaultNoException_thenCatchAndLog() {
23+
Exceptions.log().run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
24+
}
25+
26+
@Test
27+
public void givenLogger_whenDefaultNoException_thenCatchAndLogWithClassName() {
28+
Exceptions.log(logger).run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
29+
}
30+
31+
@Test
32+
public void givenLoggerAndMessage_whenDefaultNoException_thenCatchAndLogWithMessage() {
33+
Exceptions.log(logger, "Something went wrong:").run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
34+
}
35+
36+
@Test
37+
public void givenDefaultValue_whenDefaultNoException_thenCatchAndLogPrintDefault() {
38+
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> Integer.parseInt("foobar")).orElse(-1));
39+
}
40+
41+
@Test(expected = Error.class)
42+
public void givenCustomHandler_whenError_thenRethrowError() {
43+
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
44+
customExceptionHandler.run(() -> throwError());
45+
}
46+
47+
@Test
48+
public void givenCustomHandler_whenException_thenCatchAndLog() {
49+
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
50+
customExceptionHandler.run(() -> throwException());
51+
}
52+
53+
private static void throwError() {
54+
throw new Error("This is very bad.");
55+
}
56+
57+
private static void throwException() {
58+
String testString = "foo";
59+
testString.charAt(5);
60+
}
61+
62+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2006-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.baeldung.spring_batch_intro.partitioner;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import org.springframework.batch.core.partition.support.Partitioner;
23+
import org.springframework.batch.item.ExecutionContext;
24+
import org.springframework.core.io.Resource;
25+
import org.springframework.util.Assert;
26+
27+
public class CustomMultiResourcePartitioner implements Partitioner {
28+
29+
private static final String DEFAULT_KEY_NAME = "fileName";
30+
31+
private static final String PARTITION_KEY = "partition";
32+
33+
private Resource[] resources = new Resource[0];
34+
35+
private String keyName = DEFAULT_KEY_NAME;
36+
37+
/**
38+
* The resources to assign to each partition. In Spring configuration you
39+
* can use a pattern to select multiple resources.
40+
* @param resources the resources to use
41+
*/
42+
public void setResources(Resource[] resources) {
43+
this.resources = resources;
44+
}
45+
46+
/**
47+
* The name of the key for the file name in each {@link ExecutionContext}.
48+
* Defaults to "fileName".
49+
* @param keyName the value of the key
50+
*/
51+
public void setKeyName(String keyName) {
52+
this.keyName = keyName;
53+
}
54+
55+
/**
56+
* Assign the filename of each of the injected resources to an
57+
* {@link ExecutionContext}.
58+
*
59+
* @see Partitioner#partition(int)
60+
*/
61+
@Override
62+
public Map<String, ExecutionContext> partition(int gridSize) {
63+
Map<String, ExecutionContext> map = new HashMap<String, ExecutionContext>(gridSize);
64+
int i = 0, k = 1;
65+
for (Resource resource : resources) {
66+
ExecutionContext context = new ExecutionContext();
67+
Assert.state(resource.exists(), "Resource does not exist: " + resource);
68+
context.putString(keyName, resource.getFilename());
69+
context.putString("opFileName", "output" + k++ + ".xml");
70+
71+
map.put(PARTITION_KEY + i, context);
72+
i++;
73+
}
74+
return map;
75+
}
76+
77+
}

0 commit comments

Comments
 (0)