From a3c47eca35af99a65a39d7775e7fe951be325f9b Mon Sep 17 00:00:00 2001 From: mokhan Date: Wed, 26 Jul 2017 21:01:36 +0530 Subject: [PATCH 1/8] @GetMapping and @PostMapping instead of @RequestMappings change --- .../baeldung/web/log/controller/TaxiFareController.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java b/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java index 28bf07e8a6..c39be88059 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java @@ -6,9 +6,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.baeldung.web.log.data.RateCard; @@ -23,14 +23,14 @@ public class TaxiFareController { private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class); - @RequestMapping(method = RequestMethod.GET, value = "/taxifare/get/") + @GetMapping(value = "/taxifare/get/") @ResponseBody public RateCard getTaxiFare() { LOGGER.debug("getTaxiFare() - START"); return new RateCard(); } - @RequestMapping(method = RequestMethod.POST, value = "/taxifare/calculate/") + @PostMapping(value = "/taxifare/calculate/") @ResponseBody public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) { LOGGER.debug("calculateTaxiFare() - START"); From 0a631ac3269726b3569c94153c6c9e2e78f2e632 Mon Sep 17 00:00:00 2001 From: Seun Matt Date: Thu, 27 Jul 2017 21:59:45 +0100 Subject: [PATCH 2/8] Example Code for BAEL-1048 (#2316) * 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 --- libraries/pom.xml | 6 ++ .../pcollections/PCollectionsUnitTest.java | 91 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index c5e1f64594..a655f5267a 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -448,6 +448,11 @@ byte-buddy-agent ${bytebuddy.version} + + org.pcollections + pcollections + ${pcollections.version} + 0.7.0 @@ -487,5 +492,6 @@ 2.0.0.0 1.6.0 1.7.1 + 2.1.2 diff --git a/libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java b/libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java new file mode 100644 index 0000000000..b2f815074a --- /dev/null +++ b/libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java @@ -0,0 +1,91 @@ +package com.baeldung.pcollections; + +import org.junit.Test; +import org.pcollections.*; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class PCollectionsUnitTest { + + @Test + public void whenEmpty_thenCreateEmptyHashPMap() { + HashPMap pmap = HashTreePMap.empty(); + assertEquals(pmap.size(), 0); + } + + @Test + public void givenKeyValue_whenSingleton_thenCreateNonEmptyHashPMap() { + HashPMap pmap1 = HashTreePMap.singleton("key1", "value1"); + assertEquals(pmap1.size(), 1); + } + + @Test + public void givenExistingHashMap_whenFrom_thenCreateHashPMap() { + Map map = new HashMap(); + map.put("mkey1", "mval1"); + map.put("mkey2", "mval2"); + + HashPMap pmap2 = HashTreePMap.from(map); + assertEquals(pmap2.size(), 2); + } + + @Test + public void whenHashPMapMethods_thenPerformOperations() { + + HashPMap pmap = HashTreePMap.empty(); + pmap = pmap.plus("key1", "value1"); + assertEquals(pmap.size(), 1); + + Map map = new HashMap(); + map.put("key2", "val2"); + map.put("key3", "val3"); + pmap = pmap.plusAll(map); + + assertEquals(pmap.size(), 3); + + pmap = pmap.minus("key1"); + assertFalse(pmap.containsKey("key1")); + + pmap = pmap.minusAll(map.keySet()); + assertEquals(pmap.size(), 0); + + } + + @Test + public void whenTreePVectorMethods_thenPerformOperations() { + TreePVector pVector = TreePVector.empty(); + + pVector = pVector.plus("e1"); + pVector = pVector.plusAll(Arrays.asList("e2", "e3", "e4")); + assertEquals(4, pVector.size()); + + TreePVector pSub = pVector.subList(0, 2); + assertTrue(pSub.contains("e1") && pSub.contains("e2")); + + TreePVector pVW = (TreePVector) pVector.with(0, "e10"); + assertEquals(pVW.get(0), "e10"); + + pVector = pVector.minus("e1"); + TreePVector pV1 = pVector.minusAll(Arrays.asList("e2", "e3")); + assertEquals(pV1.size(), 1); + } + + @Test + public void whenMapPSetMethods_thenPerformOperations() { + + MapPSet pSet = HashTreePSet.empty() + .plusAll(Arrays.asList("e1","e2","e3","e4")); + assertEquals(pSet.size(), 4); + + pSet = pSet.minus("e4"); + assertFalse(pSet.contains("e4")); + + } + +} From 9e193396ef620b41ad305ee6df2110f4a74e8113 Mon Sep 17 00:00:00 2001 From: Tian Baoqiang Date: Fri, 28 Jul 2017 04:58:24 -0500 Subject: [PATCH 3/8] BAEL-1026 (#2322) --- ratpack/pom.xml | 99 ++++++++++--------- .../java/com/baeldung/spring/ArticleList.java | 11 +++ .../main/java/com/baeldung/spring/Config.java | 24 +++++ .../java/com/baeldung/spring/Content.java | 10 ++ .../com/baeldung/spring/EmbedRatpackApp.java | 46 +++++++++ .../baeldung/spring/EmbedSpringBootApp.java | 19 ++++ ratpack/src/main/resources/public/index.html | 10 ++ .../EmbedRatpackAppIntegrationTest.java | 41 ++++++++ 8 files changed, 215 insertions(+), 45 deletions(-) create mode 100644 ratpack/src/main/java/com/baeldung/spring/ArticleList.java create mode 100644 ratpack/src/main/java/com/baeldung/spring/Config.java create mode 100644 ratpack/src/main/java/com/baeldung/spring/Content.java create mode 100644 ratpack/src/main/java/com/baeldung/spring/EmbedRatpackApp.java create mode 100644 ratpack/src/main/java/com/baeldung/spring/EmbedSpringBootApp.java create mode 100644 ratpack/src/main/resources/public/index.html create mode 100644 ratpack/src/test/java/com/baeldung/spring/EmbedRatpackAppIntegrationTest.java diff --git a/ratpack/pom.xml b/ratpack/pom.xml index 8681f5fc10..7a75ec50b7 100644 --- a/ratpack/pom.xml +++ b/ratpack/pom.xml @@ -1,55 +1,64 @@ - 4.0.0 - com.baeldung - ratpack - jar - 1.0-SNAPSHOT - ratpack - http://maven.apache.org + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + 4.0.0 + com.baeldung + ratpack + jar + 1.0-SNAPSHOT + ratpack + http://maven.apache.org - - UTF-8 - 1.8 - 1.8 - + + UTF-8 + 1.8 + 1.8 + 1.4.6 + com.baeldung parent-modules 1.0.0-SNAPSHOT - - - - - io.ratpack - ratpack-core - 1.4.5 - - - io.ratpack - ratpack-hikari - 1.4.5 - - - io.ratpack - ratpack-test - 1.4.5 - - - com.h2database - h2 - 1.4.193 - - + - - ${project.artifactId} - - - src/main/resources - - - + + + + io.ratpack + ratpack-spring-boot-starter + ${ratpack.version} + pom + + + + io.ratpack + ratpack-core + ${ratpack.version} + + + io.ratpack + ratpack-hikari + ${ratpack.version} + + + io.ratpack + ratpack-test + ${ratpack.version} + + + com.h2database + h2 + 1.4.193 + + + + + ${project.artifactId} + + + src/main/resources + + + diff --git a/ratpack/src/main/java/com/baeldung/spring/ArticleList.java b/ratpack/src/main/java/com/baeldung/spring/ArticleList.java new file mode 100644 index 0000000000..b4d50bb3d3 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/spring/ArticleList.java @@ -0,0 +1,11 @@ +package com.baeldung.spring; + +import java.util.List; + +/** + * @author aiet + */ +public interface ArticleList { + + List articles(); +} diff --git a/ratpack/src/main/java/com/baeldung/spring/Config.java b/ratpack/src/main/java/com/baeldung/spring/Config.java new file mode 100644 index 0000000000..ec0d1787e6 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/spring/Config.java @@ -0,0 +1,24 @@ +package com.baeldung.spring; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Arrays; + +/** + * @author aiet + */ +@Configuration +public class Config { + + @Bean + public Content content() { + return () -> "hello baeldung!"; + } + + @Bean + public ArticleList articles() { + return () -> Arrays.asList("Introduction to Ratpack", "Ratpack Google Guice Integration", "Ratpack Spring Boot Integration"); + } + +} diff --git a/ratpack/src/main/java/com/baeldung/spring/Content.java b/ratpack/src/main/java/com/baeldung/spring/Content.java new file mode 100644 index 0000000000..4d01c70cb9 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/spring/Content.java @@ -0,0 +1,10 @@ +package com.baeldung.spring; + +/** + * @author aiet + */ +public interface Content { + + String body(); + +} diff --git a/ratpack/src/main/java/com/baeldung/spring/EmbedRatpackApp.java b/ratpack/src/main/java/com/baeldung/spring/EmbedRatpackApp.java new file mode 100644 index 0000000000..7f3483d676 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/spring/EmbedRatpackApp.java @@ -0,0 +1,46 @@ +package com.baeldung.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import ratpack.func.Action; +import ratpack.handling.Chain; +import ratpack.server.ServerConfig; +import ratpack.spring.config.EnableRatpack; + +/** + * @author aiet + */ +@SpringBootApplication +@EnableRatpack +public class EmbedRatpackApp { + + @Autowired private Content content; + @Autowired private ArticleList list; + + @Bean + public Action hello() { + return chain -> chain.get("hello", ctx -> ctx.render(content.body())); + } + + @Bean + public Action list() { + return chain -> chain.get("list", ctx -> ctx.render(list + .articles() + .toString())); + } + + @Bean + public ServerConfig ratpackServerConfig() { + return ServerConfig + .builder() + .findBaseDir("public") + .build(); + } + + public static void main(String[] args) { + SpringApplication.run(EmbedRatpackApp.class, args); + } + +} diff --git a/ratpack/src/main/java/com/baeldung/spring/EmbedSpringBootApp.java b/ratpack/src/main/java/com/baeldung/spring/EmbedSpringBootApp.java new file mode 100644 index 0000000000..05ff00cbbd --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/spring/EmbedSpringBootApp.java @@ -0,0 +1,19 @@ +package com.baeldung.spring; + +import ratpack.server.RatpackServer; + +import static ratpack.spring.Spring.spring; + +public class EmbedSpringBootApp { + + public static void main(String[] args) throws Exception { + RatpackServer.start(server -> server + .registry(spring(Config.class)) + .handlers(chain -> chain.get(ctx -> ctx.render(ctx + .get(Content.class) + .body())))); + } + +} + + diff --git a/ratpack/src/main/resources/public/index.html b/ratpack/src/main/resources/public/index.html new file mode 100644 index 0000000000..d6573bfb7f --- /dev/null +++ b/ratpack/src/main/resources/public/index.html @@ -0,0 +1,10 @@ + + + + + Special Static Resource + + +This page is static. + + \ No newline at end of file diff --git a/ratpack/src/test/java/com/baeldung/spring/EmbedRatpackAppIntegrationTest.java b/ratpack/src/test/java/com/baeldung/spring/EmbedRatpackAppIntegrationTest.java new file mode 100644 index 0000000000..802fe75d5c --- /dev/null +++ b/ratpack/src/test/java/com/baeldung/spring/EmbedRatpackAppIntegrationTest.java @@ -0,0 +1,41 @@ +package com.baeldung.spring; + +import org.junit.Test; +import ratpack.test.MainClassApplicationUnderTest; + +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +/** + * @author aiet + */ +public class EmbedRatpackAppIntegrationTest { + + MainClassApplicationUnderTest appUnderTest = new MainClassApplicationUnderTest(EmbedRatpackApp.class); + + @Test + public void whenSayHello_thenGotWelcomeMessage() { + assertEquals("hello baeldung!", appUnderTest + .getHttpClient() + .getText("/hello")); + } + + @Test + public void whenRequestList_thenGotArticles() throws IOException { + assertEquals(3, appUnderTest + .getHttpClient() + .getText("/list") + .split(",").length); + } + + @Test + public void whenRequestStaticResource_thenGotStaticContent() { + assertThat(appUnderTest + .getHttpClient() + .getText("/"), containsString("page is static")); + } + +} From 828e5143d133188d46e8570638543b172d611965 Mon Sep 17 00:00:00 2001 From: dimitarsazdovski Date: Fri, 28 Jul 2017 14:33:00 +0200 Subject: [PATCH 4/8] BAEL-907: Code example and tests (#2295) * BAEL-907: Code example and tests * Moved files to libraries module * Moved example to test class * Moved files. Formatted code. Changed object declaring. * Code fixes according to comments --- .../CircularFifoQueueTest.java | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 libraries/src/test/java/com/baeldung/circularfifoqueue/CircularFifoQueueTest.java diff --git a/libraries/src/test/java/com/baeldung/circularfifoqueue/CircularFifoQueueTest.java b/libraries/src/test/java/com/baeldung/circularfifoqueue/CircularFifoQueueTest.java new file mode 100644 index 0000000000..9f670af03c --- /dev/null +++ b/libraries/src/test/java/com/baeldung/circularfifoqueue/CircularFifoQueueTest.java @@ -0,0 +1,159 @@ +package com.baeldung.circularfifoqueue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.collections4.queue.CircularFifoQueue; +import org.junit.Assert; +import org.junit.Test; + +public class CircularFifoQueueTest { + + private static final int DEFAULT_SIZE = 32; + + private static final int FIXED_SIZE = 5; + + private static final int COLLECTION_SIZE = 7; + + private static final String TEST_COLOR = "Red"; + + private static final String TEST_COLOR_BY_INDEX = "Blue"; + + @Test + public void whenUsingDefualtConstructor_correctSizeQueue() { + CircularFifoQueue bits = new CircularFifoQueue<>(); + + Assert.assertEquals(DEFAULT_SIZE, bits.maxSize()); + } + + @Test + public void givenAddElements_whenUsingIntConstructor_correctSizeQueue() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(FIXED_SIZE, colors.maxSize()); + } + + @Test + public void whenUsingCollectionConstructor_correctSizeQueue() { + List days = new ArrayList<>(); + days.add("Monday"); + days.add("Tuesday"); + days.add("Wednesday"); + days.add("Thursday"); + days.add("Friday"); + days.add("Saturday"); + days.add("Sunday"); + + CircularFifoQueue daysOfWeek = new CircularFifoQueue<>(days); + + Assert.assertEquals(COLLECTION_SIZE, daysOfWeek.maxSize()); + } + + @Test + public void givenAddElements_whenGetElement_correctElement() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(TEST_COLOR_BY_INDEX, colors.get(1)); + } + + @Test + public void givenAddElements_whenPollElement_correctElement() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(TEST_COLOR, colors.poll()); + } + + @Test + public void givenAddElements_whenPeekQueue_correctElement() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(TEST_COLOR, colors.peek()); + } + + @Test + public void givenAddElements_whenElementQueue_correctElement() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(TEST_COLOR, colors.element()); + } + + @Test + public void givenAddElements_whenRemoveElement_correctElement() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(TEST_COLOR, colors.remove()); + } + + @Test + public void givenFullQueue_whenClearQueue_getIsEmpty() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + colors.clear(); + + Assert.assertEquals(true, colors.isEmpty()); + } + + @Test + public void givenFullQueue_whenCheckFull_getIsFull() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(false, colors.isFull()); + } + + @Test + public void givenFullQueue_whenAddMoreElements_getIsAtFullCapacity() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + colors.add("Orange"); + colors.add("Violet"); + colors.add("Pink"); + + Assert.assertEquals(true, colors.isAtFullCapacity()); + } + +} From d71f330b09ca2993d27094cb8ad31ead792540b6 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Fri, 28 Jul 2017 17:49:28 +0200 Subject: [PATCH 5/8] Enable GitHub Incremental Builder (#2323) * Enable GIB * Enable GIB --- pom.xml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index b6813a8c18..fa6a3e5673 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,9 @@ UTF-8 refs/heads/master + true + false + false 4.12 1.3 @@ -328,11 +331,11 @@ - + 3.4 + From 9b143b820c7b6e077664552773c7711facefcbd8 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Fri, 28 Jul 2017 18:15:37 +0200 Subject: [PATCH 6/8] Refactor core-java (#2324) --- .../cyclicbarrier/CyclicBarrierExample.java | 30 +++++++++---------- .../concurrent/cyclicbarrier/Task.java | 27 ++++++++--------- .../equalshashcode/entities/Rectangle.java | 2 +- .../executable/ExecutableMavenJar.java | 3 +- 4 files changed, 29 insertions(+), 33 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java b/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java index 85c0cf7680..dd80a7971c 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java +++ b/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java @@ -4,22 +4,20 @@ import java.util.concurrent.CyclicBarrier; public class CyclicBarrierExample { - public void start() { - CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> { - // Task - System.out.println("All previous tasks are completed"); - }); + public void start() { + CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> { + // Task + System.out.println("All previous tasks are completed"); + }); - Thread t1 = new Thread(new Task(cyclicBarrier), "T1"); - Thread t2 = new Thread(new Task(cyclicBarrier), "T2"); - Thread t3 = new Thread(new Task(cyclicBarrier), "T3"); - - if (!cyclicBarrier.isBroken()) { - t1.start(); - t2.start(); - t3.start(); - } - - } + Thread t1 = new Thread(new Task(cyclicBarrier), "T1"); + Thread t2 = new Thread(new Task(cyclicBarrier), "T2"); + Thread t3 = new Thread(new Task(cyclicBarrier), "T3"); + if (!cyclicBarrier.isBroken()) { + t1.start(); + t2.start(); + t3.start(); + } + } } diff --git a/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java b/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java index 4f7801e8c5..cc9ed105dc 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java +++ b/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java @@ -6,20 +6,19 @@ import java.util.concurrent.CyclicBarrier; public class Task implements Runnable { private CyclicBarrier barrier; - - public Task(CyclicBarrier barrier) { - this.barrier = barrier; - } - @Override - public void run() { + public Task(CyclicBarrier barrier) { + this.barrier = barrier; + } + + @Override + public void run() { try { - System.out.println("Thread : "+ Thread.currentThread().getName() + " is waiting"); - barrier.await(); - System.out.println("Thread : "+ Thread.currentThread().getName() + " is released"); - } catch (InterruptedException | BrokenBarrierException e) { - e.printStackTrace(); - } - } - + System.out.println("Thread : " + Thread.currentThread().getName() + " is waiting"); + barrier.await(); + System.out.println("Thread : " + Thread.currentThread().getName() + " is released"); + } catch (InterruptedException | BrokenBarrierException e) { + e.printStackTrace(); + } + } } diff --git a/core-java/src/main/java/org/baeldung/equalshashcode/entities/Rectangle.java b/core-java/src/main/java/org/baeldung/equalshashcode/entities/Rectangle.java index 1e1423f0b3..5e38eb6088 100644 --- a/core-java/src/main/java/org/baeldung/equalshashcode/entities/Rectangle.java +++ b/core-java/src/main/java/org/baeldung/equalshashcode/entities/Rectangle.java @@ -4,7 +4,7 @@ public class Rectangle extends Shape { private double width; private double length; - public Rectangle(double width, double length) { + Rectangle(double width, double length) { this.width = width; this.length = length; } diff --git a/core-java/src/main/java/org/baeldung/executable/ExecutableMavenJar.java b/core-java/src/main/java/org/baeldung/executable/ExecutableMavenJar.java index 09344902b7..d291ac0d3b 100644 --- a/core-java/src/main/java/org/baeldung/executable/ExecutableMavenJar.java +++ b/core-java/src/main/java/org/baeldung/executable/ExecutableMavenJar.java @@ -1,11 +1,10 @@ package org.baeldung.executable; -import javax.swing.JOptionPane; +import javax.swing.*; public class ExecutableMavenJar { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "It worked!", "Executable Jar with Maven", 1); } - } From 7f22e3dc35e033c24b42a6a83f336a49cf1aea40 Mon Sep 17 00:00:00 2001 From: cleversonzanon Date: Fri, 28 Jul 2017 17:51:16 -0300 Subject: [PATCH 7/8] Destructuring Declarations in Kotlin - Cleverson Zanon | cleverson.ssantos1008@gmail.com (#2318) * Different Types of Bean Injection in Spring code * Dataclasses in Kotlin * Revert "Different Types of Bean Injection in Spring code" This reverts commit 4b747726b93a9f6bf76d6518792fc77e0d5c2fc9. * Destructuring Declarations in Kotlin * Corrections on Destructuring Declarations in Kotlin --- .../destructuringdeclarations/Sandbox.kt | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Sandbox.kt b/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Sandbox.kt index 87775f9378..a5018d93c8 100644 --- a/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Sandbox.kt +++ b/kotlin/src/main/kotlin/com/baeldung/destructuringdeclarations/Sandbox.kt @@ -1,11 +1,10 @@ package com.baeldung.destructuringdeclarations import com.baeldung.destructuringdeclarations.Person -import com.baeldung.destructuringdeclarations.Result fun main(args: Array) { - //2.1. Objects + //2.1. Objects val person = Person(1, "Jon Snow", 20) val(id, name, age) = person @@ -13,20 +12,15 @@ fun main(args: Array) { println(name) //Jon Snow println(age) //20 - //The equivalent of line 10 -/* val id = person.component1(); - val name = person.component2(); - val age = person.component3();*/ - //2.2. Functions fun getPersonInfo() = Person(2, "Ned Stark", 45) val(idf, namef, agef) = getPersonInfo() - fun twoValuesReturn(): Result { + fun twoValuesReturn(): Pair { // needed code - return Result(1, "success") + return Pair(1, "success") } // Now, to use this function: @@ -41,15 +35,10 @@ fun main(args: Array) { } //2.4. Underscore and Destructuring in Lambdas - val (_, status2) = twoValuesReturn() + val (_, name2, age2) = person + val (id3, name3) = person map.mapValues { entry -> "${entry.value}!" } map.mapValues { (key, value) -> "$value!" } - //A pair of parameters vs. a destructuring pair -/* { a -> ... } // one parameter - { a, b -> ... } // two parameters - { (a, b) -> ... } // a destructured pair - { (a, b), c -> ... } // a destructured pair and another parameter*/ - } \ No newline at end of file From e8df7f8116ad20a632c19ae97ee5c5ac2be5af96 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 29 Jul 2017 10:06:27 +0200 Subject: [PATCH 8/8] Taxi fare refactor (#2328) --- spring-rest/difference-uri-url-rest/pom.xml | 32 ------------------ .../baeldung/springboot/rest/Greeting.java | 21 ------------ .../springboot/rest/GreetingController.java | 21 ------------ .../rest/SpringBootWebApplication.java | 27 --------------- .../rest/client/ApplicationClient.java | 27 --------------- .../springboot/rest/client/Greeting.java | 33 ------------------- .../rest/test/DifferenceURIURLRESTTest.java | 32 ------------------ .../controller/DataProducerController.java | 2 +- .../com/baeldung/web/log/app/Application.java | 1 - .../log/app/TaxiFareRequestInterceptor.java | 4 +-- .../web/log/config/TaxiFareMVCConfig.java | 5 ++- .../log/controller/TaxiFareController.java | 9 +++-- .../com/baeldung/web/log/data/RateCard.java | 19 ++++++----- .../com/baeldung/web/log/data/TaxiRide.java | 11 ++++--- .../service/TaxiFareCalculatorService.java | 14 +++----- .../org/baeldung/config/MainApplication.java | 1 - .../repository/HeavyResourceRepository.java | 7 ++-- 17 files changed, 34 insertions(+), 232 deletions(-) delete mode 100644 spring-rest/difference-uri-url-rest/pom.xml delete mode 100644 spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/Greeting.java delete mode 100644 spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/GreetingController.java delete mode 100644 spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/SpringBootWebApplication.java delete mode 100644 spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/ApplicationClient.java delete mode 100644 spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/Greeting.java delete mode 100644 spring-rest/difference-uri-url-rest/src/test/java/com/baeldung/springboot/rest/test/DifferenceURIURLRESTTest.java diff --git a/spring-rest/difference-uri-url-rest/pom.xml b/spring-rest/difference-uri-url-rest/pom.xml deleted file mode 100644 index 5ab6e63240..0000000000 --- a/spring-rest/difference-uri-url-rest/pom.xml +++ /dev/null @@ -1,32 +0,0 @@ - - 4.0.0 - org.baeldung.springboot.rest - difference-uri-url-rest - 0.0.1-SNAPSHOT - war - - - 1.8 - - - - org.springframework.boot - spring-boot-starter-parent - 1.5.2.RELEASE - - - - - - org.springframework.boot - spring-boot-starter-web - compile - - - org.springframework.boot - spring-boot-starter-test - test - - - - \ No newline at end of file diff --git a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/Greeting.java b/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/Greeting.java deleted file mode 100644 index cc166587df..0000000000 --- a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/Greeting.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.springboot.rest; - -public class Greeting { - private static final long serialVersionUID = 1L; - - private Integer id = null; - private String content = null; - - public Greeting(Integer id) { - this.id = id; - this.content = "Hello World"; - } - - public Integer getId() { - return id; - } - - public String getContent() { - return content; - } -} \ No newline at end of file diff --git a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/GreetingController.java b/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/GreetingController.java deleted file mode 100644 index 3fca9a1a76..0000000000 --- a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/GreetingController.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.springboot.rest; - -import java.util.concurrent.atomic.AtomicLong; - -import org.springframework.stereotype.Component; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -@RestController("/") -@Component -public class GreetingController { - - private final AtomicLong counter = new AtomicLong(); - - @RequestMapping(value = "/greetings", method = RequestMethod.GET) - public Greeting greeting() { - - return new Greeting(new Integer((int) counter.incrementAndGet())); - } -} \ No newline at end of file diff --git a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/SpringBootWebApplication.java b/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/SpringBootWebApplication.java deleted file mode 100644 index 08d4c2c65e..0000000000 --- a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/SpringBootWebApplication.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.springboot.rest; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.web.support.SpringBootServletInitializer; - -@EnableAutoConfiguration -@SpringBootApplication -public class SpringBootWebApplication extends SpringBootServletInitializer { - - // method for explicit deployment on Application Server - @Override - protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { - return application.sources(SpringBootWebApplication.class); - } - - // run it as standalone JAVA application - public static void main(String[] args) throws Exception { - SpringApplication.run(SpringBootWebApplication.class, args); - } - - //Samples - // http://localhost:8080/greetings - // http://localhost:8989/difference-uri-url-rest/greetings -} \ No newline at end of file diff --git a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/ApplicationClient.java b/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/ApplicationClient.java deleted file mode 100644 index 2d92c890c5..0000000000 --- a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/ApplicationClient.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.springboot.rest.client; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.web.client.RestTemplate; - -public class ApplicationClient { - //private static final Logger log = LoggerFactory.getLogger(ApplicationClient.class); - final static String URI_STRING = "http://localhost:8080/difference-uri-url-rest/greetings"; - - - public ApplicationClient() { - super(); - } - - public Greeting init() { - RestTemplate restTemplate = new RestTemplate(); - Greeting greeting = restTemplate.getForObject(ApplicationClient.URI_STRING, Greeting.class); - //log.info(greeting.toString()); - return greeting; - } - public static void main(String args[]) { - Greeting greeting = new ApplicationClient().init(); - System.out.println(greeting.toString()); - } - -} diff --git a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/Greeting.java b/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/Greeting.java deleted file mode 100644 index 7d1119d155..0000000000 --- a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/Greeting.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.springboot.rest.client; - -import java.io.Serializable; - -public class Greeting implements Serializable { - private static final long serialVersionUID = 1L; - - private Integer id = null; - private String content = null; - - /** Default constructor is mandatory for client */ - public Greeting() { - super(); - } - - public Greeting(Integer id) { - this.id = id; - this.content = "Hello World"; - } - - public Integer getId() { - return id; - } - - public String getContent() { - return content; - } - - @Override - public String toString() { - return "Id: " + getId().toString() + " Content: " + getContent(); - } -} \ No newline at end of file diff --git a/spring-rest/difference-uri-url-rest/src/test/java/com/baeldung/springboot/rest/test/DifferenceURIURLRESTTest.java b/spring-rest/difference-uri-url-rest/src/test/java/com/baeldung/springboot/rest/test/DifferenceURIURLRESTTest.java deleted file mode 100644 index 4397f8e088..0000000000 --- a/spring-rest/difference-uri-url-rest/src/test/java/com/baeldung/springboot/rest/test/DifferenceURIURLRESTTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.springboot.rest.test; - -import static org.junit.Assert.assertNotNull; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.springframework.web.client.RestTemplate; - -import com.baeldung.springboot.rest.client.Greeting; - -public class DifferenceURIURLRESTTest { - final static String URI_STRING = "http://localhost:8080/difference-uri-url-rest/greetings"; - static RestTemplate restTemplate; - Greeting greeting; - - @BeforeClass - public static void setupTest() { - restTemplate = new RestTemplate(); - } - - @Test - public void givenRestTenplate_whenIsNotNull_thenSuccess() { - assertNotNull("Rest Template not null", restTemplate); - } - - @Test - public void givenWiredConstructorParam_whenIsNotNull_thenSuccess() { - greeting = restTemplate.getForObject(URI_STRING, Greeting.class); - assertNotNull("Greeting class is not null", greeting); - } - -} diff --git a/spring-rest/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java b/spring-rest/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java index 6f34bdb9ae..ab233a8b60 100644 --- a/spring-rest/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java +++ b/spring-rest/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java @@ -17,7 +17,7 @@ public class DataProducerController { return "Hello world"; } - @GetMapping(value = "/get-image") + @GetMapping("/get-image") public @ResponseBody byte[] getImage() throws IOException { final InputStream in = getClass().getResourceAsStream("/com/baeldung/produceimage/image.jpg"); return IOUtils.toByteArray(in); diff --git a/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java b/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java index 9bdbbd0d9f..41042008ef 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java @@ -14,5 +14,4 @@ public class Application extends SpringBootServletInitializer { public static void main(final String[] args) { SpringApplication.run(Application.class, args); } - } \ No newline at end of file diff --git a/spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java b/spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java index 831d236edd..b154f3665f 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java @@ -14,11 +14,11 @@ import com.baeldung.web.log.util.RequestLoggingUtil; @Component public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter { - Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class); + private final static Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - String postData = null; + String postData; HttpServletRequest requestCacheWrapperObject = null; try { // Uncomment to produce the stream closed issue diff --git a/spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java b/spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java index fa26706ff0..f433b4f3c7 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java @@ -1,18 +1,17 @@ package com.baeldung.web.log.config; +import com.baeldung.web.log.app.TaxiFareRequestInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import com.baeldung.web.log.app.TaxiFareRequestInterceptor; - @Configuration public class TaxiFareMVCConfig extends WebMvcConfigurerAdapter { @Autowired private TaxiFareRequestInterceptor taxiFareRequestInterceptor; - + @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/**/taxifare/**/"); diff --git a/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java b/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java index c39be88059..7de88d44a8 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java @@ -14,8 +14,9 @@ import org.springframework.web.bind.annotation.ResponseBody; import com.baeldung.web.log.data.RateCard; import com.baeldung.web.log.data.TaxiRide; import com.baeldung.web.log.service.TaxiFareCalculatorService; +import org.springframework.web.bind.annotation.RestController; -@Controller +@RestController public class TaxiFareController { @Autowired @@ -23,15 +24,13 @@ public class TaxiFareController { private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class); - @GetMapping(value = "/taxifare/get/") - @ResponseBody + @GetMapping("/taxifare/get/") public RateCard getTaxiFare() { LOGGER.debug("getTaxiFare() - START"); return new RateCard(); } - @PostMapping(value = "/taxifare/calculate/") - @ResponseBody + @PostMapping("/taxifare/calculate/") public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) { LOGGER.debug("calculateTaxiFare() - START"); String totalFare = taxiFareCalculatorService.calculateFare(taxiRide); diff --git a/spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java b/spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java index 35ae38fd11..c7955b561b 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java @@ -4,25 +4,28 @@ public class RateCard { private String nightSurcharge; private String ratePerMile; - - public RateCard(){ - nightSurcharge="Extra $ 100"; - ratePerMile="$ 10 Per Mile"; + + public RateCard() { + nightSurcharge = "Extra $ 100"; + ratePerMile = "$ 10 Per Mile"; } - - + + public String getNightSurcharge() { return nightSurcharge; } + public void setNightSurcharge(String nightSurcharge) { this.nightSurcharge = nightSurcharge; } + public String getRatePerMile() { return ratePerMile; } + public void setRatePerMile(String ratePerMile) { this.ratePerMile = ratePerMile; } - - + + } diff --git a/spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java b/spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java index 0877cdced4..2e0f33f02b 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java @@ -5,14 +5,15 @@ public class TaxiRide { private Boolean isNightSurcharge; private Long distanceInMile; - public TaxiRide(){} - - public TaxiRide(Boolean isNightSurcharge, Long distanceInMile){ + public TaxiRide() { + } + + public TaxiRide(Boolean isNightSurcharge, Long distanceInMile) { this.isNightSurcharge = isNightSurcharge; this.distanceInMile = distanceInMile; } - - + + public Boolean getIsNightSurcharge() { return isNightSurcharge; } diff --git a/spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java b/spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java index ec9c81187a..1176b31e4c 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java @@ -1,20 +1,14 @@ package com.baeldung.web.log.service; -import org.springframework.stereotype.Service; - import com.baeldung.web.log.data.TaxiRide; +import org.springframework.stereotype.Service; @Service public class TaxiFareCalculatorService { public String calculateFare(TaxiRide taxiRide) { - Long fare = 0l; - if (taxiRide.getIsNightSurcharge()) { - fare = taxiRide.getDistanceInMile() * 10 + 100; - } else { - fare = taxiRide.getDistanceInMile() * 10; - } - return String.valueOf(fare); + return String.valueOf((Long) (taxiRide.getIsNightSurcharge() + ? taxiRide.getDistanceInMile() * 10 + 100 + : taxiRide.getDistanceInMile() * 10)); } - } diff --git a/spring-rest/src/main/java/org/baeldung/config/MainApplication.java b/spring-rest/src/main/java/org/baeldung/config/MainApplication.java index e31fdfaaa9..36b021a537 100644 --- a/spring-rest/src/main/java/org/baeldung/config/MainApplication.java +++ b/spring-rest/src/main/java/org/baeldung/config/MainApplication.java @@ -12,5 +12,4 @@ public class MainApplication extends WebMvcConfigurerAdapter { public static void main(final String[] args) { SpringApplication.run(MainApplication.class, args); } - } \ No newline at end of file diff --git a/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java b/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java index 4ed5c21b83..e4eb6d8875 100644 --- a/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java +++ b/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java @@ -1,11 +1,11 @@ package org.baeldung.repository; -import java.util.Map; - import org.baeldung.web.dto.HeavyResource; import org.baeldung.web.dto.HeavyResourceAddressOnly; -public class HeavyResourceRepository { +import java.util.Map; + +public class HeavyResourceRepository { public void save(HeavyResource heavyResource) { } @@ -21,6 +21,7 @@ public class HeavyResourceRepository { public void save(HeavyResource heavyResource, String id) { } + public void save(HeavyResourceAddressOnly partialUpdate, String id) { }