Skip to content

Commit dcc7fa6

Browse files
kirangonellaadamd1985
authored andcommitted
Updated for BAEL 971 (eugenp#2702)
* Updated Lang3Utils.java Added a new test for ConcurrentException and updated other exception with Lambda * LazyInitializer sample files * Updated file with LazyInitializer Unit Test * Updated BuidlerMethods class to include LazyInitializer & BackgroundInitializer * Updated Lang3UtilsTest class * Updated Lang3UtilsTest class
1 parent 9aa221a commit dcc7fa6

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.apache.commons.lang3.builder.EqualsBuilder;
44
import org.apache.commons.lang3.builder.HashCodeBuilder;
55
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
import org.apache.commons.lang3.concurrent.ConcurrentException;
7+
import org.apache.commons.lang3.concurrent.BackgroundInitializer;
68

79
public class BuilderMethods {
810

@@ -56,5 +58,36 @@ public static void main(final String[] arguments) {
5658
System.out.println(simple1.getName());
5759
System.out.println(simple1.hashCode());
5860
System.out.println(simple1.toString());
61+
62+
SampleLazyInitializer sampleLazyInitializer = new SampleLazyInitializer();
63+
64+
try {
65+
sampleLazyInitializer.get();
66+
} catch (ConcurrentException e1) {
67+
// TODO Auto-generated catch block
68+
e1.printStackTrace();
69+
}
70+
71+
SampleBackgroundInitializer sampleBackgroundInitializer = new SampleBackgroundInitializer();
72+
sampleBackgroundInitializer.start();
73+
74+
// Proceed with other tasks instead of waiting for the SampleBackgroundInitializer task to finish.
75+
76+
try {
77+
Object result = sampleBackgroundInitializer.get();
78+
} catch (ConcurrentException e) {
79+
e.printStackTrace();
80+
}
81+
}
82+
}
83+
84+
class SampleBackgroundInitializer extends BackgroundInitializer<String>{
85+
86+
@Override
87+
protected String initialize() throws Exception {
88+
return null;
5989
}
90+
91+
// Any complex task that takes some time
92+
6093
}

libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import static org.junit.Assert.assertArrayEquals;
44
import static org.junit.Assert.assertEquals;
55
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertNotNull;
67
import static org.junit.Assert.assertNotSame;
8+
import static org.junit.Assert.assertNull;
79
import static org.junit.Assert.assertSame;
810
import static org.junit.Assert.assertTrue;
911
import static org.junit.Assert.fail;
@@ -20,6 +22,7 @@
2022
import org.apache.commons.lang3.BooleanUtils;
2123
import org.apache.commons.lang3.SystemUtils;
2224
import org.apache.commons.lang3.arch.Processor;
25+
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
2326
import org.apache.commons.lang3.concurrent.ConcurrentException;
2427
import org.apache.commons.lang3.concurrent.ConcurrentRuntimeException;
2528
import org.apache.commons.lang3.concurrent.ConcurrentUtils;
@@ -133,4 +136,14 @@ public void testLazyInitializer() throws Exception {
133136
assertEquals(sampleObjectOne, sampleObjectTwo);
134137
}
135138

139+
@Test
140+
public void testBuildDefaults() {
141+
BasicThreadFactory.Builder builder = new BasicThreadFactory.Builder();
142+
BasicThreadFactory factory = builder.build();
143+
assertNull("No naming pattern set Yet", factory.getNamingPattern());
144+
BasicThreadFactory factory2 = builder.namingPattern("sampleNamingPattern").daemon(true).priority(Thread.MIN_PRIORITY).build();
145+
assertNotNull("Got a naming pattern", factory2.getNamingPattern());
146+
assertEquals("sampleNamingPattern", factory2.getNamingPattern());
147+
148+
}
136149
}

0 commit comments

Comments
 (0)