From 59780e757f3dace7129dbceb7a592a0d1ad26dba Mon Sep 17 00:00:00 2001 From: akeshri Date: Sun, 28 Jun 2020 22:31:13 +0530 Subject: [PATCH 01/13] Create pom.xml --- hexagonal-architecture/pom.xml | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 hexagonal-architecture/pom.xml diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml new file mode 100644 index 0000000000..87e599318c --- /dev/null +++ b/hexagonal-architecture/pom.xml @@ -0,0 +1,46 @@ + + 4.0.0 + + com.article + hexagonal-architecture + 0.0.1-SNAPSHOT + jar + + org.springframework.boot + spring-boot-starter-parent + 1.3.1.RELEASE + + + hexagonal-architecture + http://maven.apache.org + + + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + + org.springframework.boot + spring-boot-starter-test + test + + + com.fasterxml.jackson.dataformat + jackson-dataformat-csv + + + From 0750b9fca237a76e3f15847a08d325488a8b1873 Mon Sep 17 00:00:00 2001 From: akeshri Date: Sun, 28 Jun 2020 22:32:39 +0530 Subject: [PATCH 02/13] Add files via upload --- .../baeldung/hexagonal/architecture/App.java | 18 ++++ .../hexagonal/architecture/ConsoleApp.java | 48 ++++++++++ .../controller/ProductController.java | 52 +++++++++++ .../architecture/dtos/ProductDto.java | 71 +++++++++++++++ .../hexagonal/architecture/model/Product.java | 87 +++++++++++++++++++ .../repository/ProductRepository.java | 14 +++ .../architecture/service/ProductService.java | 21 +++++ .../service/ProductServiceImpl.java | 44 ++++++++++ .../resources/application-batch.properties | 9 ++ .../resources/application-test.properties | 8 ++ .../src/main/resources/application.properties | 8 ++ .../src/main/resources/db/PRODUCT.sql | 9 ++ .../service/ProductServiceTest.java | 43 +++++++++ 13 files changed, 432 insertions(+) create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java create mode 100644 hexagonal-architecture/src/main/resources/application-batch.properties create mode 100644 hexagonal-architecture/src/main/resources/application-test.properties create mode 100644 hexagonal-architecture/src/main/resources/application.properties create mode 100644 hexagonal-architecture/src/main/resources/db/PRODUCT.sql create mode 100644 hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java new file mode 100644 index 0000000000..3563e3a0ec --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java @@ -0,0 +1,18 @@ +package com.baeldung.hexagonal.architecture; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author AshwiniKeshri + * + */ + +@SpringBootApplication +public class App +{ + public static void main( String[] args ) + { + SpringApplication.run(App.class, args); + } +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java new file mode 100644 index 0000000000..70edb8f9ed --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java @@ -0,0 +1,48 @@ +package com.baeldung.hexagonal.architecture; + +import java.io.File; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; +import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; + +import com.baeldung.hexagonal.architecture.model.Product; +import com.baeldung.hexagonal.architecture.service.ProductService; +import com.fasterxml.jackson.dataformat.csv.CsvMapper; +import com.fasterxml.jackson.dataformat.csv.CsvSchema; + +/** + * @author AshwiniKeshri + * + */ + +@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,WebMvcAutoConfiguration.class}) +public class ConsoleApp implements CommandLineRunner { + @Autowired + private ProductService productService; + + public static void main(String[] args) { + SpringApplication.run(ConsoleApp.class, args); + } + + @Override + public void run(String... args) throws Exception { + String filePath = ""; + if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) + && (filePath = args[1]).length() > 0) { + File sourceFile = new File(filePath); + if (sourceFile.exists()) { + CsvMapper mapper = new CsvMapper(); + List products = mapper.readerFor(Product.class).with(CsvSchema.emptySchema().withHeader()) + .readValues(sourceFile).readAll(); + productService.saveAll(products); + } + + } + + } +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java new file mode 100644 index 0000000000..6645c379c2 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java @@ -0,0 +1,52 @@ +package com.baeldung.hexagonal.architecture.controller; + +import java.util.List; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +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.RestController; + +import com.baeldung.hexagonal.architecture.dtos.ProductDto; +import com.baeldung.hexagonal.architecture.model.Product; +import com.baeldung.hexagonal.architecture.service.ProductService; + +/** + * @author AshwiniKeshri + * + */ + +@RestController +@RequestMapping("api/v1/product") +public class ProductController { + + @Autowired + private ProductService productService; + + @RequestMapping(value = "/all", method = RequestMethod.GET) + public List list() { + return productService.findAll().stream().map(p -> new ProductDto(p)).collect(Collectors.toList()); + } + + @RequestMapping(value = "/{productId}", method = RequestMethod.GET) + public ProductDto get(@PathVariable long productId) { + Product p = productService.findById(productId); + return p != null ? new ProductDto(p) : null; + } + + @RequestMapping(value = "/add", method = RequestMethod.POST) + public ProductDto create(@RequestBody ProductDto product) { + Product p = new Product(); + p.setDescription(product.getDescription()); + p.setName(product.getName()); + p.setPrice(product.getPrice()); + p.setQuantity(product.getQuantity()); + Long id = productService.create(p); + product.setId(id); + return product; + } + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java new file mode 100644 index 0000000000..336631fb10 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java @@ -0,0 +1,71 @@ +package com.baeldung.hexagonal.architecture.dtos; + +import com.baeldung.hexagonal.architecture.model.Product; +/** + * @author AshwiniKeshri + * + */ +public class ProductDto { + + private Long id; + + private String name; + + private Long quantity; + + private Double price; + + private String description; + + public ProductDto() {} + + public ProductDto(Product product) { + this.description = product.getDescription(); + this.id = product.getId(); + this.name = product.getName(); + this.price = product.getPrice(); + this.quantity = product.getQuantity(); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java new file mode 100644 index 0000000000..dec4548283 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java @@ -0,0 +1,87 @@ +/** + * + */ +package com.baeldung.hexagonal.architecture.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author AshwiniKeshri + * + */ + +@Entity +@Table(name = "PRODUCT") +public class Product implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = 4000353732860709995L; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name="NAME") + private String name; + + @Column(name = "QUANTITY") + private Long quantity; + + @Column(name = "PRICE") + private Double price; + + @Column(name = "DESCRIPTION") + private String description; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity > 0 ? quantity : 0; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price == null ? 0.0 : price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java new file mode 100644 index 0000000000..76c888ab59 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java @@ -0,0 +1,14 @@ +package com.baeldung.hexagonal.architecture.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.baeldung.hexagonal.architecture.model.Product; + +/** + * @author AshwiniKeshri + * + */ + +public interface ProductRepository extends JpaRepository{ + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java new file mode 100644 index 0000000000..b1d05a7db4 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java @@ -0,0 +1,21 @@ +package com.baeldung.hexagonal.architecture.service; + +import java.util.List; + +import com.baeldung.hexagonal.architecture.model.Product; + +/** + * @author AshwiniKeshri + * + */ + +public interface ProductService { + + List findAll(); + + Product findById(long id); + + Long create(Product product); + + void saveAll(List products); +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java new file mode 100644 index 0000000000..1005b5753d --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java @@ -0,0 +1,44 @@ +package com.baeldung.hexagonal.architecture.service; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.hexagonal.architecture.model.Product; +import com.baeldung.hexagonal.architecture.repository.ProductRepository; + +/** + * @author AshwiniKeshri + * + */ + +@Service +public class ProductServiceImpl implements ProductService { + + private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); + + @Autowired + private ProductRepository productRepository; + + public List findAll() { + return productRepository.findAll(); + } + + public Product findById(long id) { + return productRepository.findOne(id); + } + + public Long create(Product product) { + product = productRepository.saveAndFlush(product); + return product.getId(); + } + + @Override + public void saveAll(List products) { + productRepository.save(products); + } + +} diff --git a/hexagonal-architecture/src/main/resources/application-batch.properties b/hexagonal-architecture/src/main/resources/application-batch.properties new file mode 100644 index 0000000000..8c83d19f74 --- /dev/null +++ b/hexagonal-architecture/src/main/resources/application-batch.properties @@ -0,0 +1,9 @@ +spring.main.web-environment=false +spring.jpa.hibernate.ddl-auto=false; +spring.h2.console.enabled=true +spring.h2.console.path=/h2 + +spring.datasource.url=jdbc:h2:file:~/hexagonal +spring.datasource.username=sa +spring.datasource.password= +spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/application-test.properties b/hexagonal-architecture/src/main/resources/application-test.properties new file mode 100644 index 0000000000..701313a878 --- /dev/null +++ b/hexagonal-architecture/src/main/resources/application-test.properties @@ -0,0 +1,8 @@ +spring.jpa.hibernate.ddl-auto=false; +spring.h2.console.enabled=true +spring.h2.console.path=/h2 + +spring.datasource.url=jdbc:h2:file:~/hexagonal_test +spring.datasource.username=sa +spring.datasource.password= +spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/application.properties b/hexagonal-architecture/src/main/resources/application.properties new file mode 100644 index 0000000000..14c80a1af4 --- /dev/null +++ b/hexagonal-architecture/src/main/resources/application.properties @@ -0,0 +1,8 @@ +spring.jpa.hibernate.ddl-auto=false; +spring.h2.console.enabled=true +spring.h2.console.path=/h2 + +spring.datasource.url=jdbc:h2:file:~/hexagonal +spring.datasource.username=sa +spring.datasource.password= +spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/db/PRODUCT.sql b/hexagonal-architecture/src/main/resources/db/PRODUCT.sql new file mode 100644 index 0000000000..a0fef3a710 --- /dev/null +++ b/hexagonal-architecture/src/main/resources/db/PRODUCT.sql @@ -0,0 +1,9 @@ +CREATE TABLE PRODUCT( + ID INT AUTO_INCREMENT, + NAME VARCHAR(255), + QUANTITY INTEGER, + PRICE DOUBLE, + DESCRIPTION VARCHAR(1000), +); + +INSERT INTO PRODUCT(NAME,QUANTITY,PRICE,DESCRIPTION) VALUES ('iPhone 11 Pro',10,300,'First triple camera system'); \ No newline at end of file diff --git a/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java b/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java new file mode 100644 index 0000000000..021fdf1289 --- /dev/null +++ b/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java @@ -0,0 +1,43 @@ +/** + * + */ +package com.baeldung.hexagonal.architecture.service; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.PropertySources; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.hexagonal.architecture.App; +import com.baeldung.hexagonal.architecture.model.Product; +import com.baeldung.hexagonal.architecture.service.ProductService; + +/** + * @author AshwiniKeshri + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(App.class) +@ActiveProfiles(value = "test") +public class ProductServiceTest { + + @Autowired + private ProductService productService; + + @Test + public void testCreateProduct() { + Product product = new Product(); + product.setDescription("test product"); + product.setName("Product1"); + product.setPrice(10.0); + product.setQuantity(100l); + Long id = productService.create(product); + Assert.assertTrue(id >0); + } + +} From 854fea2f739d08bc2db811efa2d7a8285faf5110 Mon Sep 17 00:00:00 2001 From: akeshri Date: Sun, 28 Jun 2020 22:51:35 +0530 Subject: [PATCH 03/13] Create README.md Read me changes --- hexagonal-architecture/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 hexagonal-architecture/README.md diff --git a/hexagonal-architecture/README.md b/hexagonal-architecture/README.md new file mode 100644 index 0000000000..4dd10368e0 --- /dev/null +++ b/hexagonal-architecture/README.md @@ -0,0 +1,17 @@ +# Hexagonal Architecture +A quick and practical example of Hexagonal Architecture using Spring boot. + +This application is using h2 database,which can be accessible http:/localhost:8080/h2 + +Main Application schema : hexagonal + +Test Application Schema : hexagonal_test + +1. Rest Api : execute [App](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java) + - Get All products : http://localhost:8080/api/v1/product/all + - Get product by id : http://localhost:8080/api/v1/product/{productId} + - Add a product : http://localhost:8080/api/v1/product/add + For more detail refer [ProductController](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java) + +2. Batch processing : We need to configure active profile as batch i.e. -Dspring.profiles.active=batch and execute [ConsoleApp](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java) +3. Test case : [ProductServiceTest](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java) From 31b4bb0dcf56935938a4e5044204d9d1eb8738af Mon Sep 17 00:00:00 2001 From: akeshri Date: Fri, 3 Jul 2020 22:32:44 +0530 Subject: [PATCH 04/13] 1.fixed code format 2. resolved build issue --- hexagonal-architecture/pom.xml | 2 + .../article/hexagonal/architecture/App.java | 16 ++++ .../hexagonal/architecture/ConsoleApp.java | 50 +++++++++++ .../controller/ProductController.java | 54 ++++++++++++ .../architecture/dtos/ProductDto.java | 72 ++++++++++++++++ .../hexagonal/architecture/model/Product.java | 85 +++++++++++++++++++ .../repository/ProductRepository.java | 14 +++ .../architecture/service/ProductService.java | 21 +++++ .../service/ProductServiceImpl.java | 44 ++++++++++ .../service/ProductServiceTest.java | 43 ++++++++++ 10 files changed, 401 insertions(+) create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java create mode 100644 hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml index 87e599318c..d014617639 100644 --- a/hexagonal-architecture/pom.xml +++ b/hexagonal-architecture/pom.xml @@ -18,6 +18,8 @@ UTF-8 + 1.8 + 1.8 diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java new file mode 100644 index 0000000000..ebc661bfdb --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java @@ -0,0 +1,16 @@ +package com.article.hexagonal.architecture; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author AshwiniKeshri + * + */ + +@SpringBootApplication +public class App { + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java new file mode 100644 index 0000000000..0024438737 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java @@ -0,0 +1,50 @@ +package com.article.hexagonal.architecture; + +import java.io.File; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; +import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; + +import com.article.hexagonal.architecture.model.Product; +import com.article.hexagonal.architecture.service.ProductService; +import com.fasterxml.jackson.dataformat.csv.CsvMapper; +import com.fasterxml.jackson.dataformat.csv.CsvSchema; + +/** + * @author AshwiniKeshri + * + */ + +@SpringBootApplication(exclude = { EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class }) +public class ConsoleApp implements CommandLineRunner { + @Autowired + private ProductService productService; + + public static void main(String[] args) { + SpringApplication.run(ConsoleApp.class, args); + } + + @Override + public void run(String... args) throws Exception { + String filePath = ""; + if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) && (filePath = args[1]).length() > 0) { + File sourceFile = new File(filePath); + if (sourceFile.exists()) { + CsvMapper mapper = new CsvMapper(); + List products = mapper.readerFor(Product.class) + .with(CsvSchema.emptySchema() + .withHeader()) + . readValues(sourceFile) + .readAll(); + productService.saveAll(products); + } + + } + + } +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java new file mode 100644 index 0000000000..66372980d0 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java @@ -0,0 +1,54 @@ +package com.article.hexagonal.architecture.controller; + +import java.util.List; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +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.RestController; + +import com.article.hexagonal.architecture.dtos.ProductDto; +import com.article.hexagonal.architecture.model.Product; +import com.article.hexagonal.architecture.service.ProductService; + +/** + * @author AshwiniKeshri + * + */ + +@RestController +@RequestMapping("api/v1/product") +public class ProductController { + + @Autowired + private ProductService productService; + + @RequestMapping(value = "/all", method = RequestMethod.GET) + public List list() { + productService.findAll().stream().map(x->x.getDescription()) + return null; + //return productService.findAll().stream().map(p -> new ProductDto(p)).collect(Collectors.toList()); + } + + @RequestMapping(value = "/{productId}", method = RequestMethod.GET) + public ProductDto get(@PathVariable long productId) { + Product p = productService.findById(productId); + return p != null ? new ProductDto(p) : null; + } + + @RequestMapping(value = "/add", method = RequestMethod.POST) + public ProductDto create(@RequestBody ProductDto product) { + Product p = new Product(); + p.setDescription(product.getDescription()); + p.setName(product.getName()); + p.setPrice(product.getPrice()); + p.setQuantity(product.getQuantity()); + Long id = productService.create(p); + product.setId(id); + return product; + } + +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java new file mode 100644 index 0000000000..209ae69b0a --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java @@ -0,0 +1,72 @@ +package com.article.hexagonal.architecture.dtos; + +import com.article.hexagonal.architecture.model.Product; + +/** + * @author AshwiniKeshri + * + */ +public class ProductDto { + + private Long id; + + private String name; + + private Long quantity; + + private Double price; + + private String description; + + public ProductDto() { + } + + public ProductDto(Product product) { + this.description = product.getDescription(); + this.id = product.getId(); + this.name = product.getName(); + this.price = product.getPrice(); + this.quantity = product.getQuantity(); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java new file mode 100644 index 0000000000..f0f95d4d11 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java @@ -0,0 +1,85 @@ +/** + * + */ +package com.article.hexagonal.architecture.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author AshwiniKeshri + * + */ + +@Entity +@Table(name = "PRODUCT") +public class Product implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 4000353732860709995L; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name = "NAME") + private String name; + + @Column(name = "QUANTITY") + private Long quantity; + + @Column(name = "PRICE") + private Double price; + + @Column(name = "DESCRIPTION") + private String description; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity > 0 ? quantity : 0; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price == null ? 0.0 : price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java new file mode 100644 index 0000000000..fec151780c --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java @@ -0,0 +1,14 @@ +package com.article.hexagonal.architecture.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.article.hexagonal.architecture.model.Product; + +/** + * @author AshwiniKeshri + * + */ + +public interface ProductRepository extends JpaRepository { + +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java new file mode 100644 index 0000000000..5ed1e7de96 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java @@ -0,0 +1,21 @@ +package com.article.hexagonal.architecture.service; + +import java.util.List; + +import com.article.hexagonal.architecture.model.Product; + +/** + * @author AshwiniKeshri + * + */ + +public interface ProductService { + + List findAll(); + + Product findById(long id); + + Long create(Product product); + + void saveAll(List products); +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java new file mode 100644 index 0000000000..ccd1599392 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java @@ -0,0 +1,44 @@ +package com.article.hexagonal.architecture.service; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.article.hexagonal.architecture.model.Product; +import com.article.hexagonal.architecture.repository.ProductRepository; + +/** + * @author AshwiniKeshri + * + */ + +@Service +public class ProductServiceImpl implements ProductService { + + private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); + + @Autowired + private ProductRepository productRepository; + + public List findAll() { + return productRepository.findAll(); + } + + public Product findById(long id) { + return productRepository.findOne(id); + } + + public Long create(Product product) { + product = productRepository.saveAndFlush(product); + return product.getId(); + } + + @Override + public void saveAll(List products) { + productRepository.save(products); + } + +} diff --git a/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java b/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java new file mode 100644 index 0000000000..6635fef2da --- /dev/null +++ b/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java @@ -0,0 +1,43 @@ +/** + * + */ +package com.article.hexagonal.architecture.service; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.PropertySources; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.article.hexagonal.architecture.App; +import com.article.hexagonal.architecture.model.Product; +import com.article.hexagonal.architecture.service.ProductService; + +/** + * @author AshwiniKeshri + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(App.class) +@ActiveProfiles(value = "test") +public class ProductServiceTest { + + @Autowired + private ProductService productService; + + @Test + public void testCreateProduct() { + Product product = new Product(); + product.setDescription("test product"); + product.setName("Product1"); + product.setPrice(10.0); + product.setQuantity(100l); + Long id = productService.create(product); + Assert.assertTrue(id > 0); + } + +} From 188436428f6a3ca0792b9284ecbdf9696f5e167c Mon Sep 17 00:00:00 2001 From: akeshri Date: Sat, 4 Jul 2020 00:10:07 +0530 Subject: [PATCH 05/13] remove duplicate code --- .../article/hexagonal/architecture/App.java | 16 ---- .../hexagonal/architecture/ConsoleApp.java | 50 ----------- .../controller/ProductController.java | 54 ------------ .../architecture/dtos/ProductDto.java | 72 ---------------- .../hexagonal/architecture/model/Product.java | 85 ------------------- .../repository/ProductRepository.java | 14 --- .../architecture/service/ProductService.java | 21 ----- .../service/ProductServiceImpl.java | 44 ---------- .../service/ProductServiceTest.java | 43 ---------- 9 files changed, 399 deletions(-) delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java delete mode 100644 hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java deleted file mode 100644 index ebc661bfdb..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.article.hexagonal.architecture; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -/** - * @author AshwiniKeshri - * - */ - -@SpringBootApplication -public class App { - public static void main(String[] args) { - SpringApplication.run(App.class, args); - } -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java deleted file mode 100644 index 0024438737..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.article.hexagonal.architecture; - -import java.io.File; -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; -import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; - -import com.article.hexagonal.architecture.model.Product; -import com.article.hexagonal.architecture.service.ProductService; -import com.fasterxml.jackson.dataformat.csv.CsvMapper; -import com.fasterxml.jackson.dataformat.csv.CsvSchema; - -/** - * @author AshwiniKeshri - * - */ - -@SpringBootApplication(exclude = { EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class }) -public class ConsoleApp implements CommandLineRunner { - @Autowired - private ProductService productService; - - public static void main(String[] args) { - SpringApplication.run(ConsoleApp.class, args); - } - - @Override - public void run(String... args) throws Exception { - String filePath = ""; - if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) && (filePath = args[1]).length() > 0) { - File sourceFile = new File(filePath); - if (sourceFile.exists()) { - CsvMapper mapper = new CsvMapper(); - List products = mapper.readerFor(Product.class) - .with(CsvSchema.emptySchema() - .withHeader()) - . readValues(sourceFile) - .readAll(); - productService.saveAll(products); - } - - } - - } -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java deleted file mode 100644 index 66372980d0..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.article.hexagonal.architecture.controller; - -import java.util.List; -import java.util.stream.Collectors; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; -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.RestController; - -import com.article.hexagonal.architecture.dtos.ProductDto; -import com.article.hexagonal.architecture.model.Product; -import com.article.hexagonal.architecture.service.ProductService; - -/** - * @author AshwiniKeshri - * - */ - -@RestController -@RequestMapping("api/v1/product") -public class ProductController { - - @Autowired - private ProductService productService; - - @RequestMapping(value = "/all", method = RequestMethod.GET) - public List list() { - productService.findAll().stream().map(x->x.getDescription()) - return null; - //return productService.findAll().stream().map(p -> new ProductDto(p)).collect(Collectors.toList()); - } - - @RequestMapping(value = "/{productId}", method = RequestMethod.GET) - public ProductDto get(@PathVariable long productId) { - Product p = productService.findById(productId); - return p != null ? new ProductDto(p) : null; - } - - @RequestMapping(value = "/add", method = RequestMethod.POST) - public ProductDto create(@RequestBody ProductDto product) { - Product p = new Product(); - p.setDescription(product.getDescription()); - p.setName(product.getName()); - p.setPrice(product.getPrice()); - p.setQuantity(product.getQuantity()); - Long id = productService.create(p); - product.setId(id); - return product; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java deleted file mode 100644 index 209ae69b0a..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.article.hexagonal.architecture.dtos; - -import com.article.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ -public class ProductDto { - - private Long id; - - private String name; - - private Long quantity; - - private Double price; - - private String description; - - public ProductDto() { - } - - public ProductDto(Product product) { - this.description = product.getDescription(); - this.id = product.getId(); - this.name = product.getName(); - this.price = product.getPrice(); - this.quantity = product.getQuantity(); - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Long getQuantity() { - return quantity; - } - - public void setQuantity(Long quantity) { - this.quantity = quantity; - } - - public Double getPrice() { - return price; - } - - public void setPrice(Double price) { - this.price = price; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java deleted file mode 100644 index f0f95d4d11..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java +++ /dev/null @@ -1,85 +0,0 @@ -/** - * - */ -package com.article.hexagonal.architecture.model; - -import java.io.Serializable; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; - -/** - * @author AshwiniKeshri - * - */ - -@Entity -@Table(name = "PRODUCT") -public class Product implements Serializable { - - /** - * - */ - private static final long serialVersionUID = 4000353732860709995L; - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - - @Column(name = "NAME") - private String name; - - @Column(name = "QUANTITY") - private Long quantity; - - @Column(name = "PRICE") - private Double price; - - @Column(name = "DESCRIPTION") - private String description; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Long getQuantity() { - return quantity; - } - - public void setQuantity(Long quantity) { - this.quantity = quantity > 0 ? quantity : 0; - } - - public Double getPrice() { - return price; - } - - public void setPrice(Double price) { - this.price = price == null ? 0.0 : price; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java deleted file mode 100644 index fec151780c..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.article.hexagonal.architecture.repository; - -import org.springframework.data.jpa.repository.JpaRepository; - -import com.article.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ - -public interface ProductRepository extends JpaRepository { - -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java deleted file mode 100644 index 5ed1e7de96..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.article.hexagonal.architecture.service; - -import java.util.List; - -import com.article.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ - -public interface ProductService { - - List findAll(); - - Product findById(long id); - - Long create(Product product); - - void saveAll(List products); -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java deleted file mode 100644 index ccd1599392..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.article.hexagonal.architecture.service; - -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.article.hexagonal.architecture.model.Product; -import com.article.hexagonal.architecture.repository.ProductRepository; - -/** - * @author AshwiniKeshri - * - */ - -@Service -public class ProductServiceImpl implements ProductService { - - private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); - - @Autowired - private ProductRepository productRepository; - - public List findAll() { - return productRepository.findAll(); - } - - public Product findById(long id) { - return productRepository.findOne(id); - } - - public Long create(Product product) { - product = productRepository.saveAndFlush(product); - return product.getId(); - } - - @Override - public void saveAll(List products) { - productRepository.save(products); - } - -} diff --git a/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java b/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java deleted file mode 100644 index 6635fef2da..0000000000 --- a/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * - */ -package com.article.hexagonal.architecture.service; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.context.annotation.PropertySources; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.article.hexagonal.architecture.App; -import com.article.hexagonal.architecture.model.Product; -import com.article.hexagonal.architecture.service.ProductService; - -/** - * @author AshwiniKeshri - * - */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(App.class) -@ActiveProfiles(value = "test") -public class ProductServiceTest { - - @Autowired - private ProductService productService; - - @Test - public void testCreateProduct() { - Product product = new Product(); - product.setDescription("test product"); - product.setName("Product1"); - product.setPrice(10.0); - product.setQuantity(100l); - Long id = productService.create(product); - Assert.assertTrue(id > 0); - } - -} From 1ea3421381332d055fac6695003a2f015cc2c7bd Mon Sep 17 00:00:00 2001 From: akeshri Date: Sun, 5 Jul 2020 21:33:50 +0530 Subject: [PATCH 06/13] fixed code formatting issue --- .../baeldung/hexagonal/architecture/App.java | 6 +- .../hexagonal/architecture/ConsoleApp.java | 42 +++---- .../controller/ProductController.java | 47 ++++---- .../architecture/dtos/ProductDto.java | 103 ++++++++--------- .../hexagonal/architecture/model/Product.java | 104 +++++++++--------- .../repository/ProductRepository.java | 2 +- .../architecture/service/ProductService.java | 14 +-- .../service/ProductServiceImpl.java | 38 +++---- .../service/ProductServiceTest.java | 28 ++--- 9 files changed, 193 insertions(+), 191 deletions(-) diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java index 3563e3a0ec..29dbec470f 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java @@ -9,10 +9,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; */ @SpringBootApplication -public class App -{ - public static void main( String[] args ) - { +public class App { + public static void main(String[] args) { SpringApplication.run(App.class, args); } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java index 70edb8f9ed..6aef791893 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java @@ -20,29 +20,31 @@ import com.fasterxml.jackson.dataformat.csv.CsvSchema; * */ -@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,WebMvcAutoConfiguration.class}) +@SpringBootApplication(exclude = { EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class }) public class ConsoleApp implements CommandLineRunner { - @Autowired - private ProductService productService; + @Autowired + private ProductService productService; - public static void main(String[] args) { - SpringApplication.run(ConsoleApp.class, args); - } + public static void main(String[] args) { + SpringApplication.run(ConsoleApp.class, args); + } - @Override - public void run(String... args) throws Exception { - String filePath = ""; - if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) - && (filePath = args[1]).length() > 0) { - File sourceFile = new File(filePath); - if (sourceFile.exists()) { - CsvMapper mapper = new CsvMapper(); - List products = mapper.readerFor(Product.class).with(CsvSchema.emptySchema().withHeader()) - .readValues(sourceFile).readAll(); - productService.saveAll(products); - } + @Override + public void run(String... args) throws Exception { + String filePath = ""; + if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) && (filePath = args[1]).length() > 0) { + File sourceFile = new File(filePath); + if (sourceFile.exists()) { + CsvMapper mapper = new CsvMapper(); + List products = mapper.readerFor(Product.class) + .with(CsvSchema.emptySchema() + .withHeader()) + . readValues(sourceFile) + .readAll(); + productService.saveAll(products); + } - } + } - } + } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java index 6645c379c2..f54d3268df 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java @@ -23,30 +23,33 @@ import com.baeldung.hexagonal.architecture.service.ProductService; @RequestMapping("api/v1/product") public class ProductController { - @Autowired - private ProductService productService; + @Autowired + private ProductService productService; - @RequestMapping(value = "/all", method = RequestMethod.GET) - public List list() { - return productService.findAll().stream().map(p -> new ProductDto(p)).collect(Collectors.toList()); - } + @RequestMapping(value = "/all", method = RequestMethod.GET) + public List list() { + return productService.findAll() + .stream() + .map(p -> new ProductDto(p)) + .collect(Collectors.toList()); + } - @RequestMapping(value = "/{productId}", method = RequestMethod.GET) - public ProductDto get(@PathVariable long productId) { - Product p = productService.findById(productId); - return p != null ? new ProductDto(p) : null; - } + @RequestMapping(value = "/{productId}", method = RequestMethod.GET) + public ProductDto get(@PathVariable long productId) { + Product p = productService.findById(productId); + return p != null ? new ProductDto(p) : null; + } - @RequestMapping(value = "/add", method = RequestMethod.POST) - public ProductDto create(@RequestBody ProductDto product) { - Product p = new Product(); - p.setDescription(product.getDescription()); - p.setName(product.getName()); - p.setPrice(product.getPrice()); - p.setQuantity(product.getQuantity()); - Long id = productService.create(p); - product.setId(id); - return product; - } + @RequestMapping(value = "/add", method = RequestMethod.POST) + public ProductDto create(@RequestBody ProductDto product) { + Product p = new Product(); + p.setDescription(product.getDescription()); + p.setName(product.getName()); + p.setPrice(product.getPrice()); + p.setQuantity(product.getQuantity()); + Long id = productService.create(p); + product.setId(id); + return product; + } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java index 336631fb10..51692f56aa 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java @@ -1,71 +1,72 @@ package com.baeldung.hexagonal.architecture.dtos; import com.baeldung.hexagonal.architecture.model.Product; + /** * @author AshwiniKeshri * */ public class ProductDto { - private Long id; - - private String name; - - private Long quantity; - - private Double price; - - private String description; - - public ProductDto() {} - - public ProductDto(Product product) { - this.description = product.getDescription(); - this.id = product.getId(); - this.name = product.getName(); - this.price = product.getPrice(); - this.quantity = product.getQuantity(); - } + private Long id; - public Long getId() { - return id; - } + private String name; - public void setId(Long id) { - this.id = id; - } + private Long quantity; - public String getName() { - return name; - } + private Double price; - public void setName(String name) { - this.name = name; - } + private String description; - public Long getQuantity() { - return quantity; - } + public ProductDto() { + } - public void setQuantity(Long quantity) { - this.quantity = quantity; - } + public ProductDto(Product product) { + this.description = product.getDescription(); + this.id = product.getId(); + this.name = product.getName(); + this.price = product.getPrice(); + this.quantity = product.getQuantity(); + } - public Double getPrice() { - return price; - } + public Long getId() { + return id; + } - public void setPrice(Double price) { - this.price = price; - } + public void setId(Long id) { + this.id = id; + } - public String getDescription() { - return description; - } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } - public void setDescription(String description) { - this.description = description; - } - - } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java index dec4548283..697ff68b50 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java @@ -19,69 +19,67 @@ import javax.persistence.Table; @Entity @Table(name = "PRODUCT") -public class Product implements Serializable{ +public class Product implements Serializable { - /** - * - */ - private static final long serialVersionUID = 4000353732860709995L; + /** + * + */ + private static final long serialVersionUID = 4000353732860709995L; - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - - @Column(name="NAME") - private String name; - - @Column(name = "QUANTITY") - private Long quantity; - - @Column(name = "PRICE") - private Double price; - - @Column(name = "DESCRIPTION") - private String description; + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; - public Long getId() { - return id; - } + @Column(name = "NAME") + private String name; - public void setId(Long id) { - this.id = id; - } + @Column(name = "QUANTITY") + private Long quantity; - public String getName() { - return name; - } + @Column(name = "PRICE") + private Double price; - public void setName(String name) { - this.name = name; - } + @Column(name = "DESCRIPTION") + private String description; - public Long getQuantity() { - return quantity; - } + public Long getId() { + return id; + } - public void setQuantity(Long quantity) { - this.quantity = quantity > 0 ? quantity : 0; - } + public void setId(Long id) { + this.id = id; + } - public Double getPrice() { - return price; - } + public String getName() { + return name; + } - public void setPrice(Double price) { - this.price = price == null ? 0.0 : price; - } + public void setName(String name) { + this.name = name; + } - public String getDescription() { - return description; - } + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity > 0 ? quantity : 0; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price == null ? 0.0 : price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } - public void setDescription(String description) { - this.description = description; - } - - - } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java index 76c888ab59..ec50e86dc3 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java @@ -9,6 +9,6 @@ import com.baeldung.hexagonal.architecture.model.Product; * */ -public interface ProductRepository extends JpaRepository{ +public interface ProductRepository extends JpaRepository { } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java index b1d05a7db4..4844723140 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java @@ -11,11 +11,11 @@ import com.baeldung.hexagonal.architecture.model.Product; public interface ProductService { - List findAll(); - - Product findById(long id); - - Long create(Product product); - - void saveAll(List products); + List findAll(); + + Product findById(long id); + + Long create(Product product); + + void saveAll(List products); } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java index 1005b5753d..e2d182a954 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java @@ -18,27 +18,27 @@ import com.baeldung.hexagonal.architecture.repository.ProductRepository; @Service public class ProductServiceImpl implements ProductService { - private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); - - @Autowired - private ProductRepository productRepository; - - public List findAll() { - return productRepository.findAll(); - } + private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); - public Product findById(long id) { - return productRepository.findOne(id); - } + @Autowired + private ProductRepository productRepository; - public Long create(Product product) { - product = productRepository.saveAndFlush(product); - return product.getId(); - } + public List findAll() { + return productRepository.findAll(); + } - @Override - public void saveAll(List products) { - productRepository.save(products); - } + public Product findById(long id) { + return productRepository.findOne(id); + } + + public Long create(Product product) { + product = productRepository.saveAndFlush(product); + return product.getId(); + } + + @Override + public void saveAll(List products) { + productRepository.save(products); + } } diff --git a/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java b/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java index 021fdf1289..748df3c0d0 100644 --- a/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java +++ b/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java @@ -25,19 +25,19 @@ import com.baeldung.hexagonal.architecture.service.ProductService; @SpringApplicationConfiguration(App.class) @ActiveProfiles(value = "test") public class ProductServiceTest { - - @Autowired - private ProductService productService; - @Test - public void testCreateProduct() { - Product product = new Product(); - product.setDescription("test product"); - product.setName("Product1"); - product.setPrice(10.0); - product.setQuantity(100l); - Long id = productService.create(product); - Assert.assertTrue(id >0); - } - + @Autowired + private ProductService productService; + + @Test + public void testCreateProduct() { + Product product = new Product(); + product.setDescription("test product"); + product.setName("Product1"); + product.setPrice(10.0); + product.setQuantity(100l); + Long id = productService.create(product); + Assert.assertTrue(id > 0); + } + } From 9b37a588ace41fa5484083b6e94a76666d735ac2 Mon Sep 17 00:00:00 2001 From: akeshri Date: Sat, 29 Aug 2020 21:02:52 +0530 Subject: [PATCH 07/13] changes --- .../com/baeldung/article/HashMapExample.java | 36 ++++++++++++++++++ .../article/LinkedHashMapExample.java | 37 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java create mode 100644 MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java diff --git a/MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java b/MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java new file mode 100644 index 0000000000..3292d995ab --- /dev/null +++ b/MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java @@ -0,0 +1,36 @@ +package com.baeldung.article; + +import java.util.AbstractMap; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * @author AshwiniKeshri + * + */ +public class HashMapExample { + public static void main(String[] args) { + + Map hashmap = new HashMap<>(); + hashmap.put(5, "A"); + hashmap.put(1, "B"); + hashmap.put(2, "C"); + // hashmap.put(0, "D"); + + System.out.println(hashmap); + + Set> entrySet = hashmap.entrySet(); + + Iterator> iterator = entrySet.iterator(); + + if (iterator.hasNext()) + System.out.println("Using Iteraor: " + iterator.next()); + + System.out.println("Using Stream: " + entrySet.stream() + .findFirst() + .orElse(new AbstractMap.SimpleEntry(-1, "DEFAULT"))); + + } +} diff --git a/MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java b/MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java new file mode 100644 index 0000000000..249b9a0585 --- /dev/null +++ b/MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java @@ -0,0 +1,37 @@ +package com.baeldung.article; + +import java.util.AbstractMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +/** + * @author AshwiniKeshri + * + */ +public class LinkedHashMapExample { + + public static void main(String[] args) { + + LinkedHashMap linkedHashMap = new LinkedHashMap<>(); + linkedHashMap.put(5, "A"); + linkedHashMap.put(1, "B"); + linkedHashMap.put(2, "C"); + // linkedHashMap.put(0, "D"); + + System.out.println(linkedHashMap); + + Set> entrySet = linkedHashMap.entrySet(); + + Iterator> iterator = entrySet.iterator(); + + if (iterator.hasNext()) + System.out.println("Using Iterator: " + iterator.next()); + + System.out.println("Using Stream: " + entrySet.stream() + .findFirst() + .orElse(new AbstractMap.SimpleEntry(0, "DEFAULT"))); + } + +} From 85efd879ad9a71cf814f250a3b1d92b2d3aad552 Mon Sep 17 00:00:00 2001 From: akeshri Date: Sat, 29 Aug 2020 23:42:56 +0530 Subject: [PATCH 08/13] changes --- .../src/com/baeldung/article/HashMapExample.java | 0 .../src/com/baeldung/article/LinkedHashMapExample.java | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {MapFirstKeyExample => map-first-key-tutorial}/src/com/baeldung/article/HashMapExample.java (100%) rename {MapFirstKeyExample => map-first-key-tutorial}/src/com/baeldung/article/LinkedHashMapExample.java (100%) diff --git a/MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java b/map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java similarity index 100% rename from MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java rename to map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java diff --git a/MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java b/map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java similarity index 100% rename from MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java rename to map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java From 93b8f878c8f1eab53c4cbc41627ae0cf1b46f877 Mon Sep 17 00:00:00 2001 From: akeshri Date: Sat, 29 Aug 2020 23:48:29 +0530 Subject: [PATCH 09/13] changes --- map-first-key-tutorial/README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 map-first-key-tutorial/README.md diff --git a/map-first-key-tutorial/README.md b/map-first-key-tutorial/README.md new file mode 100644 index 0000000000..13fb71643b --- /dev/null +++ b/map-first-key-tutorial/README.md @@ -0,0 +1,4 @@ +# HashMap - Getting First Key And Value +A example on how to get first key and value from HashMap. + +- JDK 1.8 and above \ No newline at end of file From 79fcbe173cfa843fa6226dbb1c49b0924a54fc38 Mon Sep 17 00:00:00 2001 From: akeshri Date: Tue, 8 Sep 2020 21:04:43 +0530 Subject: [PATCH 10/13] first pair code --- .../mapfirstpair/MapFirstPairService.java | 17 +++ .../mapfirstpair/MapFirstPairServiceImpl.java | 40 ++++++ .../MapFirstPairServiceUnitTest.java | 125 ++++++++++++++++++ hexagonal-architecture/README.md | 17 --- hexagonal-architecture/pom.xml | 48 ------- .../baeldung/hexagonal/architecture/App.java | 16 --- .../hexagonal/architecture/ConsoleApp.java | 50 ------- .../controller/ProductController.java | 55 -------- .../architecture/dtos/ProductDto.java | 72 ---------- .../hexagonal/architecture/model/Product.java | 85 ------------ .../repository/ProductRepository.java | 14 -- .../architecture/service/ProductService.java | 21 --- .../service/ProductServiceImpl.java | 44 ------ .../resources/application-batch.properties | 9 -- .../resources/application-test.properties | 8 -- .../src/main/resources/application.properties | 8 -- .../src/main/resources/db/PRODUCT.sql | 9 -- .../service/ProductServiceTest.java | 43 ------ 18 files changed, 182 insertions(+), 499 deletions(-) create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java create mode 100644 core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java delete mode 100644 hexagonal-architecture/README.md delete mode 100644 hexagonal-architecture/pom.xml delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java delete mode 100644 hexagonal-architecture/src/main/resources/application-batch.properties delete mode 100644 hexagonal-architecture/src/main/resources/application-test.properties delete mode 100644 hexagonal-architecture/src/main/resources/application.properties delete mode 100644 hexagonal-architecture/src/main/resources/db/PRODUCT.sql delete mode 100644 hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java new file mode 100644 index 0000000000..5d3c4fac32 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java @@ -0,0 +1,17 @@ +/** + * + */ +package com.baeldung.collections.mapfirstpair; + +import java.util.Map; + +/** + * @author ASHWINI + * + */ +public interface MapFirstPairService { + + Map.Entry getFirstPairUsingIterator(Map map); + + Map.Entry getFirstPairUsingStream(Map map); +} diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java new file mode 100644 index 0000000000..aff6430216 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java @@ -0,0 +1,40 @@ +/** + * + */ +package com.baeldung.collections.mapfirstpair; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * @author ASHWINI + * + */ +public class MapFirstPairServiceImpl implements MapFirstPairService { + + public Map.Entry getFirstPairUsingIterator(Map map) { + if (map == null || map.size() == 0) + return null; + + Iterator> iterator = map.entrySet() + .iterator(); + + if (iterator.hasNext()) + return iterator.next(); + + return null; + } + + public Map.Entry getFirstPairUsingStream(Map map) { + if (map == null || map.size() == 0) + return null; + + Set> entrySet = map.entrySet(); + + return entrySet.stream() + .findFirst() + .get(); + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java new file mode 100644 index 0000000000..56e293ffc5 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java @@ -0,0 +1,125 @@ +package com.baeldung.collections.mapfirstpair; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.util.AbstractMap; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +public class MapFirstPairServiceUnitTest { + + private MapFirstPairService mapFirstPairService; + + @Before + public void Setup() { + + mapFirstPairService = new MapFirstPairServiceImpl(); + } + + private void populateMapValues(Map map) { + if (map != null) { + map.put(5, "A"); + map.put(1, "B"); + map.put(2, "C"); + } + } + + @Test + public void whenUsingIteratorForHashMap() { + Map hashMap = new HashMap<>(); + populateMapValues(hashMap); + + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); + Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + assertNotEquals(pairInsertedFirst, actualValue); + } + + @Test + public void whenUsingStreamForHashMap() { + Map hashMap = new HashMap<>(); + populateMapValues(hashMap); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); + Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + assertNotEquals(pairInsertedFirst, actualValue); + } + + @Test + public void whenUsingIteratorForLinkedHashMap() { + Map linkedHashMap = new LinkedHashMap<>(); + populateMapValues(linkedHashMap); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenUsingStreamForLinkedHashMap() { + Map linkedHashMap = new LinkedHashMap<>(); + populateMapValues(linkedHashMap); + + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() { + Map hashMap = new HashMap<>(); + populateMapValues(hashMap); + + hashMap.put(0, "D"); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() { + Map hashMap = new HashMap<>(); + populateMapValues(hashMap); + + hashMap.put(0, "D"); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() { + Map linkedHashMap = new LinkedHashMap<>(); + populateMapValues(linkedHashMap); + + linkedHashMap.put(0, "D"); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() { + Map linkedHashMap = new LinkedHashMap<>(); + populateMapValues(linkedHashMap); + + linkedHashMap.put(0, "D"); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } +} diff --git a/hexagonal-architecture/README.md b/hexagonal-architecture/README.md deleted file mode 100644 index 4dd10368e0..0000000000 --- a/hexagonal-architecture/README.md +++ /dev/null @@ -1,17 +0,0 @@ -# Hexagonal Architecture -A quick and practical example of Hexagonal Architecture using Spring boot. - -This application is using h2 database,which can be accessible http:/localhost:8080/h2 - -Main Application schema : hexagonal - -Test Application Schema : hexagonal_test - -1. Rest Api : execute [App](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java) - - Get All products : http://localhost:8080/api/v1/product/all - - Get product by id : http://localhost:8080/api/v1/product/{productId} - - Add a product : http://localhost:8080/api/v1/product/add - For more detail refer [ProductController](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java) - -2. Batch processing : We need to configure active profile as batch i.e. -Dspring.profiles.active=batch and execute [ConsoleApp](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java) -3. Test case : [ProductServiceTest](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java) diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml deleted file mode 100644 index d014617639..0000000000 --- a/hexagonal-architecture/pom.xml +++ /dev/null @@ -1,48 +0,0 @@ - - 4.0.0 - - com.article - hexagonal-architecture - 0.0.1-SNAPSHOT - jar - - org.springframework.boot - spring-boot-starter-parent - 1.3.1.RELEASE - - - hexagonal-architecture - http://maven.apache.org - - - UTF-8 - 1.8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-data-jpa - - - com.h2database - h2 - - - org.springframework.boot - spring-boot-starter-test - test - - - com.fasterxml.jackson.dataformat - jackson-dataformat-csv - - - diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java deleted file mode 100644 index 29dbec470f..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.hexagonal.architecture; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -/** - * @author AshwiniKeshri - * - */ - -@SpringBootApplication -public class App { - public static void main(String[] args) { - SpringApplication.run(App.class, args); - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java deleted file mode 100644 index 6aef791893..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.hexagonal.architecture; - -import java.io.File; -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; -import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; - -import com.baeldung.hexagonal.architecture.model.Product; -import com.baeldung.hexagonal.architecture.service.ProductService; -import com.fasterxml.jackson.dataformat.csv.CsvMapper; -import com.fasterxml.jackson.dataformat.csv.CsvSchema; - -/** - * @author AshwiniKeshri - * - */ - -@SpringBootApplication(exclude = { EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class }) -public class ConsoleApp implements CommandLineRunner { - @Autowired - private ProductService productService; - - public static void main(String[] args) { - SpringApplication.run(ConsoleApp.class, args); - } - - @Override - public void run(String... args) throws Exception { - String filePath = ""; - if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) && (filePath = args[1]).length() > 0) { - File sourceFile = new File(filePath); - if (sourceFile.exists()) { - CsvMapper mapper = new CsvMapper(); - List products = mapper.readerFor(Product.class) - .with(CsvSchema.emptySchema() - .withHeader()) - . readValues(sourceFile) - .readAll(); - productService.saveAll(products); - } - - } - - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java deleted file mode 100644 index f54d3268df..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.baeldung.hexagonal.architecture.controller; - -import java.util.List; -import java.util.stream.Collectors; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; -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.RestController; - -import com.baeldung.hexagonal.architecture.dtos.ProductDto; -import com.baeldung.hexagonal.architecture.model.Product; -import com.baeldung.hexagonal.architecture.service.ProductService; - -/** - * @author AshwiniKeshri - * - */ - -@RestController -@RequestMapping("api/v1/product") -public class ProductController { - - @Autowired - private ProductService productService; - - @RequestMapping(value = "/all", method = RequestMethod.GET) - public List list() { - return productService.findAll() - .stream() - .map(p -> new ProductDto(p)) - .collect(Collectors.toList()); - } - - @RequestMapping(value = "/{productId}", method = RequestMethod.GET) - public ProductDto get(@PathVariable long productId) { - Product p = productService.findById(productId); - return p != null ? new ProductDto(p) : null; - } - - @RequestMapping(value = "/add", method = RequestMethod.POST) - public ProductDto create(@RequestBody ProductDto product) { - Product p = new Product(); - p.setDescription(product.getDescription()); - p.setName(product.getName()); - p.setPrice(product.getPrice()); - p.setQuantity(product.getQuantity()); - Long id = productService.create(p); - product.setId(id); - return product; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java deleted file mode 100644 index 51692f56aa..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.baeldung.hexagonal.architecture.dtos; - -import com.baeldung.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ -public class ProductDto { - - private Long id; - - private String name; - - private Long quantity; - - private Double price; - - private String description; - - public ProductDto() { - } - - public ProductDto(Product product) { - this.description = product.getDescription(); - this.id = product.getId(); - this.name = product.getName(); - this.price = product.getPrice(); - this.quantity = product.getQuantity(); - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Long getQuantity() { - return quantity; - } - - public void setQuantity(Long quantity) { - this.quantity = quantity; - } - - public Double getPrice() { - return price; - } - - public void setPrice(Double price) { - this.price = price; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java deleted file mode 100644 index 697ff68b50..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java +++ /dev/null @@ -1,85 +0,0 @@ -/** - * - */ -package com.baeldung.hexagonal.architecture.model; - -import java.io.Serializable; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; - -/** - * @author AshwiniKeshri - * - */ - -@Entity -@Table(name = "PRODUCT") -public class Product implements Serializable { - - /** - * - */ - private static final long serialVersionUID = 4000353732860709995L; - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - - @Column(name = "NAME") - private String name; - - @Column(name = "QUANTITY") - private Long quantity; - - @Column(name = "PRICE") - private Double price; - - @Column(name = "DESCRIPTION") - private String description; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Long getQuantity() { - return quantity; - } - - public void setQuantity(Long quantity) { - this.quantity = quantity > 0 ? quantity : 0; - } - - public Double getPrice() { - return price; - } - - public void setPrice(Double price) { - this.price = price == null ? 0.0 : price; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java deleted file mode 100644 index ec50e86dc3..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.hexagonal.architecture.repository; - -import org.springframework.data.jpa.repository.JpaRepository; - -import com.baeldung.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ - -public interface ProductRepository extends JpaRepository { - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java deleted file mode 100644 index 4844723140..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.hexagonal.architecture.service; - -import java.util.List; - -import com.baeldung.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ - -public interface ProductService { - - List findAll(); - - Product findById(long id); - - Long create(Product product); - - void saveAll(List products); -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java deleted file mode 100644 index e2d182a954..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung.hexagonal.architecture.service; - -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.baeldung.hexagonal.architecture.model.Product; -import com.baeldung.hexagonal.architecture.repository.ProductRepository; - -/** - * @author AshwiniKeshri - * - */ - -@Service -public class ProductServiceImpl implements ProductService { - - private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); - - @Autowired - private ProductRepository productRepository; - - public List findAll() { - return productRepository.findAll(); - } - - public Product findById(long id) { - return productRepository.findOne(id); - } - - public Long create(Product product) { - product = productRepository.saveAndFlush(product); - return product.getId(); - } - - @Override - public void saveAll(List products) { - productRepository.save(products); - } - -} diff --git a/hexagonal-architecture/src/main/resources/application-batch.properties b/hexagonal-architecture/src/main/resources/application-batch.properties deleted file mode 100644 index 8c83d19f74..0000000000 --- a/hexagonal-architecture/src/main/resources/application-batch.properties +++ /dev/null @@ -1,9 +0,0 @@ -spring.main.web-environment=false -spring.jpa.hibernate.ddl-auto=false; -spring.h2.console.enabled=true -spring.h2.console.path=/h2 - -spring.datasource.url=jdbc:h2:file:~/hexagonal -spring.datasource.username=sa -spring.datasource.password= -spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/application-test.properties b/hexagonal-architecture/src/main/resources/application-test.properties deleted file mode 100644 index 701313a878..0000000000 --- a/hexagonal-architecture/src/main/resources/application-test.properties +++ /dev/null @@ -1,8 +0,0 @@ -spring.jpa.hibernate.ddl-auto=false; -spring.h2.console.enabled=true -spring.h2.console.path=/h2 - -spring.datasource.url=jdbc:h2:file:~/hexagonal_test -spring.datasource.username=sa -spring.datasource.password= -spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/application.properties b/hexagonal-architecture/src/main/resources/application.properties deleted file mode 100644 index 14c80a1af4..0000000000 --- a/hexagonal-architecture/src/main/resources/application.properties +++ /dev/null @@ -1,8 +0,0 @@ -spring.jpa.hibernate.ddl-auto=false; -spring.h2.console.enabled=true -spring.h2.console.path=/h2 - -spring.datasource.url=jdbc:h2:file:~/hexagonal -spring.datasource.username=sa -spring.datasource.password= -spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/db/PRODUCT.sql b/hexagonal-architecture/src/main/resources/db/PRODUCT.sql deleted file mode 100644 index a0fef3a710..0000000000 --- a/hexagonal-architecture/src/main/resources/db/PRODUCT.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE TABLE PRODUCT( - ID INT AUTO_INCREMENT, - NAME VARCHAR(255), - QUANTITY INTEGER, - PRICE DOUBLE, - DESCRIPTION VARCHAR(1000), -); - -INSERT INTO PRODUCT(NAME,QUANTITY,PRICE,DESCRIPTION) VALUES ('iPhone 11 Pro',10,300,'First triple camera system'); \ No newline at end of file diff --git a/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java b/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java deleted file mode 100644 index 748df3c0d0..0000000000 --- a/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * - */ -package com.baeldung.hexagonal.architecture.service; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.context.annotation.PropertySources; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.baeldung.hexagonal.architecture.App; -import com.baeldung.hexagonal.architecture.model.Product; -import com.baeldung.hexagonal.architecture.service.ProductService; - -/** - * @author AshwiniKeshri - * - */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(App.class) -@ActiveProfiles(value = "test") -public class ProductServiceTest { - - @Autowired - private ProductService productService; - - @Test - public void testCreateProduct() { - Product product = new Product(); - product.setDescription("test product"); - product.setName("Product1"); - product.setPrice(10.0); - product.setQuantity(100l); - Long id = productService.create(product); - Assert.assertTrue(id > 0); - } - -} From 46a4378ad8d35a20eeb9214119221ad7b6046fa1 Mon Sep 17 00:00:00 2001 From: akeshri Date: Tue, 8 Sep 2020 21:14:28 +0530 Subject: [PATCH 11/13] changes --- map-first-key-tutorial/README.md | 4 -- .../com/baeldung/article/HashMapExample.java | 36 ------------------ .../article/LinkedHashMapExample.java | 37 ------------------- 3 files changed, 77 deletions(-) delete mode 100644 map-first-key-tutorial/README.md delete mode 100644 map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java delete mode 100644 map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java diff --git a/map-first-key-tutorial/README.md b/map-first-key-tutorial/README.md deleted file mode 100644 index 13fb71643b..0000000000 --- a/map-first-key-tutorial/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# HashMap - Getting First Key And Value -A example on how to get first key and value from HashMap. - -- JDK 1.8 and above \ No newline at end of file diff --git a/map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java b/map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java deleted file mode 100644 index 3292d995ab..0000000000 --- a/map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.article; - -import java.util.AbstractMap; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -/** - * @author AshwiniKeshri - * - */ -public class HashMapExample { - public static void main(String[] args) { - - Map hashmap = new HashMap<>(); - hashmap.put(5, "A"); - hashmap.put(1, "B"); - hashmap.put(2, "C"); - // hashmap.put(0, "D"); - - System.out.println(hashmap); - - Set> entrySet = hashmap.entrySet(); - - Iterator> iterator = entrySet.iterator(); - - if (iterator.hasNext()) - System.out.println("Using Iteraor: " + iterator.next()); - - System.out.println("Using Stream: " + entrySet.stream() - .findFirst() - .orElse(new AbstractMap.SimpleEntry(-1, "DEFAULT"))); - - } -} diff --git a/map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java b/map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java deleted file mode 100644 index 249b9a0585..0000000000 --- a/map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.article; - -import java.util.AbstractMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; - -/** - * @author AshwiniKeshri - * - */ -public class LinkedHashMapExample { - - public static void main(String[] args) { - - LinkedHashMap linkedHashMap = new LinkedHashMap<>(); - linkedHashMap.put(5, "A"); - linkedHashMap.put(1, "B"); - linkedHashMap.put(2, "C"); - // linkedHashMap.put(0, "D"); - - System.out.println(linkedHashMap); - - Set> entrySet = linkedHashMap.entrySet(); - - Iterator> iterator = entrySet.iterator(); - - if (iterator.hasNext()) - System.out.println("Using Iterator: " + iterator.next()); - - System.out.println("Using Stream: " + entrySet.stream() - .findFirst() - .orElse(new AbstractMap.SimpleEntry(0, "DEFAULT"))); - } - -} From e4dc7f5c8d9730e743f60abb8a69451c25f4a29c Mon Sep 17 00:00:00 2001 From: akeshri Date: Wed, 9 Sep 2020 09:34:10 +0530 Subject: [PATCH 12/13] changes --- .../mapfirstpair/MapFirstPairExample.java | 42 +++++++++++++++ .../mapfirstpair/MapFirstPairService.java | 17 ------ .../mapfirstpair/MapFirstPairServiceImpl.java | 40 -------------- ...nitTest.java => MapFirstPairUnitTest.java} | 54 ++++++++----------- 4 files changed, 65 insertions(+), 88 deletions(-) create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java delete mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java delete mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java rename core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/{MapFirstPairServiceUnitTest.java => MapFirstPairUnitTest.java} (61%) diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java new file mode 100644 index 0000000000..8e5fc296d4 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java @@ -0,0 +1,42 @@ +package com.baeldung.collections.mapfirstpair; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +public class MapFirstPairExample { + + public Map.Entry getFirstPairUsingIterator(Map map) { + if (map == null || map.size() == 0) + return null; + + Iterator> iterator = map.entrySet() + .iterator(); + + if (iterator.hasNext()) + return iterator.next(); + + return null; + } + + public Map.Entry getFirstPairUsingStream(Map map) { + if (map == null || map.size() == 0) + return null; + + Set> entrySet = map.entrySet(); + + return entrySet.stream() + .findFirst() + .get(); + } + + public Map populateMapValues(Map map) { + if (map != null) { + map.put(5, "A"); + map.put(1, "B"); + map.put(2, "C"); + } + return map; + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java deleted file mode 100644 index 5d3c4fac32..0000000000 --- a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java +++ /dev/null @@ -1,17 +0,0 @@ -/** - * - */ -package com.baeldung.collections.mapfirstpair; - -import java.util.Map; - -/** - * @author ASHWINI - * - */ -public interface MapFirstPairService { - - Map.Entry getFirstPairUsingIterator(Map map); - - Map.Entry getFirstPairUsingStream(Map map); -} diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java deleted file mode 100644 index aff6430216..0000000000 --- a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * - */ -package com.baeldung.collections.mapfirstpair; - -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -/** - * @author ASHWINI - * - */ -public class MapFirstPairServiceImpl implements MapFirstPairService { - - public Map.Entry getFirstPairUsingIterator(Map map) { - if (map == null || map.size() == 0) - return null; - - Iterator> iterator = map.entrySet() - .iterator(); - - if (iterator.hasNext()) - return iterator.next(); - - return null; - } - - public Map.Entry getFirstPairUsingStream(Map map) { - if (map == null || map.size() == 0) - return null; - - Set> entrySet = map.entrySet(); - - return entrySet.stream() - .findFirst() - .get(); - } - -} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java similarity index 61% rename from core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java rename to core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java index 56e293ffc5..c8198bcc02 100644 --- a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java +++ b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java @@ -11,30 +11,22 @@ import java.util.Map; import org.junit.Before; import org.junit.Test; -public class MapFirstPairServiceUnitTest { +public class MapFirstPairUnitTest { - private MapFirstPairService mapFirstPairService; + private MapFirstPairExample mapFirstPairExample; @Before public void Setup() { - mapFirstPairService = new MapFirstPairServiceImpl(); - } - - private void populateMapValues(Map map) { - if (map != null) { - map.put(5, "A"); - map.put(1, "B"); - map.put(2, "C"); - } + mapFirstPairExample = new MapFirstPairExample(); } @Test - public void whenUsingIteratorForHashMap() { + public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst() { Map hashMap = new HashMap<>(); - populateMapValues(hashMap); + hashMap = mapFirstPairExample.populateMapValues(hashMap); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); @@ -43,10 +35,10 @@ public class MapFirstPairServiceUnitTest { } @Test - public void whenUsingStreamForHashMap() { + public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() { Map hashMap = new HashMap<>(); - populateMapValues(hashMap); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap); + hashMap = mapFirstPairExample.populateMapValues(hashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); @@ -55,21 +47,21 @@ public class MapFirstPairServiceUnitTest { } @Test - public void whenUsingIteratorForLinkedHashMap() { + public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { Map linkedHashMap = new LinkedHashMap<>(); - populateMapValues(linkedHashMap); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap); + linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); } @Test - public void whenUsingStreamForLinkedHashMap() { + public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { Map linkedHashMap = new LinkedHashMap<>(); - populateMapValues(linkedHashMap); + linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -78,10 +70,10 @@ public class MapFirstPairServiceUnitTest { @Test public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() { Map hashMap = new HashMap<>(); - populateMapValues(hashMap); + hashMap = mapFirstPairExample.populateMapValues(hashMap); hashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); assertEquals(expectedValue, actualValue); @@ -90,10 +82,10 @@ public class MapFirstPairServiceUnitTest { @Test public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() { Map hashMap = new HashMap<>(); - populateMapValues(hashMap); + hashMap = mapFirstPairExample.populateMapValues(hashMap); hashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); assertEquals(expectedValue, actualValue); @@ -102,10 +94,10 @@ public class MapFirstPairServiceUnitTest { @Test public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() { Map linkedHashMap = new LinkedHashMap<>(); - populateMapValues(linkedHashMap); + linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); linkedHashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -114,10 +106,10 @@ public class MapFirstPairServiceUnitTest { @Test public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() { Map linkedHashMap = new LinkedHashMap<>(); - populateMapValues(linkedHashMap); + linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); linkedHashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); From e23f29e728841155498e0a8df3827e4288bda26c Mon Sep 17 00:00:00 2001 From: akeshri Date: Thu, 10 Sep 2020 09:26:24 +0530 Subject: [PATCH 13/13] changes --- .../mapfirstpair/MapFirstPairExample.java | 42 ------------ .../mapfirstpair/MapFirstPairUnitTest.java | 68 +++++++++++++------ 2 files changed, 47 insertions(+), 63 deletions(-) delete mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java deleted file mode 100644 index 8e5fc296d4..0000000000 --- a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.collections.mapfirstpair; - -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -public class MapFirstPairExample { - - public Map.Entry getFirstPairUsingIterator(Map map) { - if (map == null || map.size() == 0) - return null; - - Iterator> iterator = map.entrySet() - .iterator(); - - if (iterator.hasNext()) - return iterator.next(); - - return null; - } - - public Map.Entry getFirstPairUsingStream(Map map) { - if (map == null || map.size() == 0) - return null; - - Set> entrySet = map.entrySet(); - - return entrySet.stream() - .findFirst() - .get(); - } - - public Map populateMapValues(Map map) { - if (map != null) { - map.put(5, "A"); - map.put(1, "B"); - map.put(2, "C"); - } - return map; - } - -} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java index c8198bcc02..b25e0932d8 100644 --- a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java +++ b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java @@ -5,28 +5,54 @@ import static org.junit.Assert.assertNotEquals; import java.util.AbstractMap; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; -import org.junit.Before; import org.junit.Test; public class MapFirstPairUnitTest { - private MapFirstPairExample mapFirstPairExample; + private Map.Entry getFirstPairUsingIterator(Map map) { + if (map == null || map.size() == 0) + return null; - @Before - public void Setup() { + Iterator> iterator = map.entrySet() + .iterator(); - mapFirstPairExample = new MapFirstPairExample(); + if (iterator.hasNext()) + return iterator.next(); + + return null; + } + + private Map.Entry getFirstPairUsingStream(Map map) { + if (map == null || map.size() == 0) + return null; + + Set> entrySet = map.entrySet(); + + return entrySet.stream() + .findFirst() + .get(); + } + + private Map populateMapValues(Map map) { + if (map != null) { + map.put(5, "A"); + map.put(1, "B"); + map.put(2, "C"); + } + return map; } @Test public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); + hashMap = populateMapValues(hashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap); + Map.Entry actualValue = getFirstPairUsingIterator(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); @@ -37,8 +63,8 @@ public class MapFirstPairUnitTest { @Test public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap); + hashMap = populateMapValues(hashMap); + Map.Entry actualValue = getFirstPairUsingStream(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); @@ -49,8 +75,8 @@ public class MapFirstPairUnitTest { @Test public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingIterator(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -59,9 +85,9 @@ public class MapFirstPairUnitTest { @Test public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingStream(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -70,10 +96,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); + hashMap = populateMapValues(hashMap); hashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap); + Map.Entry actualValue = getFirstPairUsingIterator(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); assertEquals(expectedValue, actualValue); @@ -82,10 +108,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); + hashMap = populateMapValues(hashMap); hashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap); + Map.Entry actualValue = getFirstPairUsingStream(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); assertEquals(expectedValue, actualValue); @@ -94,10 +120,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); linkedHashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingIterator(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -106,10 +132,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); linkedHashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingStream(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue);