From dcc7fa65dd314dad2fd68c3fd245ea3f1aecc915 Mon Sep 17 00:00:00 2001 From: Kiran Date: Wed, 4 Oct 2017 02:59:54 +0530 Subject: [PATCH] Updated for BAEL 971 (#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 --- .../commons/lang3/BuilderMethods.java | 33 +++++++++++++++++++ .../commons/lang3/Lang3UtilsTest.java | 13 ++++++++ 2 files changed, 46 insertions(+) diff --git a/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java b/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java index c64f7e7511..35cae7426d 100644 --- a/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java +++ b/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java @@ -3,6 +3,8 @@ package com.baeldung.commons.lang3; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.concurrent.ConcurrentException; +import org.apache.commons.lang3.concurrent.BackgroundInitializer; public class BuilderMethods { @@ -56,5 +58,36 @@ public class BuilderMethods { System.out.println(simple1.getName()); System.out.println(simple1.hashCode()); System.out.println(simple1.toString()); + + SampleLazyInitializer sampleLazyInitializer = new SampleLazyInitializer(); + + try { + sampleLazyInitializer.get(); + } catch (ConcurrentException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + SampleBackgroundInitializer sampleBackgroundInitializer = new SampleBackgroundInitializer(); + sampleBackgroundInitializer.start(); + + // Proceed with other tasks instead of waiting for the SampleBackgroundInitializer task to finish. + + try { + Object result = sampleBackgroundInitializer.get(); + } catch (ConcurrentException e) { + e.printStackTrace(); + } } } + +class SampleBackgroundInitializer extends BackgroundInitializer{ + + @Override + protected String initialize() throws Exception { + return null; + } + + // Any complex task that takes some time + +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java index af70ccecc7..29bcebeb2b 100644 --- a/libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java +++ b/libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java @@ -3,7 +3,9 @@ package com.baeldung.commons.lang3; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -20,6 +22,7 @@ import org.apache.commons.lang3.ArchUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.arch.Processor; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; import org.apache.commons.lang3.concurrent.ConcurrentException; import org.apache.commons.lang3.concurrent.ConcurrentRuntimeException; import org.apache.commons.lang3.concurrent.ConcurrentUtils; @@ -133,4 +136,14 @@ public class Lang3UtilsTest { assertEquals(sampleObjectOne, sampleObjectTwo); } + @Test + public void testBuildDefaults() { + BasicThreadFactory.Builder builder = new BasicThreadFactory.Builder(); + BasicThreadFactory factory = builder.build(); + assertNull("No naming pattern set Yet", factory.getNamingPattern()); + BasicThreadFactory factory2 = builder.namingPattern("sampleNamingPattern").daemon(true).priority(Thread.MIN_PRIORITY).build(); + assertNotNull("Got a naming pattern", factory2.getNamingPattern()); + assertEquals("sampleNamingPattern", factory2.getNamingPattern()); + + } }