From 8180274da26ec9e7d4049d0d6ce62c7497c940fc Mon Sep 17 00:00:00 2001 From: Dassi orleando Date: Fri, 25 Aug 2017 18:39:12 +0100 Subject: [PATCH 1/9] BAEL-1074 Difference between two dates in Java (#2493) * Different types of bean injection in Spring * Difference between two dates in java * Update README.md * Simple clean of difference between dates * Clean my test article --- core-java/README.md | 3 + core-java/pom.xml | 14 ++++- .../java/com/baeldung/DateDiffUnitTest.java | 63 +++++++++++++++++++ java-difference-date/README.md | 5 ++ java-difference-date/pom.xml | 58 +++++++++++++++++ .../test/java/com/baeldung/DateDiffTest.java | 61 ++++++++++++++++++ 6 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 core-java/src/test/java/com/baeldung/DateDiffUnitTest.java create mode 100644 java-difference-date/README.md create mode 100644 java-difference-date/pom.xml create mode 100644 java-difference-date/src/test/java/com/baeldung/DateDiffTest.java diff --git a/core-java/README.md b/core-java/README.md index dabf6f39be..a4b5127e98 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -141,3 +141,6 @@ - [Guide to UUID in Java](http://www.baeldung.com/java-uuid) - [How to Get the Last Element of a Stream in Java?](http://www.baeldung.com/java-stream-last-element) - [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char) +- [Period and Duration in Java](http://www.baeldung.com/java-period-duration) +- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro) +- [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api) diff --git a/core-java/pom.xml b/core-java/pom.xml index 422965a0ed..586486027a 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -186,6 +186,16 @@ fscontext ${fscontext.version} + + joda-time + joda-time + ${joda-time.version} + + + com.darwinsys + hirondelle-date4j + ${hirondelle-date4j.version} + @@ -408,6 +418,8 @@ 1.8.7 1.16.12 4.6-b01 + 2.9.9 + 1.5.1 1.3 @@ -416,9 +428,9 @@ 3.6.1 1.7.0 + 3.6.0 2.19.1 - diff --git a/core-java/src/test/java/com/baeldung/DateDiffUnitTest.java b/core-java/src/test/java/com/baeldung/DateDiffUnitTest.java new file mode 100644 index 0000000000..40b7fac30d --- /dev/null +++ b/core-java/src/test/java/com/baeldung/DateDiffUnitTest.java @@ -0,0 +1,63 @@ +package com.baeldung; + +import org.joda.time.DateTime; +import org.junit.Test; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.Duration; +import java.time.ZonedDateTime; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; + +public class DateDiffUnitTest { + + @Test + public void givenTwoDatesBeforeJava8_whenDifferentiating_thenWeGetSix() throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH); + Date firstDate = sdf.parse("06/24/2017"); + Date secondDate = sdf.parse("06/30/2017"); + + long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime()); + long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS); + + assertEquals(diff, 6); + } + + @Test + public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() { + ZonedDateTime now = ZonedDateTime.now(); + ZonedDateTime sixDaysBehind = now.minusDays(6); + + Duration duration = Duration.between(now, sixDaysBehind); + long diff = Math.abs(duration.toDays()); + + assertEquals(diff, 6); + } + + @Test + public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() { + DateTime now = DateTime.now(); + DateTime sixDaysBehind = now.minusDays(6); + + org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind); + long diff = Math.abs(duration.getStandardDays()); + + assertEquals(diff, 6); + } + + @Test + public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() { + hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault()); + hirondelle.date4j.DateTime sixDaysBehind = now.minusDays(6); + + long diff = Math.abs(now.numDaysFrom(sixDaysBehind)); + + assertEquals(diff, 6); + } + +} \ No newline at end of file diff --git a/java-difference-date/README.md b/java-difference-date/README.md new file mode 100644 index 0000000000..2a024c27a2 --- /dev/null +++ b/java-difference-date/README.md @@ -0,0 +1,5 @@ +## Relevant articles: + +- [Period and Duration in Java](http://www.baeldung.com/java-period-duration) +- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro) +- [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api) \ No newline at end of file diff --git a/java-difference-date/pom.xml b/java-difference-date/pom.xml new file mode 100644 index 0000000000..388753de90 --- /dev/null +++ b/java-difference-date/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.baeldung + java-difference-date + 0.0.1-SNAPSHOT + jar + + java-difference-date + Difference between two dates in java + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + joda-time + joda-time + ${joda-time.version} + + + com.darwinsys + hirondelle-date4j + ${hirondelle-date4j.version} + + + junit + junit + ${junit.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + 1.8 + 4.12 + 2.9.9 + 1.5.1 + + diff --git a/java-difference-date/src/test/java/com/baeldung/DateDiffTest.java b/java-difference-date/src/test/java/com/baeldung/DateDiffTest.java new file mode 100644 index 0000000000..4203f7ef38 --- /dev/null +++ b/java-difference-date/src/test/java/com/baeldung/DateDiffTest.java @@ -0,0 +1,61 @@ +package com.baeldung; + +import org.joda.time.DateTime; +import org.junit.Test; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.Duration; +import java.time.ZonedDateTime; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; + +public class DateDiffTest { + @Test + public void givenTwoDatesBeforeJava8_whenDifferentiating_thenWeGetSix() throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH); + Date firstDate = sdf.parse("06/24/2017"); + Date secondDate = sdf.parse("06/30/2017"); + + long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime()); + long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS); + + assertEquals(diff, 6); + } + + @Test + public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() { + ZonedDateTime now = ZonedDateTime.now(); + ZonedDateTime sixDaysBehind = now.minusDays(6); + + Duration duration = Duration.between(now, sixDaysBehind); + long diff = Math.abs(duration.toDays()); + + assertEquals(diff, 6); + } + + @Test + public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() { + DateTime now = DateTime.now(); + DateTime sixDaysBehind = now.minusDays(6); + + org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind); + long diff = Math.abs(duration.getStandardDays()); + + assertEquals(diff, 6); + } + + @Test + public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() { + hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault()); + hirondelle.date4j.DateTime sixDaysBehind = now.minusDays(6); + + long diff = Math.abs(now.numDaysFrom(sixDaysBehind)); + + assertEquals(diff, 6); + } +} \ No newline at end of file From cec17bdbfe717e701b67690782b2ca061bb4c94b Mon Sep 17 00:00:00 2001 From: Seun Matt Date: Fri, 25 Aug 2017 18:49:00 +0100 Subject: [PATCH 2/9] Updated Example Code for Apache Shiro (#2501) * added updated example codes * updated example code StringToCharStream * deleted StringToCharStream.java locally * removed redundant file * added code for apache commons collection SetUtils * refactored example code * added example code for bytebuddy * added example code for PCollections * update pom * refactored tests for PCollections * spring security xml config * spring security xml config * remove redundant comment * example code for apache-shiro * updated example code for Vavr Collections * updated Vavr's Collection example * updated Vavr Collection file * updated example code for Apache Shiro --- apache-shiro/pom.xml | 19 +++- .../com/baeldung/ShiroSpringApplication.java | 45 ++++++++ .../controllers/ShiroSpringController.java | 105 ++++++++++++++++++ .../com/baeldung/models/UserCredentials.java | 40 +++++++ .../src/main/resources/application.properties | 11 ++ .../src/main/resources/templates/index.ftl | 10 ++ .../src/main/resources/templates/login.ftl | 27 +++++ .../src/main/resources/templates/secure.ftl | 15 +++ 8 files changed, 269 insertions(+), 3 deletions(-) create mode 100644 apache-shiro/src/main/java/com/baeldung/ShiroSpringApplication.java create mode 100644 apache-shiro/src/main/java/com/baeldung/controllers/ShiroSpringController.java create mode 100644 apache-shiro/src/main/java/com/baeldung/models/UserCredentials.java create mode 100644 apache-shiro/src/main/resources/application.properties create mode 100644 apache-shiro/src/main/resources/templates/index.ftl create mode 100644 apache-shiro/src/main/resources/templates/login.ftl create mode 100644 apache-shiro/src/main/resources/templates/secure.ftl diff --git a/apache-shiro/pom.xml b/apache-shiro/pom.xml index 97ed872a26..711ddb5cee 100644 --- a/apache-shiro/pom.xml +++ b/apache-shiro/pom.xml @@ -9,9 +9,9 @@ 1.0-SNAPSHOT - com.baeldung - parent-modules - 1.0.0-SNAPSHOT + org.springframework.boot + spring-boot-starter-parent + 1.5.2.RELEASE @@ -21,6 +21,19 @@ + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-freemarker + + + org.apache.shiro + shiro-spring-boot-web-starter + ${apache-shiro-core-version} + org.apache.shiro shiro-core diff --git a/apache-shiro/src/main/java/com/baeldung/ShiroSpringApplication.java b/apache-shiro/src/main/java/com/baeldung/ShiroSpringApplication.java new file mode 100644 index 0000000000..e12d3ebffa --- /dev/null +++ b/apache-shiro/src/main/java/com/baeldung/ShiroSpringApplication.java @@ -0,0 +1,45 @@ +package com.baeldung; + +import org.apache.shiro.realm.Realm; +import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition; +import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +/** + * Created by smatt on 21/08/2017. + */ +@SpringBootApplication +public class ShiroSpringApplication { + + private static final transient Logger log = LoggerFactory.getLogger(ShiroSpringApplication.class); + + public static void main(String... args) { + SpringApplication.run(ShiroSpringApplication.class, args); + } + + + @Bean + public Realm realm() { + return new MyCustomRealm(); + } + + + @Bean + public ShiroFilterChainDefinition shiroFilterChainDefinition() { + DefaultShiroFilterChainDefinition filter + = new DefaultShiroFilterChainDefinition(); + + filter.addPathDefinition("/secure", "authc"); + filter.addPathDefinition("/**", "anon"); + + return filter; + } + + + + +} diff --git a/apache-shiro/src/main/java/com/baeldung/controllers/ShiroSpringController.java b/apache-shiro/src/main/java/com/baeldung/controllers/ShiroSpringController.java new file mode 100644 index 0000000000..e6e72b2579 --- /dev/null +++ b/apache-shiro/src/main/java/com/baeldung/controllers/ShiroSpringController.java @@ -0,0 +1,105 @@ +package com.baeldung.controllers; + +import com.baeldung.models.UserCredentials; +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.subject.Subject; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; + +import javax.servlet.http.HttpServletRequest; + +@Controller +public class ShiroSpringController { + + + + @GetMapping("/") + public String index() { + return "index"; + } + + + @RequestMapping( value = "/login", method = {RequestMethod.GET, RequestMethod.POST}) + public String login(HttpServletRequest req, UserCredentials cred, RedirectAttributes attr) { + + if(req.getMethod().equals(RequestMethod.GET.toString())) { + return "login"; + } + else { + + Subject subject = SecurityUtils.getSubject(); + + if(!subject.isAuthenticated()) { + UsernamePasswordToken token = new UsernamePasswordToken( + cred.getUsername(), cred.getPassword(), cred.isRememberMe()); + try { + subject.login(token); + } catch (AuthenticationException ae) { + ae.printStackTrace(); + attr.addFlashAttribute("error", "Invalid Credentials"); + return "redirect:/login"; + } + } + + return "redirect:/secure"; + } + } + + + @GetMapping("/secure") + public String secure(ModelMap modelMap) { + + Subject currentUser = SecurityUtils.getSubject(); + String role = "", permission = ""; + + if(currentUser.hasRole("admin")) { + role = role + "You are an Admin"; + } + else if(currentUser.hasRole("editor")) { + role = role + "You are an Editor"; + } + else if(currentUser.hasRole("author")) { + role = role + "You are an Author"; + } + + if(currentUser.isPermitted("articles:compose")) { + permission = permission + "You can compose an article, "; + } else { + permission = permission + "You are not permitted to compose an article!, "; + } + + if(currentUser.isPermitted("articles:save")) { + permission = permission + "You can save articles, "; + } else { + permission = permission + "\nYou can not save articles, "; + } + + if(currentUser.isPermitted("articles:publish")) { + permission = permission + "\nYou can publish articles"; + } else { + permission = permission + "\nYou can not publish articles"; + } + + modelMap.addAttribute("username", currentUser.getPrincipal()); + modelMap.addAttribute("permission", permission); + modelMap.addAttribute("role", role); + + return "secure"; + } + + + @PostMapping("/logout") + public String logout() { + Subject subject = SecurityUtils.getSubject(); + subject.logout(); + return "redirect:/"; + } + +} diff --git a/apache-shiro/src/main/java/com/baeldung/models/UserCredentials.java b/apache-shiro/src/main/java/com/baeldung/models/UserCredentials.java new file mode 100644 index 0000000000..51b429046a --- /dev/null +++ b/apache-shiro/src/main/java/com/baeldung/models/UserCredentials.java @@ -0,0 +1,40 @@ +package com.baeldung.models; + +public class UserCredentials { + + private String username; + private String password; + private boolean rememberMe = false; + + public UserCredentials() {} + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public boolean isRememberMe() { + return rememberMe; + } + + public void setRememberMe(boolean rememberMe) { + this.rememberMe = rememberMe; + } + + @Override + public String toString() { + return "username = " + getUsername() + + "\nrememberMe = " + isRememberMe(); + } +} diff --git a/apache-shiro/src/main/resources/application.properties b/apache-shiro/src/main/resources/application.properties new file mode 100644 index 0000000000..5bcb6b3352 --- /dev/null +++ b/apache-shiro/src/main/resources/application.properties @@ -0,0 +1,11 @@ +server.port=9000 +server.servlet-path=/ +server.context-path=/ + +#shiro-spring-boot-config +shiro.loginUrl = /login +shiro.successUrl = /secure +shiro.unauthorizedUrl = /login + +#freemarker +spring.freemarker.suffix=.ftl diff --git a/apache-shiro/src/main/resources/templates/index.ftl b/apache-shiro/src/main/resources/templates/index.ftl new file mode 100644 index 0000000000..0210d656fc --- /dev/null +++ b/apache-shiro/src/main/resources/templates/index.ftl @@ -0,0 +1,10 @@ + + + Index + + +

Welcome Guest!

+
+ Login + + \ No newline at end of file diff --git a/apache-shiro/src/main/resources/templates/login.ftl b/apache-shiro/src/main/resources/templates/login.ftl new file mode 100644 index 0000000000..3a0552dc2e --- /dev/null +++ b/apache-shiro/src/main/resources/templates/login.ftl @@ -0,0 +1,27 @@ + + + Login + + +

Login

+
+
+ <#if (error?length > 0)??> +

${error}

+ <#else> + + + +
+ +

+ +
+ +

+ Remember Me +

+ +
+ + \ No newline at end of file diff --git a/apache-shiro/src/main/resources/templates/secure.ftl b/apache-shiro/src/main/resources/templates/secure.ftl new file mode 100644 index 0000000000..4466a0162f --- /dev/null +++ b/apache-shiro/src/main/resources/templates/secure.ftl @@ -0,0 +1,15 @@ + + + Secure + + +

Welcome ${username}!

+

Role: ${role}

+

Permissions

+

${permission}

+
+
+ +
+ + \ No newline at end of file From d492c6f4d819705fcb6df71b2a57b7fd04524c2c Mon Sep 17 00:00:00 2001 From: "Eunice A. Obugyei" Date: Fri, 25 Aug 2017 18:33:03 +0000 Subject: [PATCH 3/9] BAEL-1014 [Spring MVC with Kotlin] (#2495) * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Removed unnecessary comment * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] Added Exception test cases * Applied baeldung formatter in Eclipse * Merged from https://github.com/eugenp/tutorials Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Revert "Merged from https://github.com/eugenp/tutorials" This reverts commit 74447a163b9e3f244a2578315fbdb525d20cd16b. * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Spring Security for a Java EE Application[http://jira.baeldung.com/browse/BAEL-884] * Updated spring-security version to 4.2.3.RELEASE * Added spring-mvc-kotlin module for http://jira.baeldung.com/browse/BAEL-1014 * Removed dependency for kotlin-reflect * Switched use of JSP to Thymeleaf * Switched use of JSP to Thymeleaf --- spring-mvc-kotlin/pom.xml | 12 ++++-- .../kotlin/mvc/ApplicationWebConfig.kt | 43 ++++++++++++++----- .../main/webapp/WEB-INF/spring-web-config.xml | 23 +++++++--- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/spring-mvc-kotlin/pom.xml b/spring-mvc-kotlin/pom.xml index 5375ecae7c..264cf49817 100644 --- a/spring-mvc-kotlin/pom.xml +++ b/spring-mvc-kotlin/pom.xml @@ -35,9 +35,15 @@ 4.3.10.RELEASE
- javax.servlet - jstl - 1.2 + org.thymeleaf + thymeleaf + 3.0.7.RELEASE + + + + org.thymeleaf + thymeleaf-spring4 + 3.0.7.RELEASE
diff --git a/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/mvc/ApplicationWebConfig.kt b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/mvc/ApplicationWebConfig.kt index 4907e46efb..ec1c4e9511 100644 --- a/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/mvc/ApplicationWebConfig.kt +++ b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/mvc/ApplicationWebConfig.kt @@ -1,13 +1,16 @@ package com.baeldung.kotlin.mvc +import org.springframework.context.ApplicationContext +import org.springframework.context.ApplicationContextAware import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration -import org.springframework.web.servlet.ViewResolver import org.springframework.web.servlet.config.annotation.EnableWebMvc import org.springframework.web.servlet.config.annotation.ViewControllerRegistry import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter -import org.springframework.web.servlet.view.InternalResourceViewResolver -import org.springframework.web.servlet.view.JstlView +import org.thymeleaf.spring4.SpringTemplateEngine +import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver +import org.thymeleaf.spring4.view.ThymeleafViewResolver +import org.thymeleaf.templatemode.TemplateMode @@ -15,7 +18,13 @@ import org.springframework.web.servlet.view.JstlView @EnableWebMvc @Configuration -open class ApplicationWebConfig: WebMvcConfigurerAdapter() { +open class ApplicationWebConfig: WebMvcConfigurerAdapter(), ApplicationContextAware { + + private var applicationContext: ApplicationContext? = null + + override fun setApplicationContext(applicationContext: ApplicationContext?) { + this.applicationContext = applicationContext + } override fun addViewControllers(registry: ViewControllerRegistry?) { super.addViewControllers(registry) @@ -24,14 +33,28 @@ open class ApplicationWebConfig: WebMvcConfigurerAdapter() { } @Bean - open fun viewResolver(): ViewResolver { - val bean = InternalResourceViewResolver() + open fun templateResolver(): SpringResourceTemplateResolver { + val templateResolver = SpringResourceTemplateResolver() + templateResolver.prefix = "/WEB-INF/view/" + templateResolver.suffix = ".html" + templateResolver.templateMode = TemplateMode.HTML + templateResolver.setApplicationContext(this.applicationContext); + return templateResolver + } - bean.setViewClass(JstlView::class.java) - bean.setPrefix("/WEB-INF/view/") - bean.setSuffix(".jsp") + @Bean + open fun templateEngine(): SpringTemplateEngine { + val templateEngine = SpringTemplateEngine() + templateEngine.setTemplateResolver(templateResolver()) + return templateEngine + } - return bean + @Bean + open fun viewResolver(): ThymeleafViewResolver { + val viewResolver = ThymeleafViewResolver() + viewResolver.templateEngine = templateEngine() + viewResolver.order = 1 + return viewResolver } } \ No newline at end of file diff --git a/spring-mvc-kotlin/src/main/webapp/WEB-INF/spring-web-config.xml b/spring-mvc-kotlin/src/main/webapp/WEB-INF/spring-web-config.xml index ffe83a66fa..c7f110ea94 100644 --- a/spring-mvc-kotlin/src/main/webapp/WEB-INF/spring-web-config.xml +++ b/spring-mvc-kotlin/src/main/webapp/WEB-INF/spring-web-config.xml @@ -12,13 +12,26 @@ - - - - - + + + + + + + + + + + + + + + + \ No newline at end of file From 09d10ac85f6ed177b52f90b64f73d1ae484934f6 Mon Sep 17 00:00:00 2001 From: ramansahasi Date: Sat, 26 Aug 2017 00:54:43 +0530 Subject: [PATCH 4/9] BAEL-1076 How to get a number of digits in an int? (#2457) * BAEL-1076 How to get a number of digits in an int? Six different ways to find the number of digits in an integer. * BAEL-1076 How to get a number of digits in an int? * Update NumberOfDigits.java --- .../baeldung/numberofdigits/Benchmarking.java | 131 ++++++++++++++++++ .../numberofdigits/NumberOfDigits.java | 97 +++++++++++++ .../numberofdigits/NumberOfDigitsDriver.java | 27 ++++ .../NumberOfDigitsIntegrationTest.java | 100 +++++++++++++ 4 files changed, 355 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java create mode 100644 core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java create mode 100644 core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java create mode 100644 core-java/src/test/java/com/baeldung/numberofdigits/NumberOfDigitsIntegrationTest.java diff --git a/core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java b/core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java new file mode 100644 index 0000000000..fbd9af2c30 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/numberofdigits/Benchmarking.java @@ -0,0 +1,131 @@ +package com.baeldung.numberofdigits; + +import static com.baeldung.designpatterns.util.LogerUtil.LOG;; + +public class Benchmarking {; + + private static final int LOWER_BOUND = 1; + private static final int UPPER_BOUND = 999999999; + + + public static void main(String[] args) { + LOG.info("Testing all methods..."); + + long length = test_stringBasedSolution(); + LOG.info("String Based Solution : " + length); + + length = test_logarithmicApproach(); + LOG.info("Logarithmic Approach : " + length); + + length = test_repeatedMultiplication(); + LOG.info("Repeated Multiplication : " + length); + + length = test_shiftOperators(); + LOG.info("Shift Operators : " + length); + + length = test_dividingWithPowersOf2(); + LOG.info("Dividing with Powers of 2 : " + length); + + length = test_divideAndConquer(); + LOG.info("Divide And Conquer : " + length); + + } + + private static long test_stringBasedSolution() { + + long startTime, stopTime, elapsedTime; + startTime = System.currentTimeMillis(); + + int total = 0; + for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { + total += NumberOfDigits.stringBasedSolution(i); + } + + stopTime = System.currentTimeMillis(); + elapsedTime = stopTime - startTime; + + return elapsedTime; + } + + private static long test_logarithmicApproach() { + + long startTime, stopTime, elapsedTime; + startTime = System.currentTimeMillis(); + + int total = 0; + for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { + total += NumberOfDigits.logarithmicApproach(i); + } + + stopTime = System.currentTimeMillis(); + elapsedTime = stopTime - startTime; + + return elapsedTime; + } + + private static long test_repeatedMultiplication() { + + long startTime, stopTime, elapsedTime; + startTime = System.currentTimeMillis(); + + int total = 0; + for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { + total += NumberOfDigits.repeatedMultiplication(i); + } + + stopTime = System.currentTimeMillis(); + elapsedTime = stopTime - startTime; + + return elapsedTime; + } + + private static long test_shiftOperators() { + + long startTime, stopTime, elapsedTime; + startTime = System.currentTimeMillis(); + + int total = 0; + for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { + total += NumberOfDigits.shiftOperators(i); + } + + stopTime = System.currentTimeMillis(); + elapsedTime = stopTime - startTime; + + return elapsedTime; + } + + private static long test_dividingWithPowersOf2() { + + long startTime, stopTime, elapsedTime; + startTime = System.currentTimeMillis(); + + int total = 0; + for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { + total += NumberOfDigits.dividingWithPowersOf2(i); + } + + stopTime = System.currentTimeMillis(); + elapsedTime = stopTime - startTime; + + return elapsedTime; + } + + private static long test_divideAndConquer() { + + long startTime, stopTime, elapsedTime; + startTime = System.currentTimeMillis(); + + int total = 0; + for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) { + total += NumberOfDigits.divideAndConquer(i); + } + + stopTime = System.currentTimeMillis(); + elapsedTime = stopTime - startTime; + + return elapsedTime; + } + + +} diff --git a/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java b/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java new file mode 100644 index 0000000000..f455430750 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java @@ -0,0 +1,97 @@ +package com.baeldung.numberofdigits; + +public class NumberOfDigits { + public static int stringBasedSolution(int number) { + int length = String.valueOf(number).length(); + return length; + } + + public static int logarithmicApproach(int number) { + int length = (int) Math.log10(number) + 1; + return length; + } + + public static int repeatedMultiplication(int number) { + int length = 0; + long temp = 1; + while(temp <= number) { + length++; + temp *= 10; + } + return length; + } + + public static int shiftOperators(int number) { + int length = 0; + long temp = 1; + while(temp <= number) { + length++; + temp = (temp << 3) + (temp << 1); + } + return length; + } + + public static int dividingWithPowersOf2(int number) { + int length = 1; + if (number >= 100000000) { + length += 8; + number /= 100000000; + } + if (number >= 10000) { + length += 4; + number /= 10000; + } + if (number >= 100) { + length += 2; + number /= 100; + } + if (number >= 10) { + length += 1; + } + return length; + } + + public static int divideAndConquer(int number) { + if (number < 100000){ + // 5 digits or less + if (number < 100){ + // 1 or 2 + if (number < 10) + return 1; + else + return 2; + }else{ + // 3 to 5 digits + if (number < 1000) + return 3; + else{ + // 4 or 5 digits + if (number < 10000) + return 4; + else + return 5; + } + } + } else { + // 6 digits or more + if (number < 10000000) { + // 6 or 7 digits + if (number < 1000000) + return 6; + else + return 7; + } else { + // 8 to 10 digits + if (number < 100000000) + return 8; + else { + // 9 or 10 digits + if (number < 1000000000) + return 9; + else + return 10; + } + } + } + } +} diff --git a/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java b/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java new file mode 100644 index 0000000000..4d76a5d677 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigitsDriver.java @@ -0,0 +1,27 @@ +package com.baeldung.numberofdigits; + +import static com.baeldung.designpatterns.util.LogerUtil.LOG; + +public class NumberOfDigitsDriver { + public static void main(String[] args) { + LOG.info("Testing all methods..."); + + long length = NumberOfDigits.stringBasedSolution(602); + LOG.info("String Based Solution : " + length); + + length = NumberOfDigits.logarithmicApproach(602); + LOG.info("Logarithmic Approach : " + length); + + length = NumberOfDigits.repeatedMultiplication(602); + LOG.info("Repeated Multiplication : " + length); + + length = NumberOfDigits.shiftOperators(602); + LOG.info("Shift Operators : " + length); + + length = NumberOfDigits.dividingWithPowersOf2(602); + LOG.info("Dividing with Powers of 2 : " + length); + + length = NumberOfDigits.divideAndConquer(602); + LOG.info("Divide And Conquer : " + length); + } +} diff --git a/core-java/src/test/java/com/baeldung/numberofdigits/NumberOfDigitsIntegrationTest.java b/core-java/src/test/java/com/baeldung/numberofdigits/NumberOfDigitsIntegrationTest.java new file mode 100644 index 0000000000..01b874622e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/numberofdigits/NumberOfDigitsIntegrationTest.java @@ -0,0 +1,100 @@ +package com.baeldung.numberofdigits; + +import org.junit.Assert; +import org.junit.Assume; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +@RunWith(Theories.class) +public class NumberOfDigitsIntegrationTest { + + @DataPoints + public static int[][] lowestIntegers() + { + return new int[][]{ + {1, 1}, + {2, 10}, + {3, 100}, + {4, 1000}, + {5, 10000}, + {6, 100000}, + {7, 1000000}, + {8, 10000000}, + {9, 100000000}, + {10, 1000000000} + }; + } + + @DataPoints + public static int[][] highestIntegers() + { + return new int[][]{ + {1, 9}, + {2, 99}, + {3, 999}, + {4, 9999}, + {5, 99999}, + {6, 999999}, + {7, 9999999}, + {8, 99999999}, + {9, 999999999}, + {10, Integer.MAX_VALUE} + }; + } + + @DataPoints + public static int[][] randomIntegers() + { + return new int[][]{ + {1, 1}, + {2, 14}, + {3, 549}, + {4, 1136}, + {5, 25340}, + {6, 134321}, + {7, 1435432}, + {8, 54234129}, + {9, 113683912}, + {10, 1534031982} + }; + } + + @Theory + public void givenDataPoints_whenStringBasedSolutionInvoked_thenAllPointsMatch(final int[] entry) { + Assume.assumeTrue(entry[0] > 0 && entry[1] > 0); + Assert.assertEquals(entry[0], NumberOfDigits.stringBasedSolution(entry[1])); + } + + @Theory + public void givenDataPoints_whenLogarithmicApproachInvoked_thenAllPointsMatch(final int[] entry) { + Assume.assumeTrue(entry[0] > 0 && entry[1] > 0); + Assert.assertEquals(entry[0], NumberOfDigits.logarithmicApproach(entry[1])); + } + + @Theory + public void givenDataPoints_whenRepeatedMultiplicationInvoked_thenAllPointsMatch(final int[] entry) { + Assume.assumeTrue(entry[0] > 0 && entry[1] > 0); + Assert.assertEquals(entry[0], NumberOfDigits.repeatedMultiplication(entry[1])); + } + + @Theory + public void givenDataPoints_whenShiftOperatorsInvoked_thenAllPointsMatch(final int[] entry) { + Assume.assumeTrue(entry[0] > 0 && entry[1] > 0); + Assert.assertEquals(entry[0], NumberOfDigits.shiftOperators(entry[1])); + } + + @Theory + public void givenDataPoints_whenDividingWithPowersOf2Invoked_thenAllPointsMatch(final int[] entry) { + Assume.assumeTrue(entry[0] > 0 && entry[1] > 0); + Assert.assertEquals(entry[0], NumberOfDigits.dividingWithPowersOf2(entry[1])); + } + + @Theory + public void givenDataPoints_whenDivideAndConquerInvoked_thenAllPointsMatch(final int[] entry) { + Assume.assumeTrue(entry[0] > 0 && entry[1] > 0); + Assert.assertEquals(entry[0], NumberOfDigits.divideAndConquer(entry[1])); + } + +} \ No newline at end of file From 25263f1d6f4a098401b04a6672bbab100f77f370 Mon Sep 17 00:00:00 2001 From: Doha2012 Date: Sun, 27 Aug 2017 12:14:26 +0200 Subject: [PATCH 5/9] java concurrency (#2502) * fix spring config * fix spring config * fix spring config * minor fix * fix spring-boot module * fix pom * upgrade jackson * minor fix * java concurrency * cleanup --- core-java-concurrency/.gitignore | 26 ++ core-java-concurrency/README.md | 32 +++ core-java-concurrency/pom.xml | 237 ++++++++++++++++++ .../ScheduledExecutorServiceDemo.java | 0 .../atomic/SafeCounterWithLock.java | 0 .../atomic/SafeCounterWithoutLock.java | 0 .../concurrent/atomic/UnsafeCounter.java | 0 .../blockingqueue/BlockingQueueUsage.java | 0 .../blockingqueue/NumbersConsumer.java | 0 .../blockingqueue/NumbersProducer.java | 0 .../countdownlatch/BrokenWorker.java | 0 .../countdownlatch/WaitingWorker.java | 0 .../concurrent/countdownlatch/Worker.java | 0 .../cyclicbarrier/CyclicBarrierDemo.java | 0 .../cyclicbarrier/CyclicBarrierExample.java | 0 .../concurrent/cyclicbarrier/Task.java | 0 .../concurrent/delayqueue/DelayObject.java | 0 .../delayqueue/DelayQueueConsumer.java | 0 .../delayqueue/DelayQueueProducer.java | 0 .../DiningPhilosophers.java | 0 .../diningphilosophers/Philosopher.java | 0 .../concurrent/executor/ExecutorDemo.java | 0 .../baeldung/concurrent/executor/Invoker.java | 0 .../executorservice/ExecutorServiceDemo.java | 0 .../concurrent/executorservice/Task.java | 0 .../future/FactorialSquareCalculator.java | 0 .../concurrent/future/FutureDemo.java | 0 .../concurrent/future/SquareCalculator.java | 0 .../locks/ReentrantLockWithCondition.java | 0 .../locks/SharedObjectWithLock.java | 0 .../concurrent/locks/StampedLockDemo.java | 0 .../locks/SynchronizedHashMapWithRWLock.java | 0 .../concurrent/phaser/LongRunningAction.java | 0 .../concurrent/semaphore/SemaPhoreDemo.java | 0 .../semaphores/CounterUsingMutex.java | 0 .../DelayQueueUsingTimedSemaphore.java | 0 .../semaphores/LoginQueueUsingSemaphore.java | 0 .../baeldung/concurrent/skiplist/Event.java | 0 .../concurrent/skiplist/EventWindowSort.java | 0 .../concurrent/sleepwait/ThreadA.java | 0 .../concurrent/sleepwait/ThreadB.java | 0 .../sleepwait/WaitSleepExample.java | 0 .../BaeldungSynchronizedBlocks.java | 0 .../BaeldungSynchronizedMethods.java | 0 .../threadfactory/BaeldungThreadFactory.java | 0 .../concurrent/threadfactory/Demo.java | 0 .../concurrent/threadfactory/Task.java | 0 .../volatilekeyword/SharedObject.java | 0 .../com/baeldung/threadlocal/Context.java | 0 .../threadlocal/SharedMapWithUserContext.java | 0 .../ThreadLocalWithUserContext.java | 0 .../baeldung/threadlocal/UserRepository.java | 0 .../com/baeldung/threadpool/CountingTask.java | 0 .../ExitingExecutorServiceExample.java | 0 .../com/baeldung/threadpool/TreeNode.java | 0 .../com/baeldung/transferqueue/Consumer.java | 0 .../com/baeldung/transferqueue/Producer.java | 0 .../src/main/java/log4j.properties | 9 + .../src/main/resources/logback.xml | 19 ++ .../CompletableFutureLongRunningUnitTest.java | 0 .../accumulator/LongAccumulatorUnitTest.java | 0 .../concurrent/adder/LongAdderUnitTest.java | 0 .../atomic/ThreadSafeCounterTest.java | 0 .../atomic/ThreadUnsafeCounterManualTest.java | 0 .../CopyOnWriteArrayListUnitTest.java | 0 .../CountdownLatchExampleIntegrationTest.java | 0 .../delayqueue/DelayQueueIntegrationTest.java | 0 .../FactorialSquareCalculatorUnitTest.java | 0 .../SquareCalculatorIntegrationTest.java | 0 .../locks/SharedObjectWithLockManualTest.java | 0 ...nchronizedHashMapWithRWLockManualTest.java | 0 .../concurrent/phaser/PhaserUnitTest.java | 0 .../PriorityBlockingQueueIntegrationTest.java | 0 .../semaphores/SemaphoresManualTest.java | 0 .../ConcurrentSkipListSetIntegrationTest.java | 0 .../BaeldungSychronizedBlockTest.java | 0 .../BaeldungSynchronizeMethodsTest.java | 0 .../SharedObjectManualTest.java | 0 ...oncurrentMapAggregateStatusManualTest.java | 0 .../ConcurrentMapNullKeyValueManualTest.java | 0 .../ConcurrentMapPerformanceManualTest.java | 48 ++-- .../ConcurrentNavigableMapManualTest.java | 0 ...ncurretMapMemoryConsistencyManualTest.java | 0 .../ConcurrentModificationUnitTest.java | 0 .../Java8ExecutorServiceIntegrationTest.java | 0 .../SynchronousQueueIntegrationTest.java | 0 .../ThreadLocalIntegrationTest.java | 0 .../CoreThreadPoolIntegrationTest.java | 0 .../GuavaThreadPoolIntegrationTest.java | 0 .../TransferQueueIntegrationTest.java | 0 ...adPoolInParallelStreamIntegrationTest.java | 0 .../src/test/resources/.gitignore | 13 + core-java/README.md | 22 -- 93 files changed, 362 insertions(+), 44 deletions(-) create mode 100644 core-java-concurrency/.gitignore create mode 100644 core-java-concurrency/README.md create mode 100644 core-java-concurrency/pom.xml rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/Scheduledexecutorservice/ScheduledExecutorServiceDemo.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/atomic/UnsafeCounter.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/countdownlatch/BrokenWorker.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/countdownlatch/WaitingWorker.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/countdownlatch/Worker.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierDemo.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/delayqueue/DelayObject.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/delayqueue/DelayQueueConsumer.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/delayqueue/DelayQueueProducer.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/diningphilosophers/DiningPhilosophers.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/diningphilosophers/Philosopher.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/executor/ExecutorDemo.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/executor/Invoker.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/executorservice/ExecutorServiceDemo.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/executorservice/Task.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/future/FactorialSquareCalculator.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/future/FutureDemo.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/locks/ReentrantLockWithCondition.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/locks/SharedObjectWithLock.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/locks/StampedLockDemo.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLock.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/semaphore/SemaPhoreDemo.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/semaphores/CounterUsingMutex.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/semaphores/DelayQueueUsingTimedSemaphore.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/semaphores/LoginQueueUsingSemaphore.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/skiplist/Event.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/skiplist/EventWindowSort.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizedBlocks.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizedMethods.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/threadfactory/BaeldungThreadFactory.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/threadfactory/Demo.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/threadfactory/Task.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/concurrent/volatilekeyword/SharedObject.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/threadlocal/Context.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/threadlocal/SharedMapWithUserContext.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/threadlocal/ThreadLocalWithUserContext.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/threadlocal/UserRepository.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/threadpool/CountingTask.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/threadpool/ExitingExecutorServiceExample.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/threadpool/TreeNode.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/transferqueue/Consumer.java (100%) rename {core-java => core-java-concurrency}/src/main/java/com/baeldung/transferqueue/Producer.java (100%) create mode 100644 core-java-concurrency/src/main/java/log4j.properties create mode 100644 core-java-concurrency/src/main/resources/logback.xml rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/accumulator/LongAccumulatorUnitTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/adder/LongAdderUnitTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/atomic/ThreadUnsafeCounterManualTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/copyonwrite/CopyOnWriteArrayListUnitTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleIntegrationTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/delayqueue/DelayQueueIntegrationTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/future/FactorialSquareCalculatorUnitTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/future/SquareCalculatorIntegrationTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/locks/SharedObjectWithLockManualTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueIntegrationTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/skiplist/ConcurrentSkipListSetIntegrationTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/synchronize/BaeldungSychronizedBlockTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizeMethodsTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/concurrent/volatilekeyword/SharedObjectManualTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusManualTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java (62%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyManualTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/java8/Java8ExecutorServiceIntegrationTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/synchronousqueue/SynchronousQueueIntegrationTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/threadlocal/ThreadLocalIntegrationTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/threadpool/CoreThreadPoolIntegrationTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/threadpool/GuavaThreadPoolIntegrationTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/com/baeldung/transferqueue/TransferQueueIntegrationTest.java (100%) rename {core-java => core-java-concurrency}/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamIntegrationTest.java (100%) create mode 100644 core-java-concurrency/src/test/resources/.gitignore diff --git a/core-java-concurrency/.gitignore b/core-java-concurrency/.gitignore new file mode 100644 index 0000000000..3de4cc647e --- /dev/null +++ b/core-java-concurrency/.gitignore @@ -0,0 +1,26 @@ +*.class + +0.* + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* +.resourceCache + +# Packaged files # +*.jar +*.war +*.ear + +# Files generated by integration tests +*.txt +backup-pom.xml +/bin/ +/temp + +#IntelliJ specific +.idea/ +*.iml \ No newline at end of file diff --git a/core-java-concurrency/README.md b/core-java-concurrency/README.md new file mode 100644 index 0000000000..f1d95482d4 --- /dev/null +++ b/core-java-concurrency/README.md @@ -0,0 +1,32 @@ +========= + +## Core Java Concurrency Examples + +### Relevant Articles: +- [Guide To CompletableFuture](http://www.baeldung.com/java-completablefuture) +- [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial) +- [Introduction to Thread Pools in Java](http://www.baeldung.com/thread-pool-java-and-guava) +- [Guide to java.util.concurrent.Future](http://www.baeldung.com/java-future) +- [Guide to java.util.concurrent.BlockingQueue](http://www.baeldung.com/java-blocking-queue) +- [Guide to CountDownLatch in Java](http://www.baeldung.com/java-countdown-latch) +- [A Guide to ConcurrentMap](http://www.baeldung.com/java-concurrent-map) +- [Guide to PriorityBlockingQueue in Java](http://www.baeldung.com/java-priority-blocking-queue) +- [Avoiding the ConcurrentModificationException in Java](http://www.baeldung.com/java-concurrentmodificationexception) +- [Custom Thread Pools In Java 8 Parallel Streams](http://www.baeldung.com/java-8-parallel-streams-custom-threadpool) +- [Guide to java.util.concurrent.Locks](http://www.baeldung.com/java-concurrent-locks) +- [An Introduction to ThreadLocal in Java](http://www.baeldung.com/java-threadlocal) +- [Guide to DelayQueue](http://www.baeldung.com/java-delay-queue) +- [A Guide to Java SynchronousQueue](http://www.baeldung.com/java-synchronous-queue) +- [Guide to the Java TransferQueue](http://www.baeldung.com/java-transfer-queue) +- [Guide to the ConcurrentSkipListMap](http://www.baeldung.com/java-concurrent-skip-list-map) +- [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep) +- [LongAdder and LongAccumulator in Java](http://www.baeldung.com/java-longadder-and-longaccumulator) +- [The Dining Philosophers Problem in Java](http://www.baeldung.com/java-dining-philoshophers) +- [Guide to CopyOnWriteArrayList](http://www.baeldung.com/java-copy-on-write-arraylist) +- [Guide to the Java Phaser](http://www.baeldung.com/java-phaser) +- [Guide to Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized) +- [An Introduction to Atomic Variables in Java](http://www.baeldung.com/java-atomic-variables) +- [CyclicBarrier in Java](http://www.baeldung.com/java-cyclic-barrier) +- [Guide to Volatile Keyword in Java](http://www.baeldung.com/java-volatile) +- [Overview of the java.util.concurrent](http://www.baeldung.com/java-util-concurrent) +- [Semaphores in Java](http://www.baeldung.com/java-semaphore) diff --git a/core-java-concurrency/pom.xml b/core-java-concurrency/pom.xml new file mode 100644 index 0000000000..bf858047e9 --- /dev/null +++ b/core-java-concurrency/pom.xml @@ -0,0 +1,237 @@ + + 4.0.0 + com.baeldung + core-java-concurrency + 0.1.0-SNAPSHOT + jar + + core-java-concurrency + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + + + com.google.guava + guava + ${guava.version} + + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + + commons-io + commons-io + ${commons-io.version} + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + org.apache.commons + commons-math3 + ${commons-math3.version} + + + + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + com.jayway.awaitility + awaitility + ${avaitility.version} + test + + + + + + core-java-concurrency + + + src/main/resources + true + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + true + libs/ + org.baeldung.executable.ExecutableMavenJar + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + ${project.basedir} + + + org.baeldung.executable.ExecutableMavenJar + + + + jar-with-dependencies + + + + + + + org.apache.maven.plugins + maven-shade-plugin + + + + shade + + + true + + + org.baeldung.executable.ExecutableMavenJar + + + + + + + + com.jolira + onejar-maven-plugin + + + + org.baeldung.executable.ExecutableMavenJar + true + ${project.build.finalName}-onejar.${project.packaging} + + + one-jar + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + spring-boot + org.baeldung.executable.ExecutableMavenJar + + + + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*ManualTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + + + + + + 21.0 + 3.5 + 3.6.1 + 2.5 + 4.1 + 4.01 + + + 3.6.1 + 1.7.0 + + + diff --git a/core-java/src/main/java/com/baeldung/concurrent/Scheduledexecutorservice/ScheduledExecutorServiceDemo.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/Scheduledexecutorservice/ScheduledExecutorServiceDemo.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/Scheduledexecutorservice/ScheduledExecutorServiceDemo.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/Scheduledexecutorservice/ScheduledExecutorServiceDemo.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/atomic/UnsafeCounter.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/UnsafeCounter.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/atomic/UnsafeCounter.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/UnsafeCounter.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/countdownlatch/BrokenWorker.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/BrokenWorker.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/countdownlatch/BrokenWorker.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/BrokenWorker.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/countdownlatch/WaitingWorker.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/WaitingWorker.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/countdownlatch/WaitingWorker.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/WaitingWorker.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/countdownlatch/Worker.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/Worker.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/countdownlatch/Worker.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/Worker.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierDemo.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierDemo.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierDemo.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierDemo.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/delayqueue/DelayObject.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/delayqueue/DelayObject.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/delayqueue/DelayObject.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/delayqueue/DelayObject.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/delayqueue/DelayQueueConsumer.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/delayqueue/DelayQueueConsumer.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/delayqueue/DelayQueueConsumer.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/delayqueue/DelayQueueConsumer.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/delayqueue/DelayQueueProducer.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/delayqueue/DelayQueueProducer.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/delayqueue/DelayQueueProducer.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/delayqueue/DelayQueueProducer.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/diningphilosophers/DiningPhilosophers.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/diningphilosophers/DiningPhilosophers.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/diningphilosophers/DiningPhilosophers.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/diningphilosophers/DiningPhilosophers.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/diningphilosophers/Philosopher.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/diningphilosophers/Philosopher.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/diningphilosophers/Philosopher.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/diningphilosophers/Philosopher.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/executor/ExecutorDemo.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/executor/ExecutorDemo.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/executor/ExecutorDemo.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/executor/ExecutorDemo.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/executor/Invoker.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/executor/Invoker.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/executor/Invoker.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/executor/Invoker.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/executorservice/ExecutorServiceDemo.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/executorservice/ExecutorServiceDemo.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/executorservice/ExecutorServiceDemo.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/executorservice/ExecutorServiceDemo.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/executorservice/Task.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/executorservice/Task.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/executorservice/Task.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/executorservice/Task.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/future/FactorialSquareCalculator.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/future/FactorialSquareCalculator.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/future/FactorialSquareCalculator.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/future/FactorialSquareCalculator.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/future/FutureDemo.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/future/FutureDemo.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/future/FutureDemo.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/future/FutureDemo.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/locks/ReentrantLockWithCondition.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/locks/ReentrantLockWithCondition.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/locks/ReentrantLockWithCondition.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/locks/ReentrantLockWithCondition.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/locks/SharedObjectWithLock.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/locks/SharedObjectWithLock.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/locks/SharedObjectWithLock.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/locks/SharedObjectWithLock.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/locks/StampedLockDemo.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/locks/StampedLockDemo.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/locks/StampedLockDemo.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/locks/StampedLockDemo.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLock.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLock.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLock.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLock.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/phaser/LongRunningAction.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/semaphore/SemaPhoreDemo.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/semaphore/SemaPhoreDemo.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/semaphore/SemaPhoreDemo.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/semaphore/SemaPhoreDemo.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/semaphores/CounterUsingMutex.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/semaphores/CounterUsingMutex.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/semaphores/CounterUsingMutex.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/semaphores/CounterUsingMutex.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/semaphores/DelayQueueUsingTimedSemaphore.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/semaphores/DelayQueueUsingTimedSemaphore.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/semaphores/DelayQueueUsingTimedSemaphore.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/semaphores/DelayQueueUsingTimedSemaphore.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/semaphores/LoginQueueUsingSemaphore.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/semaphores/LoginQueueUsingSemaphore.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/semaphores/LoginQueueUsingSemaphore.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/semaphores/LoginQueueUsingSemaphore.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/skiplist/Event.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/skiplist/Event.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/skiplist/Event.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/skiplist/Event.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/skiplist/EventWindowSort.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/skiplist/EventWindowSort.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/skiplist/EventWindowSort.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/skiplist/EventWindowSort.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizedBlocks.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizedBlocks.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizedBlocks.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizedBlocks.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizedMethods.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizedMethods.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizedMethods.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizedMethods.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/threadfactory/BaeldungThreadFactory.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadfactory/BaeldungThreadFactory.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/threadfactory/BaeldungThreadFactory.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/threadfactory/BaeldungThreadFactory.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/threadfactory/Demo.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadfactory/Demo.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/threadfactory/Demo.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/threadfactory/Demo.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/threadfactory/Task.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadfactory/Task.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/threadfactory/Task.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/threadfactory/Task.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/volatilekeyword/SharedObject.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/volatilekeyword/SharedObject.java similarity index 100% rename from core-java/src/main/java/com/baeldung/concurrent/volatilekeyword/SharedObject.java rename to core-java-concurrency/src/main/java/com/baeldung/concurrent/volatilekeyword/SharedObject.java diff --git a/core-java/src/main/java/com/baeldung/threadlocal/Context.java b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/Context.java similarity index 100% rename from core-java/src/main/java/com/baeldung/threadlocal/Context.java rename to core-java-concurrency/src/main/java/com/baeldung/threadlocal/Context.java diff --git a/core-java/src/main/java/com/baeldung/threadlocal/SharedMapWithUserContext.java b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/SharedMapWithUserContext.java similarity index 100% rename from core-java/src/main/java/com/baeldung/threadlocal/SharedMapWithUserContext.java rename to core-java-concurrency/src/main/java/com/baeldung/threadlocal/SharedMapWithUserContext.java diff --git a/core-java/src/main/java/com/baeldung/threadlocal/ThreadLocalWithUserContext.java b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/ThreadLocalWithUserContext.java similarity index 100% rename from core-java/src/main/java/com/baeldung/threadlocal/ThreadLocalWithUserContext.java rename to core-java-concurrency/src/main/java/com/baeldung/threadlocal/ThreadLocalWithUserContext.java diff --git a/core-java/src/main/java/com/baeldung/threadlocal/UserRepository.java b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/UserRepository.java similarity index 100% rename from core-java/src/main/java/com/baeldung/threadlocal/UserRepository.java rename to core-java-concurrency/src/main/java/com/baeldung/threadlocal/UserRepository.java diff --git a/core-java/src/main/java/com/baeldung/threadpool/CountingTask.java b/core-java-concurrency/src/main/java/com/baeldung/threadpool/CountingTask.java similarity index 100% rename from core-java/src/main/java/com/baeldung/threadpool/CountingTask.java rename to core-java-concurrency/src/main/java/com/baeldung/threadpool/CountingTask.java diff --git a/core-java/src/main/java/com/baeldung/threadpool/ExitingExecutorServiceExample.java b/core-java-concurrency/src/main/java/com/baeldung/threadpool/ExitingExecutorServiceExample.java similarity index 100% rename from core-java/src/main/java/com/baeldung/threadpool/ExitingExecutorServiceExample.java rename to core-java-concurrency/src/main/java/com/baeldung/threadpool/ExitingExecutorServiceExample.java diff --git a/core-java/src/main/java/com/baeldung/threadpool/TreeNode.java b/core-java-concurrency/src/main/java/com/baeldung/threadpool/TreeNode.java similarity index 100% rename from core-java/src/main/java/com/baeldung/threadpool/TreeNode.java rename to core-java-concurrency/src/main/java/com/baeldung/threadpool/TreeNode.java diff --git a/core-java/src/main/java/com/baeldung/transferqueue/Consumer.java b/core-java-concurrency/src/main/java/com/baeldung/transferqueue/Consumer.java similarity index 100% rename from core-java/src/main/java/com/baeldung/transferqueue/Consumer.java rename to core-java-concurrency/src/main/java/com/baeldung/transferqueue/Consumer.java diff --git a/core-java/src/main/java/com/baeldung/transferqueue/Producer.java b/core-java-concurrency/src/main/java/com/baeldung/transferqueue/Producer.java similarity index 100% rename from core-java/src/main/java/com/baeldung/transferqueue/Producer.java rename to core-java-concurrency/src/main/java/com/baeldung/transferqueue/Producer.java diff --git a/core-java-concurrency/src/main/java/log4j.properties b/core-java-concurrency/src/main/java/log4j.properties new file mode 100644 index 0000000000..5fe42d854c --- /dev/null +++ b/core-java-concurrency/src/main/java/log4j.properties @@ -0,0 +1,9 @@ +# Set root logger level to DEBUG and its only appender to A1. +log4j.rootLogger=DEBUG, A1 + +# A1 is set to be a ConsoleAppender. +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +# A1 uses PatternLayout. +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n diff --git a/core-java-concurrency/src/main/resources/logback.xml b/core-java-concurrency/src/main/resources/logback.xml new file mode 100644 index 0000000000..ec0dc2469a --- /dev/null +++ b/core-java-concurrency/src/main/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java rename to core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/accumulator/LongAccumulatorUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/accumulator/LongAccumulatorUnitTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/accumulator/LongAccumulatorUnitTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/accumulator/LongAccumulatorUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/adder/LongAdderUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/adder/LongAdderUnitTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/adder/LongAdderUnitTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/adder/LongAdderUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadUnsafeCounterManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/atomic/ThreadUnsafeCounterManualTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/atomic/ThreadUnsafeCounterManualTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/atomic/ThreadUnsafeCounterManualTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/copyonwrite/CopyOnWriteArrayListUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/copyonwrite/CopyOnWriteArrayListUnitTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/copyonwrite/CopyOnWriteArrayListUnitTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/copyonwrite/CopyOnWriteArrayListUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleIntegrationTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleIntegrationTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/delayqueue/DelayQueueIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/delayqueue/DelayQueueIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/delayqueue/DelayQueueIntegrationTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/delayqueue/DelayQueueIntegrationTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/future/FactorialSquareCalculatorUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/future/FactorialSquareCalculatorUnitTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/future/FactorialSquareCalculatorUnitTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/future/FactorialSquareCalculatorUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/future/SquareCalculatorIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/future/SquareCalculatorIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/future/SquareCalculatorIntegrationTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/future/SquareCalculatorIntegrationTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/locks/SharedObjectWithLockManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/locks/SharedObjectWithLockManualTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/locks/SharedObjectWithLockManualTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/locks/SharedObjectWithLockManualTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/phaser/PhaserUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueIntegrationTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueIntegrationTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/skiplist/ConcurrentSkipListSetIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/skiplist/ConcurrentSkipListSetIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/skiplist/ConcurrentSkipListSetIntegrationTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/skiplist/ConcurrentSkipListSetIntegrationTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/synchronize/BaeldungSychronizedBlockTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/synchronize/BaeldungSychronizedBlockTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/synchronize/BaeldungSychronizedBlockTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/synchronize/BaeldungSychronizedBlockTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizeMethodsTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizeMethodsTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizeMethodsTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/synchronize/BaeldungSynchronizeMethodsTest.java diff --git a/core-java/src/test/java/com/baeldung/concurrent/volatilekeyword/SharedObjectManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/volatilekeyword/SharedObjectManualTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/concurrent/volatilekeyword/SharedObjectManualTest.java rename to core-java-concurrency/src/test/java/com/baeldung/concurrent/volatilekeyword/SharedObjectManualTest.java diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusManualTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusManualTest.java rename to core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusManualTest.java diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java rename to core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java similarity index 62% rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java rename to core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java index 5c1612ca60..3f36d0df5d 100644 --- a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java @@ -1,27 +1,31 @@ package com.baeldung.java.concurrentmap; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.util.Collections; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; -import java.util.concurrent.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import org.junit.Test; public class ConcurrentMapPerformanceManualTest { @Test public void givenMaps_whenGetPut500KTimes_thenConcurrentMapFaster() throws Exception { - Map hashtable = new Hashtable<>(); - Map synchronizedHashMap = Collections.synchronizedMap(new HashMap<>()); - Map concurrentHashMap = new ConcurrentHashMap<>(); + final Map hashtable = new Hashtable<>(); + final Map synchronizedHashMap = Collections.synchronizedMap(new HashMap<>()); + final Map concurrentHashMap = new ConcurrentHashMap<>(); - long hashtableAvgRuntime = timeElapseForGetPut(hashtable); - long syncHashMapAvgRuntime = timeElapseForGetPut(synchronizedHashMap); - long concurrentHashMapAvgRuntime = timeElapseForGetPut(concurrentHashMap); + final long hashtableAvgRuntime = timeElapseForGetPut(hashtable); + final long syncHashMapAvgRuntime = timeElapseForGetPut(synchronizedHashMap); + final long concurrentHashMapAvgRuntime = timeElapseForGetPut(concurrentHashMap); System.out.println(String.format("Hashtable: %s, syncHashMap: %s, ConcurrentHashMap: %s", hashtableAvgRuntime, syncHashMapAvgRuntime, concurrentHashMapAvgRuntime)); @@ -31,13 +35,13 @@ public class ConcurrentMapPerformanceManualTest { } private long timeElapseForGetPut(Map map) throws InterruptedException { - ExecutorService executorService = Executors.newFixedThreadPool(4); - long startTime = System.nanoTime(); + final ExecutorService executorService = Executors.newFixedThreadPool(4); + final long startTime = System.nanoTime(); for (int i = 0; i < 4; i++) { executorService.execute(() -> { for (int j = 0; j < 500_000; j++) { - int value = ThreadLocalRandom.current().nextInt(10000); - String key = String.valueOf(value); + final int value = ThreadLocalRandom.current().nextInt(10000); + final String key = String.valueOf(value); map.put(key, value); map.get(key); } @@ -56,11 +60,11 @@ public class ConcurrentMapPerformanceManualTest { return 1; } } - int executeTimes = 5000; + final int executeTimes = 5000; - Map mapOfSameHash = new ConcurrentHashMap<>(); + final Map mapOfSameHash = new ConcurrentHashMap<>(); ExecutorService executorService = Executors.newFixedThreadPool(2); - long sameHashStartTime = System.currentTimeMillis(); + final long sameHashStartTime = System.currentTimeMillis(); for (int i = 0; i < 2; i++) { executorService.execute(() -> { for (int j = 0; j < executeTimes; j++) { @@ -71,10 +75,10 @@ public class ConcurrentMapPerformanceManualTest { executorService.shutdown(); executorService.awaitTermination(5, TimeUnit.SECONDS); - long mapOfSameHashDuration = System.currentTimeMillis() - sameHashStartTime; - Map mapOfDefaultHash = new ConcurrentHashMap<>(); + final long mapOfSameHashDuration = System.currentTimeMillis() - sameHashStartTime; + final Map mapOfDefaultHash = new ConcurrentHashMap<>(); executorService = Executors.newFixedThreadPool(2); - long defaultHashStartTime = System.currentTimeMillis(); + final long defaultHashStartTime = System.currentTimeMillis(); for (int i = 0; i < 2; i++) { executorService.execute(() -> { for (int j = 0; j < executeTimes; j++) { @@ -85,11 +89,11 @@ public class ConcurrentMapPerformanceManualTest { executorService.shutdown(); executorService.awaitTermination(5, TimeUnit.SECONDS); - long mapOfDefaultHashDuration = System.currentTimeMillis() - defaultHashStartTime; + final long mapOfDefaultHashDuration = System.currentTimeMillis() - defaultHashStartTime; assertEquals(executeTimes * 2, mapOfDefaultHash.size()); assertEquals(executeTimes * 2, mapOfSameHash.size()); System.out.println(String.format("same-hash: %s, default-hash: %s", mapOfSameHashDuration, mapOfDefaultHashDuration)); - assertTrue("same hashCode() should greatly degrade performance", mapOfSameHashDuration > mapOfDefaultHashDuration * 10); + assertTrue("same hashCode() should greatly degrade performance", mapOfSameHashDuration > (mapOfDefaultHashDuration * 10)); } } diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTest.java rename to core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTest.java diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyManualTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyManualTest.java rename to core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyManualTest.java diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java rename to core-java-concurrency/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/java8/Java8ExecutorServiceIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/java8/Java8ExecutorServiceIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/java8/Java8ExecutorServiceIntegrationTest.java rename to core-java-concurrency/src/test/java/com/baeldung/java8/Java8ExecutorServiceIntegrationTest.java diff --git a/core-java/src/test/java/com/baeldung/synchronousqueue/SynchronousQueueIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/synchronousqueue/SynchronousQueueIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/synchronousqueue/SynchronousQueueIntegrationTest.java rename to core-java-concurrency/src/test/java/com/baeldung/synchronousqueue/SynchronousQueueIntegrationTest.java diff --git a/core-java/src/test/java/com/baeldung/threadlocal/ThreadLocalIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/threadlocal/ThreadLocalIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/threadlocal/ThreadLocalIntegrationTest.java rename to core-java-concurrency/src/test/java/com/baeldung/threadlocal/ThreadLocalIntegrationTest.java diff --git a/core-java/src/test/java/com/baeldung/threadpool/CoreThreadPoolIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/threadpool/CoreThreadPoolIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/threadpool/CoreThreadPoolIntegrationTest.java rename to core-java-concurrency/src/test/java/com/baeldung/threadpool/CoreThreadPoolIntegrationTest.java diff --git a/core-java/src/test/java/com/baeldung/threadpool/GuavaThreadPoolIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/threadpool/GuavaThreadPoolIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/threadpool/GuavaThreadPoolIntegrationTest.java rename to core-java-concurrency/src/test/java/com/baeldung/threadpool/GuavaThreadPoolIntegrationTest.java diff --git a/core-java/src/test/java/com/baeldung/transferqueue/TransferQueueIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/transferqueue/TransferQueueIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/transferqueue/TransferQueueIntegrationTest.java rename to core-java-concurrency/src/test/java/com/baeldung/transferqueue/TransferQueueIntegrationTest.java diff --git a/core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamIntegrationTest.java b/core-java-concurrency/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamIntegrationTest.java similarity index 100% rename from core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamIntegrationTest.java rename to core-java-concurrency/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamIntegrationTest.java diff --git a/core-java-concurrency/src/test/resources/.gitignore b/core-java-concurrency/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/core-java-concurrency/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/core-java/README.md b/core-java/README.md index a4b5127e98..ef63ac81e9 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -23,7 +23,6 @@ - [Guide to Java Reflection](http://www.baeldung.com/java-reflection) - [A Guide to Java Sockets](http://www.baeldung.com/a-guide-to-java-sockets) - [Java 8 Collectors](http://www.baeldung.com/java-8-collectors) -- [Guide To CompletableFuture](http://www.baeldung.com/java-completablefuture) - [Guide to Java 8’s Functional Interfaces](http://www.baeldung.com/java-8-functional-interfaces) - [Convert char to String in Java](http://www.baeldung.com/java-convert-char-to-string) - [Random List Element](http://www.baeldung.com/java-random-list-element) @@ -31,12 +30,10 @@ - [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda) - [Java – Directory Size](http://www.baeldung.com/java-folder-size) - [Java – Try with Resources](http://www.baeldung.com/java-try-with-resources) -- [A Guide to the Java ExecutorService](http://www.baeldung.com/java-executor-service-tutorial) - [Java 8 New Features](http://www.baeldung.com/java-8-new-features) - [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips) - [The Double Colon Operator in Java 8](http://www.baeldung.com/java-8-double-colon-operator) - [Java 8 Streams Advanced](http://www.baeldung.com/java-8-streams) -- [Introduction to Thread Pools in Java](http://www.baeldung.com/thread-pool-java-and-guava) - [Introduction to Java 8 Streams](http://www.baeldung.com/java-8-streams-introduction) - [Guide to the Fork/Join Framework in Java](http://www.baeldung.com/java-fork-join) - [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java) @@ -57,18 +54,11 @@ - [The Basics of Java Generics](http://www.baeldung.com/java-generics) - [The Traveling Salesman Problem in Java](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman) - [How to Create an Executable JAR with Maven](http://www.baeldung.com/executable-jar-with-maven) -- [Guide to java.util.concurrent.Future](http://www.baeldung.com/java-future) -- [Guide to java.util.concurrent.BlockingQueue](http://www.baeldung.com/java-blocking-queue) -- [Guide to CountDownLatch in Java](http://www.baeldung.com/java-countdown-latch) - [How to Design a Genetic Algorithm in Java](http://www.baeldung.com/java-genetic-algorithm) -- [A Guide to ConcurrentMap](http://www.baeldung.com/java-concurrent-map) -- [Guide to PriorityBlockingQueue in Java](http://www.baeldung.com/java-priority-blocking-queue) - [Guide to Java 8 groupingBy Collector](http://www.baeldung.com/java-groupingby-collector) -- [Avoiding the ConcurrentModificationException in Java](http://www.baeldung.com/java-concurrentmodificationexception) - [Guide to WeakHashMap in Java](http://www.baeldung.com/java-weakhashmap) - [Strategy Design Pattern in Java 8](http://www.baeldung.com/java-strategy-pattern) - [Java 8 and Infinite Streams](http://www.baeldung.com/java-inifinite-streams) -- [Custom Thread Pools In Java 8 Parallel Streams](http://www.baeldung.com/java-8-parallel-streams-custom-threadpool) - [String Operations with Java Streams](http://www.baeldung.com/java-stream-operations-on-strings) - [Spring Security – Cache Control Headers](http://www.baeldung.com/spring-security-cache-control-headers) - [Basic Introduction to JMX](http://www.baeldung.com/java-management-extensions) @@ -91,7 +81,6 @@ - [Avoiding ConcurrentModificationException when iterating and removing](http://www.baeldung.com/avoiding-concurrentmodificationexception-when-iterating-and-removing) - [Removing all nulls from a List in Java](http://www.baeldung.com/java-remove-nulls-from-list) - [Removing all duplicates from a List in Java](http://www.baeldung.com/java-remove-duplicates-from-list) -- [An Introduction to ThreadLocal in Java](http://www.baeldung.com/java-threadlocal) - [Using Math.pow in Java](http://www.baeldung.com/java-math-pow) - [Converting Strings to Enums in Java](http://www.baeldung.com/java-string-to-enum) - [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections) @@ -101,19 +90,12 @@ - [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset) - [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request) - [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection) -- [Guide to DelayQueue](http://www.baeldung.com/java-delay-queue) - [Guide to UUID in JAVA](http://www.baeldung.com/guide-to-uuid-in-java) - [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](http://www.baeldung.com/java-path) - [How to Add a Single Element to a Stream](http://www.baeldung.com/java-stream-append-prepend) - [Iterating Over Enum Values in Java](http://www.baeldung.com/java-enum-iteration) -- [A Guide to Java SynchronousQueue](http://www.baeldung.com/java-synchronous-queue) -- [Guide to the Java TransferQueue](http://www.baeldung.com/java-transfer-queue) - [Kotlin Java Interoperability](http://www.baeldung.com/kotlin-java-interoperability) -- [Guide to the ConcurrentSkipListMap](http://www.baeldung.com/java-concurrent-skip-list-map) -- [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep) -- [LongAdder and LongAccumulator in Java](http://www.baeldung.com/java-longadder-and-longaccumulator) - [Using Java MappedByteBuffer](http://www.baeldung.com/java-mapped-byte-buffer) -- [The Dining Philosophers Problem in Java](http://www.baeldung.com/java-dining-philoshophers) - [The Difference Between map() and flatMap()](http://www.baeldung.com/java-difference-map-and-flatmap) - [How to Round a Number to N Decimal Places in Java](http://www.baeldung.com/java-round-decimal-number) - [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params) @@ -122,21 +104,17 @@ - [Changing the Order in a Sum Operation Can Produce Different Results?](http://www.baeldung.com/java-floating-point-sum-order) - [How to Get a Name of a Method Being Executed?](http://www.baeldung.com/java-name-of-executing-method) - [Iterate over a Map in Java](http://www.baeldung.com/java-iterate-map) -- [CyclicBarrier in Java](http://www.baeldung.com/java-cyclic-barrier) - [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies) - [How to Copy an Array in Java](http://www.baeldung.com/java-array-copy) - [Introduction to JDBC](http://www.baeldung.com/java-jdbc) -- [Guide to CopyOnWriteArrayList](http://www.baeldung.com/java-copy-on-write-arraylist) - [Period and Duration in Java](http://www.baeldung.com/java-period-duration) - [Converting a Stack Trace to a String in Java](http://www.baeldung.com/java-stacktrace-to-string) -- [Guide to the Java Phaser](http://www.baeldung.com/java-phaser) - [Count Occurrences of a Char in a String](http://www.baeldung.com/java-count-chars) - [Java Double Brace Initialization](http://www.baeldung.com/java-double-brace-initialization) - [The StackOverflowError in Java](http://www.baeldung.com/java-stack-overflow-error) - [Split a String in Java](http://www.baeldung.com/java-split-string) - [Introduction to Java Serialization](http://www.baeldung.com/java-serialization) - [How to Remove the Last Character of a String?](http://www.baeldung.com/java-remove-last-character-of-string) -- [Guide to Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized) - [ClassNotFoundException vs NoClassDefFoundError](http://www.baeldung.com/java-classnotfoundexception-and-noclassdeffounderror) - [Guide to UUID in Java](http://www.baeldung.com/java-uuid) - [How to Get the Last Element of a Stream in Java?](http://www.baeldung.com/java-stream-last-element) From 0b85b0a46637677048b7236874944208ec2dd44c Mon Sep 17 00:00:00 2001 From: lor6 Date: Sun, 27 Aug 2017 14:20:31 +0300 Subject: [PATCH 6/9] functional bean registration test (#2503) --- .../com/baeldung/functional/MyService.java | 13 ++++++ .../functional/BeanRegistrationTest.java | 42 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 spring-5/src/main/java/com/baeldung/functional/MyService.java create mode 100644 spring-5/src/test/java/com/baeldung/functional/BeanRegistrationTest.java diff --git a/spring-5/src/main/java/com/baeldung/functional/MyService.java b/spring-5/src/main/java/com/baeldung/functional/MyService.java new file mode 100644 index 0000000000..d85a860f06 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/functional/MyService.java @@ -0,0 +1,13 @@ +package com.baeldung.functional; + +import java.util.Random; + +import org.springframework.stereotype.Component; + +public class MyService { + + public int getRandomNumber(){ + return (new Random().nextInt(10)); + } + +} diff --git a/spring-5/src/test/java/com/baeldung/functional/BeanRegistrationTest.java b/spring-5/src/test/java/com/baeldung/functional/BeanRegistrationTest.java new file mode 100644 index 0000000000..0b1542dbbc --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/functional/BeanRegistrationTest.java @@ -0,0 +1,42 @@ +package com.baeldung.functional; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.context.support.GenericWebApplicationContext; + +import com.baeldung.Spring5Application; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Spring5Application.class) +public class BeanRegistrationTest { + + @Autowired + private GenericWebApplicationContext context; + + @Test + public void whenRegisterBean_thenOk() { + context.registerBean(MyService.class, () -> new MyService()); + MyService myService = (MyService) context.getBean("com.baeldung.functional.MyService"); + assertTrue(myService.getRandomNumber() < 10); + } + + @Test + public void whenRegisterBeanWithName_thenOk() { + context.registerBean("mySecondService", MyService.class, () -> new MyService()); + MyService mySecondService = (MyService) context.getBean("mySecondService"); + assertTrue(mySecondService.getRandomNumber() < 10); + } + + @Test + public void whenRegisterBeanWithCallback_thenOk() { + context.registerBean("myCallbackService", MyService.class, () -> new MyService(), bd -> bd.setAutowireCandidate(false)); + MyService myCallbackService = (MyService) context.getBean("myCallbackService"); + assertTrue(myCallbackService.getRandomNumber() < 10); + } + +} From 515e4caaca07032e213b0afe09d90d8cda3bd13f Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sun, 27 Aug 2017 13:39:19 +0200 Subject: [PATCH 7/9] Add core-java-concurrency to main pom (#2507) * Add core-java-concurrency * Refactor codebase --- .../atomic/SafeCounterWithLock.java | 4 ++-- .../atomic/SafeCounterWithoutLock.java | 4 ++-- .../concurrent/atomic/UnsafeCounter.java | 6 ++--- .../com/baeldung/threadlocal/Context.java | 6 ++--- .../threadlocal/SharedMapWithUserContext.java | 4 ++-- .../ThreadLocalWithUserContext.java | 2 +- .../baeldung/threadlocal/UserRepository.java | 2 +- .../com/baeldung/threadpool/CountingTask.java | 8 ++++--- .../com/baeldung/threadpool/TreeNode.java | 17 +++++++++----- .../com/baeldung/transferqueue/Consumer.java | 6 ++--- .../com/baeldung/transferqueue/Producer.java | 6 ++--- .../CompletableFutureLongRunningUnitTest.java | 6 ++++- .../CountdownLatchExampleIntegrationTest.java | 10 +++++---- .../SquareCalculatorIntegrationTest.java | 1 - ...nchronizedHashMapWithRWLockManualTest.java | 4 +--- .../ConcurrentMapNullKeyValueManualTest.java | 2 +- .../ConcurrentNavigableMapManualTest.java | 22 +++++++++---------- .../ConcurrentModificationUnitTest.java | 15 ++++++------- .../Java8ExecutorServiceIntegrationTest.java | 2 +- ...adPoolInParallelStreamIntegrationTest.java | 5 ++++- pom.xml | 1 + 21 files changed, 74 insertions(+), 59 deletions(-) diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java index 38633011bf..e3a1629ce1 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java @@ -3,11 +3,11 @@ package com.baeldung.concurrent.atomic; public class SafeCounterWithLock { private volatile int counter; - public int getValue() { + int getValue() { return counter; } - public synchronized void increment() { + synchronized void increment() { counter++; } } diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java index 41e10789a6..18ade35efb 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java @@ -5,11 +5,11 @@ import java.util.concurrent.atomic.AtomicInteger; public class SafeCounterWithoutLock { private final AtomicInteger counter = new AtomicInteger(0); - public int getValue() { + int getValue() { return counter.get(); } - public void increment() { + void increment() { while(true) { int existingValue = getValue(); int newValue = existingValue + 1; diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/UnsafeCounter.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/UnsafeCounter.java index 8a72788842..500ef5bd7e 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/UnsafeCounter.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/atomic/UnsafeCounter.java @@ -1,13 +1,13 @@ package com.baeldung.concurrent.atomic; public class UnsafeCounter { - int counter; + private int counter; - public int getValue() { + int getValue() { return counter; } - public void increment() { + void increment() { counter++; } } diff --git a/core-java-concurrency/src/main/java/com/baeldung/threadlocal/Context.java b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/Context.java index 241fb2f1e0..88b78fb259 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/threadlocal/Context.java +++ b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/Context.java @@ -4,14 +4,14 @@ package com.baeldung.threadlocal; public class Context { private final String userName; - public Context(String userName) { + Context(String userName) { this.userName = userName; } @Override public String toString() { return "Context{" + - "userNameSecret='" + userName + '\'' + - '}'; + "userNameSecret='" + userName + '\'' + + '}'; } } diff --git a/core-java-concurrency/src/main/java/com/baeldung/threadlocal/SharedMapWithUserContext.java b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/SharedMapWithUserContext.java index e5854e218a..8cb4b3968f 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/threadlocal/SharedMapWithUserContext.java +++ b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/SharedMapWithUserContext.java @@ -5,11 +5,11 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class SharedMapWithUserContext implements Runnable { - public final static Map userContextPerUserId = new ConcurrentHashMap<>(); + final static Map userContextPerUserId = new ConcurrentHashMap<>(); private final Integer userId; private UserRepository userRepository = new UserRepository(); - public SharedMapWithUserContext(Integer userId) { + SharedMapWithUserContext(Integer userId) { this.userId = userId; } diff --git a/core-java-concurrency/src/main/java/com/baeldung/threadlocal/ThreadLocalWithUserContext.java b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/ThreadLocalWithUserContext.java index de7e4a0369..d4ab906c30 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/threadlocal/ThreadLocalWithUserContext.java +++ b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/ThreadLocalWithUserContext.java @@ -10,7 +10,7 @@ public class ThreadLocalWithUserContext implements Runnable { private final Integer userId; private UserRepository userRepository = new UserRepository(); - public ThreadLocalWithUserContext(Integer userId) { + ThreadLocalWithUserContext(Integer userId) { this.userId = userId; } diff --git a/core-java-concurrency/src/main/java/com/baeldung/threadlocal/UserRepository.java b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/UserRepository.java index 3fe76f75c0..2597594940 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/threadlocal/UserRepository.java +++ b/core-java-concurrency/src/main/java/com/baeldung/threadlocal/UserRepository.java @@ -4,7 +4,7 @@ import java.util.UUID; public class UserRepository { - public String getUserNameForUserId(Integer userId) { + String getUserNameForUserId(Integer userId) { return UUID.randomUUID().toString(); } } diff --git a/core-java-concurrency/src/main/java/com/baeldung/threadpool/CountingTask.java b/core-java-concurrency/src/main/java/com/baeldung/threadpool/CountingTask.java index effdf54916..a7447d040f 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/threadpool/CountingTask.java +++ b/core-java-concurrency/src/main/java/com/baeldung/threadpool/CountingTask.java @@ -2,19 +2,21 @@ package com.baeldung.threadpool; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.RecursiveTask; -import java.util.stream.Collectors; public class CountingTask extends RecursiveTask { private final TreeNode node; - public CountingTask(TreeNode node) { + CountingTask(TreeNode node) { this.node = node; } @Override protected Integer compute() { - return node.value + node.children.stream().map(childNode -> new CountingTask(childNode).fork()).collect(Collectors.summingInt(ForkJoinTask::join)); + return node.getValue() + node.getChildren().stream() + .map(childNode -> new CountingTask(childNode).fork()) + .mapToInt(ForkJoinTask::join) + .sum(); } } diff --git a/core-java-concurrency/src/main/java/com/baeldung/threadpool/TreeNode.java b/core-java-concurrency/src/main/java/com/baeldung/threadpool/TreeNode.java index 9b43152074..65dfed01a2 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/threadpool/TreeNode.java +++ b/core-java-concurrency/src/main/java/com/baeldung/threadpool/TreeNode.java @@ -1,18 +1,25 @@ package com.baeldung.threadpool; -import java.util.Set; - import com.google.common.collect.Sets; +import java.util.Set; + public class TreeNode { - int value; + private int value; - Set children; + private Set children; - public TreeNode(int value, TreeNode... children) { + TreeNode(int value, TreeNode... children) { this.value = value; this.children = Sets.newHashSet(children); } + public int getValue() { + return value; + } + + public Set getChildren() { + return children; + } } diff --git a/core-java-concurrency/src/main/java/com/baeldung/transferqueue/Consumer.java b/core-java-concurrency/src/main/java/com/baeldung/transferqueue/Consumer.java index a5f70d9df5..f3be6a030e 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/transferqueue/Consumer.java +++ b/core-java-concurrency/src/main/java/com/baeldung/transferqueue/Consumer.java @@ -11,10 +11,10 @@ public class Consumer implements Runnable { private final TransferQueue transferQueue; private final String name; - private final int numberOfMessagesToConsume; - public final AtomicInteger numberOfConsumedMessages = new AtomicInteger(); + final int numberOfMessagesToConsume; + final AtomicInteger numberOfConsumedMessages = new AtomicInteger(); - public Consumer(TransferQueue transferQueue, String name, int numberOfMessagesToConsume) { + Consumer(TransferQueue transferQueue, String name, int numberOfMessagesToConsume) { this.transferQueue = transferQueue; this.name = name; this.numberOfMessagesToConsume = numberOfMessagesToConsume; diff --git a/core-java-concurrency/src/main/java/com/baeldung/transferqueue/Producer.java b/core-java-concurrency/src/main/java/com/baeldung/transferqueue/Producer.java index c7df7c410a..b73cf5ac19 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/transferqueue/Producer.java +++ b/core-java-concurrency/src/main/java/com/baeldung/transferqueue/Producer.java @@ -12,10 +12,10 @@ public class Producer implements Runnable { private final TransferQueue transferQueue; private final String name; - private final Integer numberOfMessagesToProduce; - public final AtomicInteger numberOfProducedMessages = new AtomicInteger(); + final Integer numberOfMessagesToProduce; + final AtomicInteger numberOfProducedMessages = new AtomicInteger(); - public Producer(TransferQueue transferQueue, String name, Integer numberOfMessagesToProduce) { + Producer(TransferQueue transferQueue, String name, Integer numberOfMessagesToProduce) { this.transferQueue = transferQueue; this.name = name; this.numberOfMessagesToProduce = numberOfMessagesToProduce; diff --git a/core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java index 0a6d94e126..45d2ec68e4 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java @@ -4,7 +4,11 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.*; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.stream.Collectors; import java.util.stream.Stream; diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleIntegrationTest.java index fc343e4cee..d49a8b8590 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleIntegrationTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleIntegrationTest.java @@ -18,7 +18,9 @@ public class CountdownLatchExampleIntegrationTest { // Given List outputScraper = Collections.synchronizedList(new ArrayList<>()); CountDownLatch countDownLatch = new CountDownLatch(5); - List workers = Stream.generate(() -> new Thread(new Worker(outputScraper, countDownLatch))).limit(5).collect(toList()); + List workers = Stream.generate(() -> new Thread(new Worker(outputScraper, countDownLatch))) + .limit(5) + .collect(toList()); // When workers.forEach(Thread::start); @@ -26,7 +28,6 @@ public class CountdownLatchExampleIntegrationTest { outputScraper.add("Latch released"); // Then - outputScraper.forEach(Object::toString); assertThat(outputScraper).containsExactly("Counted down", "Counted down", "Counted down", "Counted down", "Counted down", "Latch released"); } @@ -35,7 +36,9 @@ public class CountdownLatchExampleIntegrationTest { // Given List outputScraper = Collections.synchronizedList(new ArrayList<>()); CountDownLatch countDownLatch = new CountDownLatch(5); - List workers = Stream.generate(() -> new Thread(new BrokenWorker(outputScraper, countDownLatch))).limit(5).collect(toList()); + List workers = Stream.generate(() -> new Thread(new BrokenWorker(outputScraper, countDownLatch))) + .limit(5) + .collect(toList()); // When workers.forEach(Thread::start); @@ -63,7 +66,6 @@ public class CountdownLatchExampleIntegrationTest { outputScraper.add("Workers complete"); // Then - outputScraper.forEach(Object::toString); assertThat(outputScraper).containsExactly("Workers ready", "Counted down", "Counted down", "Counted down", "Counted down", "Counted down", "Workers complete"); } diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/future/SquareCalculatorIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/future/SquareCalculatorIntegrationTest.java index 5f8b05a974..c2513f38c1 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/future/SquareCalculatorIntegrationTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/future/SquareCalculatorIntegrationTest.java @@ -22,7 +22,6 @@ public class SquareCalculatorIntegrationTest { private static final Logger LOG = LoggerFactory.getLogger(SquareCalculatorIntegrationTest.class); - @Rule public TestName name = new TestName(); diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java index 3014ae38b2..f3ced219f7 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java @@ -49,9 +49,7 @@ public class SynchronizedHashMapWithRWLockManualTest { private void executeReaderThreads(SynchronizedHashMapWithRWLock object, int threadCount, ExecutorService service) { for (int i = 0; i < threadCount; i++) - service.execute(() -> { - object.get("key" + threadCount); - }); + service.execute(() -> object.get("key" + threadCount)); } } diff --git a/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java index 33e3326427..cbac6e7f4c 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java @@ -12,7 +12,7 @@ import static org.junit.Assert.assertNull; public class ConcurrentMapNullKeyValueManualTest { - ConcurrentMap concurrentMap; + private ConcurrentMap concurrentMap; @Before public void setup() { diff --git a/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTest.java b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTest.java index 768c31ee80..c0753db513 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTest.java @@ -18,7 +18,7 @@ public class ConcurrentNavigableMapManualTest { public void givenSkipListMap_whenAccessInMultiThreads_thenOrderingStable() throws InterruptedException { NavigableMap skipListMap = new ConcurrentSkipListMap<>(); - updateMapConcurrently(skipListMap, 4); + updateMapConcurrently(skipListMap); Iterator skipListIter = skipListMap.keySet().iterator(); int previous = skipListIter.next(); @@ -28,9 +28,9 @@ public class ConcurrentNavigableMapManualTest { } } - private void updateMapConcurrently(NavigableMap navigableMap, int concurrencyLevel) throws InterruptedException { - ExecutorService executorService = Executors.newFixedThreadPool(concurrencyLevel); - for (int i = 0; i < concurrencyLevel; i++) { + private void updateMapConcurrently(NavigableMap navigableMap) throws InterruptedException { + ExecutorService executorService = Executors.newFixedThreadPool(4); + for (int i = 0; i < 4; i++) { executorService.execute(() -> { ThreadLocalRandom random = ThreadLocalRandom.current(); for (int j = 0; j < 10000; j++) { @@ -45,26 +45,26 @@ public class ConcurrentNavigableMapManualTest { @Test public void givenSkipListMap_whenNavConcurrently_thenCountCorrect() throws InterruptedException { NavigableMap skipListMap = new ConcurrentSkipListMap<>(); - int count = countMapElementByPollingFirstEntry(skipListMap, 10000, 4); + int count = countMapElementByPollingFirstEntry(skipListMap); assertEquals(10000 * 4, count); } @Test public void givenTreeMap_whenNavConcurrently_thenCountError() throws InterruptedException { NavigableMap treeMap = new TreeMap<>(); - int count = countMapElementByPollingFirstEntry(treeMap, 10000, 4); + int count = countMapElementByPollingFirstEntry(treeMap); assertNotEquals(10000 * 4, count); } - private int countMapElementByPollingFirstEntry(NavigableMap navigableMap, int elementCount, int concurrencyLevel) throws InterruptedException { - for (int i = 0; i < elementCount * concurrencyLevel; i++) { + private int countMapElementByPollingFirstEntry(NavigableMap navigableMap) throws InterruptedException { + for (int i = 0; i < 10000 * 4; i++) { navigableMap.put(i, i); } AtomicInteger counter = new AtomicInteger(0); - ExecutorService executorService = Executors.newFixedThreadPool(concurrencyLevel); - for (int j = 0; j < concurrencyLevel; j++) { + ExecutorService executorService = Executors.newFixedThreadPool(4); + for (int j = 0; j < 4; j++) { executorService.execute(() -> { - for (int i = 0; i < elementCount; i++) { + for (int i = 0; i < 10000; i++) { if (navigableMap.pollFirstEntry() != null) { counter.incrementAndGet(); } diff --git a/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java index f7a7bd5fe0..9cdac72d59 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java @@ -2,7 +2,6 @@ package com.baeldung.java.concurrentmodification; import org.junit.Test; -import java.util.ArrayList; import java.util.Collection; import java.util.ConcurrentModificationException; import java.util.Iterator; @@ -28,9 +27,9 @@ public class ConcurrentModificationUnitTest { List integers = newArrayList(1, 2, 3); - for (Iterator iterator = integers.iterator(); iterator.hasNext();) { + for (Iterator iterator = integers.iterator(); iterator.hasNext(); ) { Integer integer = iterator.next(); - if(integer == 2) { + if (integer == 2) { iterator.remove(); } } @@ -45,7 +44,7 @@ public class ConcurrentModificationUnitTest { List toRemove = newArrayList(); for (Integer integer : integers) { - if(integer == 2) { + if (integer == 2) { toRemove.add(integer); } } @@ -69,10 +68,10 @@ public class ConcurrentModificationUnitTest { Collection integers = newArrayList(1, 2, 3); List collected = integers - .stream() - .filter(i -> i != 2) - .map(Object::toString) - .collect(toList()); + .stream() + .filter(i -> i != 2) + .map(Object::toString) + .collect(toList()); assertThat(collected).containsExactly("1", "3"); } diff --git a/core-java-concurrency/src/test/java/com/baeldung/java8/Java8ExecutorServiceIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/java8/Java8ExecutorServiceIntegrationTest.java index 41eb864fd9..a1f5b6f1e2 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/java8/Java8ExecutorServiceIntegrationTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/java8/Java8ExecutorServiceIntegrationTest.java @@ -72,7 +72,7 @@ public class Java8ExecutorServiceIntegrationTest { assertTrue(threadPoolExecutor.isShutdown()); assertFalse(notExecutedTasks.isEmpty()); - assertTrue(notExecutedTasks.size() > 0 && notExecutedTasks.size() < 98); + assertTrue(notExecutedTasks.size() < 98); } private List smartShutdown(ExecutorService executorService) { diff --git a/core-java-concurrency/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamIntegrationTest.java b/core-java-concurrency/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamIntegrationTest.java index 42e85fc586..502672dea1 100644 --- a/core-java-concurrency/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamIntegrationTest.java +++ b/core-java-concurrency/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamIntegrationTest.java @@ -23,7 +23,10 @@ public class ThreadPoolInParallelStreamIntegrationTest { List aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList()); ForkJoinPool customThreadPool = new ForkJoinPool(4); - long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get(); + long actualTotal = customThreadPool + .submit(() -> aList.parallelStream() + .reduce(0L, Long::sum)) + .get(); assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal); } diff --git a/pom.xml b/pom.xml index f2dd0ae48c..da38d65df3 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,7 @@ cdi core-java + core-java-concurrency couchbase-sdk deltaspike From e1e6550a49f7aa01a968a0e57a6a9bc7103d7446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ho=C3=A0ng=20H=C6=B0ng?= Date: Mon, 28 Aug 2017 03:44:06 +0700 Subject: [PATCH 8/9] BAEL-1080 Introduction to Future in Vavr (#2488) * Add example of different types of bean injection * BAEL-1080 Introduction to Future and Pattern Matching in Vavr * Update future with Pattern Matching * remove unused files * Update future unit test of cancelled Future * remove unused import * delete unused files * using await() on future to wait until future completed instead of using while(true) * Add Awaitability to FutureTest, avoid using while loop * format code --- vavr/pom.xml | 8 + .../baeldung/vavr/future/FutureUnitTest.java | 203 ++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 vavr/src/test/java/com/baeldung/vavr/future/FutureUnitTest.java diff --git a/vavr/pom.xml b/vavr/pom.xml index 426155263c..53cd07ddf7 100644 --- a/vavr/pom.xml +++ b/vavr/pom.xml @@ -40,6 +40,13 @@ org.springframework.boot spring-boot-starter-test + + + org.awaitility + awaitility + ${awaitility.version} + test + @@ -69,6 +76,7 @@ 1.8 0.9.0 4.12 + 3.0.0 diff --git a/vavr/src/test/java/com/baeldung/vavr/future/FutureUnitTest.java b/vavr/src/test/java/com/baeldung/vavr/future/FutureUnitTest.java new file mode 100644 index 0000000000..84621e3a68 --- /dev/null +++ b/vavr/src/test/java/com/baeldung/vavr/future/FutureUnitTest.java @@ -0,0 +1,203 @@ +package com.baeldung.vavr.future; + +import static io.vavr.API.$; +import static io.vavr.API.Case; +import static io.vavr.API.Match; +import static io.vavr.Predicates.exists; +import static io.vavr.Predicates.forAll; +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.CancellationException; +import java.util.function.Predicate; + +import org.junit.Test; + +import io.vavr.collection.List; +import io.vavr.concurrent.Future; + +public class FutureUnitTest { + + private final String SUCCESS = "Success"; + private final String FAILURE = "Failure"; + + @Test + public void givenFunctionReturnInteger_WhenCallWithFuture_ShouldReturnFunctionValue() { + Future future = Future.of(() -> 1); + + assertEquals(1, future.get() + .intValue()); + } + + @Test + public void givenFunctionGetRemoteHttpResourceAsString_WhenCallSuccessWithFuture_ShouldReturnContentValueAsString() { + String url = "http://resource"; + String content = "Content from " + url; + Future future = Future.of(() -> getResource(url)); + + assertEquals(content, future.get()); + } + + @Test + public void givenFunctionThrowException_WhenCallWithFuture_ShouldReturnFailure() { + Future future = Future.of(() -> getResourceThrowException("")); + future.await(); + + assertTrue(future.isFailure()); + } + + @Test + public void givenAFutureReturnZero_WhenCheckFutureWithExistEvenValue_ShouldReturnRight() { + Future future = Future.of(() -> 2); + boolean result = future.exists(i -> i % 2 == 0); + + assertTrue(result); + } + + @Test + public void givenFunction_WhenCallWithFutureAndRegisterConsumerForSuccess_ShouldCallConsumerToStoreValue() { + final int[] store = new int[] { 0 }; + Future future = Future.of(() -> 1); + future.onSuccess(i -> { + store[0] = i; + }); + await().until(() -> store[0] == 1); + } + + @Test + public void givenFunctionThrowException_WhenCallWithFutureAndRegisterConsumerForFailer_ShouldCallConsumerToStoreException() { + final Throwable[] store = new Throwable[] { null }; + Future future = Future.of(() -> getResourceThrowException("")); + future.onFailure(err -> store[0] = err); + await().until(() -> RuntimeException.class.isInstance(store[0])); + } + + @Test + public void givenAFuture_WhenAddAndThenConsumer_ShouldCallConsumerWithResultOfFutureAction() { + int[] store1 = new int[1]; + int[] store2 = new int[1]; + Future future = Future.of(() -> 1); + Future andThenFuture = future.andThen(i -> store1[0] = i.get() + 1) + .andThen(i -> store2[0] = store1[0] + 1); + andThenFuture.await(); + + assertEquals(2, store1[0]); + assertEquals(3, store2[0]); + } + + @Test + public void givenAFailureFuture_WhenCallOrElseFunction_ShouldReturnNewFuture() { + Future future = Future.failed(new RuntimeException()); + Future future2 = future.orElse(Future.of(() -> 2)); + + assertEquals(2, future2.get() + .intValue()); + } + + @Test(expected = CancellationException.class) + public void givenAFuture_WhenCallCancel_ShouldReturnCancellationException() { + long waitTime = 1000; + Future future = Future.of(() -> { + Thread.sleep(waitTime); + return 1; + }); + future.cancel(); + future.await(); + future.get(); + } + + @Test + public void givenAFuture_WhenCallFallBackWithSuccessFuture_ShouldReturnFutureResult() { + String expectedResult = "take this"; + Future future = Future.of(() -> expectedResult); + Future secondFuture = Future.of(() -> "take that"); + Future futureResult = future.fallbackTo(secondFuture); + futureResult.await(); + + assertEquals(expectedResult, futureResult.get()); + } + + @Test + public void givenAFuture_WhenCallFallBackWithFailureFuture_ShouldReturnValueOfFallBackFuture() { + String expectedResult = "take that"; + Future future = Future.failed(new RuntimeException()); + Future fallbackFuture = Future.of(() -> expectedResult); + Future futureResult = future.fallbackTo(fallbackFuture); + futureResult.await(); + + assertEquals(expectedResult, futureResult.get()); + } + + @Test + public void givenGetResourceWithFuture_WhenWaitAndMatchWithPredicate_ShouldReturnSuccess() { + String url = "http://resource"; + Future future = Future.of(() -> getResource(url)); + future.await(); + String s = Match(future).of(Case($(future0 -> future0.isSuccess()), SUCCESS), Case($(), FAILURE)); + + assertEquals(SUCCESS, s); + } + + @Test + public void givenAFailedFuture_WhenWaitAndMatchWithPredicateCheckSuccess_ShouldReturnFailed() { + Future future = Future.failed(new RuntimeException()); + future.await(); + String s = Match(future).of(Case($(future0 -> future0.isSuccess()), SUCCESS), Case($(), FAILURE)); + + assertEquals(FAILURE, s); + } + + @Test + public void givenAFuture_WhenMatchWithFuturePredicate_ShouldReturnSuccess() { + Future future = Future.of(() -> { + Thread.sleep(10); + return 1; + }); + Predicate> predicate = f -> f.exists(i -> i % 2 == 1); + String s = Match(future).of(Case($(predicate), "Even"), Case($(), "Odd")); + + assertEquals("Even", s); + } + + @Test + public void givenAListOfFutureReturnFist3Integers_WhenMatchWithExistEvenNumberPredicate_ShouldReturnSuccess() { + List> futures = getFutureOfFirst3Number(); + Predicate> predicate0 = future -> future.exists(i -> i % 2 == 0); + String s = Match(futures).of(Case($(exists(predicate0)), "Even"), Case($(), "Odd")); + + assertEquals("Even", s); + } + + @Test + public void givenAListOfFutureReturnFist3Integers_WhenMatchWithForAllNumberBiggerThanZeroPredicate_ShouldReturnSuccess() { + List> futures = getFutureOfFirst3Number(); + Predicate> predicate0 = future -> future.exists(i -> i > 0); + String s = Match(futures).of(Case($(forAll(predicate0)), "Positive numbers"), Case($(), "None")); + + assertEquals("Positive numbers", s); + } + + @Test + public void givenAListOfFutureReturnFist3Integers_WhenMatchWithForAllNumberSmallerThanZeroPredicate_ShouldReturnFailed() { + List> futures = getFutureOfFirst3Number(); + Predicate> predicate0 = future -> future.exists(i -> i < 0); + String s = Match(futures).of(Case($(forAll(predicate0)), "Negative numbers"), Case($(), "None")); + + assertEquals("None", s); + } + + private String getResource(String url) throws InterruptedException { + Thread.sleep(10); + return "Content from " + url; + } + + private String getResourceThrowException(String url) { + throw new RuntimeException("Exception when get resource " + url); + } + + private List> getFutureOfFirst3Number() { + List> futures = List.of(Future.of(() -> 1), Future.of(() -> 2), Future.of(() -> 3)); + return futures; + } +} From c5d77f424d614a87a59c042090a33934adaa9093 Mon Sep 17 00:00:00 2001 From: Dassi orleando Date: Mon, 28 Aug 2017 05:55:59 +0100 Subject: [PATCH 9/9] BAEL-1074 Separate difference on date and datetime (#2511) * Different types of bean injection in Spring * Difference between two dates in java * Update README.md * Simple clean of difference between dates * Clean my test article * Improve dates diff: for dates and datetimes --- .../java/com/baeldung/DateDiffUnitTest.java | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/DateDiffUnitTest.java b/core-java/src/test/java/com/baeldung/DateDiffUnitTest.java index 40b7fac30d..324a0d4587 100644 --- a/core-java/src/test/java/com/baeldung/DateDiffUnitTest.java +++ b/core-java/src/test/java/com/baeldung/DateDiffUnitTest.java @@ -1,12 +1,12 @@ package com.baeldung; -import org.joda.time.DateTime; import org.junit.Test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Duration; -import java.time.ZonedDateTime; +import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.Date; import java.util.Locale; import java.util.TimeZone; @@ -30,8 +30,8 @@ public class DateDiffUnitTest { @Test public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() { - ZonedDateTime now = ZonedDateTime.now(); - ZonedDateTime sixDaysBehind = now.minusDays(6); + LocalDate now = LocalDate.now(); + LocalDate sixDaysBehind = now.minusDays(6); Duration duration = Duration.between(now, sixDaysBehind); long diff = Math.abs(duration.toDays()); @@ -40,16 +40,36 @@ public class DateDiffUnitTest { } @Test - public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() { - DateTime now = DateTime.now(); - DateTime sixDaysBehind = now.minusDays(6); + public void givenTwoDateTimesInJava8_whenDifferentiating_thenWeGetSix() { + LocalDateTime now = LocalDateTime.now(); + LocalDateTime sixMinutesBehind = now.minusMinutes(6); - org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind); - long diff = Math.abs(duration.getStandardDays()); + Duration duration = Duration.between(now, sixMinutesBehind); + long diff = Math.abs(duration.toMinutes()); assertEquals(diff, 6); } + @Test + public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() { + org.joda.time.LocalDate now = org.joda.time.LocalDate.now(); + org.joda.time.LocalDate sixDaysBehind = now.minusDays(6); + + org.joda.time.Period period = new org.joda.time.Period(now, sixDaysBehind); + long diff = Math.abs(period.getDays()); + + assertEquals(diff, 6); + } + + @Test + public void givenTwoDateTimesInJodaTime_whenDifferentiating_thenWeGetSix() { + org.joda.time.LocalDateTime now = org.joda.time.LocalDateTime.now(); + org.joda.time.LocalDateTime sixMinutesBehind = now.minusMinutes(6); + + org.joda.time.Period period = new org.joda.time.Period(now, sixMinutesBehind); + long diff = Math.abs(period.getDays()); + } + @Test public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() { hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault()); @@ -59,5 +79,4 @@ public class DateDiffUnitTest { assertEquals(diff, 6); } - } \ No newline at end of file