From 21ecb354d9a250b899998a4f3e5288d4653e9068 Mon Sep 17 00:00:00 2001 From: Radhe Sravan Date: Wed, 6 Nov 2019 07:16:40 +0530 Subject: [PATCH 1/4] Example for hexagonal architecture in java --- patterns/hexagonal-architecture/.gitignore | 33 ++++++++++++ patterns/hexagonal-architecture/README.md | 24 +++++++++ patterns/hexagonal-architecture/pom.xml | 53 +++++++++++++++++++ .../arch/AddressBookApplication.java | 16 ++++++ .../controller/AddressBookController.java | 32 +++++++++++ .../repository/ContactRepositoryImpl.java | 30 +++++++++++ .../hexagonal/arch/core/domain/Contact.java | 33 ++++++++++++ .../core/service/AddressBookServiceImpl.java | 28 ++++++++++ .../arch/port/repo/ContactRepository.java | 13 +++++ .../arch/port/service/AddressBookService.java | 13 +++++ .../src/main/resources/application.properties | 1 + 11 files changed, 276 insertions(+) create mode 100644 patterns/hexagonal-architecture/.gitignore create mode 100644 patterns/hexagonal-architecture/README.md create mode 100644 patterns/hexagonal-architecture/pom.xml create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java create mode 100644 patterns/hexagonal-architecture/src/main/resources/application.properties diff --git a/patterns/hexagonal-architecture/.gitignore b/patterns/hexagonal-architecture/.gitignore new file mode 100644 index 0000000000..65069b2e61 --- /dev/null +++ b/patterns/hexagonal-architecture/.gitignore @@ -0,0 +1,33 @@ +target/ +.mvn/ +mvnw +mvnw.cmd +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/** +!**/src/test/** + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ + +### VS Code ### +.vscode/ diff --git a/patterns/hexagonal-architecture/README.md b/patterns/hexagonal-architecture/README.md new file mode 100644 index 0000000000..94224a737d --- /dev/null +++ b/patterns/hexagonal-architecture/README.md @@ -0,0 +1,24 @@ +# HEXAGONAL ARCHITECTURE - EXAMPLE IN JAVA + +A Spring Boot base web application for address book has been built as part of this project based on the hexagonal architecture guidelines, which allows the user to add a contact ad list all the contacts that are available. + +### Running the application + +The application can be run using maven spring-boot run command + +``` +mvn spring-boot:run + +``` + +New contact can be added through a POST request on http://loalhost:8080/contacts with the body as + +``` +{ + "name": "ABC", + "address": "Address", + "contactNumber": 1234567890 +} + +``` +Existing contacts can be listed using a GET request on the same url http://loalhost:8080/contacts \ No newline at end of file diff --git a/patterns/hexagonal-architecture/pom.xml b/patterns/hexagonal-architecture/pom.xml new file mode 100644 index 0000000000..908eb21c01 --- /dev/null +++ b/patterns/hexagonal-architecture/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.0.RELEASE + + + com.baeldung.hexagonal.arch + hexagonal-architecture + 0.0.1-SNAPSHOT + hexagonal-architecture + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java new file mode 100644 index 0000000000..e20b79f8c6 --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java @@ -0,0 +1,16 @@ +package com.baeldung.hexagonal.arch; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; + +@SpringBootApplication +@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) +public class AddressBookApplication { + + public static void main(String[] args) { + SpringApplication.run(AddressBookApplication.class, args); + } + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java new file mode 100644 index 0000000000..34566c6b61 --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java @@ -0,0 +1,32 @@ +package com.baeldung.hexagonal.arch.adapter.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.hexagonal.arch.core.domain.Contact; +import com.baeldung.hexagonal.arch.port.service.AddressBookService; + +@RestController +@RequestMapping("/contacts") +public class AddressBookController { + + @Autowired + private AddressBookService addressBookService; + + @PostMapping + public void addContact(@RequestBody Contact contact) { + addressBookService.createContact(contact); + } + + @GetMapping + public List getAllContacts() { + return addressBookService.getAllContacts(); + } + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java new file mode 100644 index 0000000000..235a899d7c --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java @@ -0,0 +1,30 @@ +package com.baeldung.hexagonal.arch.adapter.repository; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.springframework.stereotype.Repository; + +import com.baeldung.hexagonal.arch.core.domain.Contact; +import com.baeldung.hexagonal.arch.port.repo.ContactRepository; + +@Repository +public class ContactRepositoryImpl implements ContactRepository { + + private Map contactsRepo = new HashMap(); + + @Override + public void createContact(Contact contact) { + contactsRepo.put(contact.getName(), contact); + } + + @Override + public List getAllContacts() { + return contactsRepo.values() + .stream() + .collect(Collectors.toList()); + } + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java new file mode 100644 index 0000000000..118657a8db --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java @@ -0,0 +1,33 @@ +package com.baeldung.hexagonal.arch.core.domain; + +public class Contact { + + private String name; + private String address; + private int contactNumber; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public int getContactNumber() { + return contactNumber; + } + + public void setContactNumber(int contactNumber) { + this.contactNumber = contactNumber; + } + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java new file mode 100644 index 0000000000..4a95e5cd72 --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java @@ -0,0 +1,28 @@ +package com.baeldung.hexagonal.arch.core.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.hexagonal.arch.core.domain.Contact; +import com.baeldung.hexagonal.arch.port.repo.ContactRepository; +import com.baeldung.hexagonal.arch.port.service.AddressBookService; + +@Service +public class AddressBookServiceImpl implements AddressBookService { + + @Autowired + ContactRepository contactRepository; + + @Override + public void createContact(Contact contact) { + contactRepository.createContact(contact); + } + + @Override + public List getAllContacts() { + return contactRepository.getAllContacts(); + } + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java new file mode 100644 index 0000000000..a8cc4f73ea --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.hexagonal.arch.port.repo; + +import java.util.List; + +import com.baeldung.hexagonal.arch.core.domain.Contact; + +public interface ContactRepository { + + void createContact(Contact contact); + + List getAllContacts(); + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java new file mode 100644 index 0000000000..b91683fbc2 --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java @@ -0,0 +1,13 @@ +package com.baeldung.hexagonal.arch.port.service; + +import java.util.List; + +import com.baeldung.hexagonal.arch.core.domain.Contact; + +public interface AddressBookService { + + public void createContact(Contact contact); + + public List getAllContacts(); + +} diff --git a/patterns/hexagonal-architecture/src/main/resources/application.properties b/patterns/hexagonal-architecture/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/resources/application.properties @@ -0,0 +1 @@ + From 595649bf05bd0500fb2f09b884cc03387a893c62 Mon Sep 17 00:00:00 2001 From: Radhe Sravan Date: Sun, 10 Nov 2019 09:15:18 +0530 Subject: [PATCH 2/4] BAEL-3469 - How to Pass command line arguments to bash script --- .../src/main/bash/userReg-flags.sh | 14 ++++++++++++++ .../src/main/bash/userReg-positional-parameter.sh | 5 +++++ .../src/main/bash/users-loop.sh | 8 ++++++++ .../src/main/bash/users-shift-operator.sh | 10 ++++++++++ 4 files changed, 37 insertions(+) create mode 100644 linux-bash/command-line-arguments/src/main/bash/userReg-flags.sh create mode 100644 linux-bash/command-line-arguments/src/main/bash/userReg-positional-parameter.sh create mode 100644 linux-bash/command-line-arguments/src/main/bash/users-loop.sh create mode 100644 linux-bash/command-line-arguments/src/main/bash/users-shift-operator.sh diff --git a/linux-bash/command-line-arguments/src/main/bash/userReg-flags.sh b/linux-bash/command-line-arguments/src/main/bash/userReg-flags.sh new file mode 100644 index 0000000000..b2b4589d03 --- /dev/null +++ b/linux-bash/command-line-arguments/src/main/bash/userReg-flags.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +while getopts u:a:f: flag +do +case "${flag}" +in +u) username=${OPTARG};; +a) age=${OPTARG};; +f) fullname=${OPTARG};; +esac +done +echo "Username: $username"; +echo "Age: $age"; +echo "Full Name: $fullname"; diff --git a/linux-bash/command-line-arguments/src/main/bash/userReg-positional-parameter.sh b/linux-bash/command-line-arguments/src/main/bash/userReg-positional-parameter.sh new file mode 100644 index 0000000000..7071ff56d4 --- /dev/null +++ b/linux-bash/command-line-arguments/src/main/bash/userReg-positional-parameter.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +echo "Username: $1"; +echo "Age: $2"; +echo "Full Name: $3"; diff --git a/linux-bash/command-line-arguments/src/main/bash/users-loop.sh b/linux-bash/command-line-arguments/src/main/bash/users-loop.sh new file mode 100644 index 0000000000..545416f3e0 --- /dev/null +++ b/linux-bash/command-line-arguments/src/main/bash/users-loop.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +i=1; +for user in "$@" +do +echo "Username - $i: $user"; +i=$((i + 1)); +done diff --git a/linux-bash/command-line-arguments/src/main/bash/users-shift-operator.sh b/linux-bash/command-line-arguments/src/main/bash/users-shift-operator.sh new file mode 100644 index 0000000000..a212b62317 --- /dev/null +++ b/linux-bash/command-line-arguments/src/main/bash/users-shift-operator.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +i=1; +j=$#; +while [ $i -le $j ] +do +echo "Username - $i: $1"; +i=$((i + 1)); +shift 1; +done From fcaf1cb33d844575890291f44752df0dd42c2e3c Mon Sep 17 00:00:00 2001 From: Radhe Sravan Date: Mon, 11 Nov 2019 17:32:16 +0530 Subject: [PATCH 3/4] Removed source code for evaluation article --- patterns/hexagonal-architecture/.gitignore | 33 ------------ patterns/hexagonal-architecture/README.md | 24 --------- patterns/hexagonal-architecture/pom.xml | 53 ------------------- .../arch/AddressBookApplication.java | 16 ------ .../controller/AddressBookController.java | 32 ----------- .../repository/ContactRepositoryImpl.java | 30 ----------- .../hexagonal/arch/core/domain/Contact.java | 33 ------------ .../core/service/AddressBookServiceImpl.java | 28 ---------- .../arch/port/repo/ContactRepository.java | 13 ----- .../arch/port/service/AddressBookService.java | 13 ----- .../src/main/resources/application.properties | 1 - 11 files changed, 276 deletions(-) delete mode 100644 patterns/hexagonal-architecture/.gitignore delete mode 100644 patterns/hexagonal-architecture/README.md delete mode 100644 patterns/hexagonal-architecture/pom.xml delete mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java delete mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java delete mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java delete mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java delete mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java delete mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java delete mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java delete mode 100644 patterns/hexagonal-architecture/src/main/resources/application.properties diff --git a/patterns/hexagonal-architecture/.gitignore b/patterns/hexagonal-architecture/.gitignore deleted file mode 100644 index 65069b2e61..0000000000 --- a/patterns/hexagonal-architecture/.gitignore +++ /dev/null @@ -1,33 +0,0 @@ -target/ -.mvn/ -mvnw -mvnw.cmd -!.mvn/wrapper/maven-wrapper.jar -!**/src/main/** -!**/src/test/** - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ -build/ - -### VS Code ### -.vscode/ diff --git a/patterns/hexagonal-architecture/README.md b/patterns/hexagonal-architecture/README.md deleted file mode 100644 index 94224a737d..0000000000 --- a/patterns/hexagonal-architecture/README.md +++ /dev/null @@ -1,24 +0,0 @@ -# HEXAGONAL ARCHITECTURE - EXAMPLE IN JAVA - -A Spring Boot base web application for address book has been built as part of this project based on the hexagonal architecture guidelines, which allows the user to add a contact ad list all the contacts that are available. - -### Running the application - -The application can be run using maven spring-boot run command - -``` -mvn spring-boot:run - -``` - -New contact can be added through a POST request on http://loalhost:8080/contacts with the body as - -``` -{ - "name": "ABC", - "address": "Address", - "contactNumber": 1234567890 -} - -``` -Existing contacts can be listed using a GET request on the same url http://loalhost:8080/contacts \ No newline at end of file diff --git a/patterns/hexagonal-architecture/pom.xml b/patterns/hexagonal-architecture/pom.xml deleted file mode 100644 index 908eb21c01..0000000000 --- a/patterns/hexagonal-architecture/pom.xml +++ /dev/null @@ -1,53 +0,0 @@ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.2.0.RELEASE - - - com.baeldung.hexagonal.arch - hexagonal-architecture - 0.0.1-SNAPSHOT - hexagonal-architecture - Demo project for Spring Boot - - - 1.8 - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - org.junit.vintage - junit-vintage-engine - - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java deleted file mode 100644 index e20b79f8c6..0000000000 --- a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.hexagonal.arch; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; - -@SpringBootApplication -@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) -public class AddressBookApplication { - - public static void main(String[] args) { - SpringApplication.run(AddressBookApplication.class, args); - } - -} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java deleted file mode 100644 index 34566c6b61..0000000000 --- a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.hexagonal.arch.adapter.controller; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import com.baeldung.hexagonal.arch.core.domain.Contact; -import com.baeldung.hexagonal.arch.port.service.AddressBookService; - -@RestController -@RequestMapping("/contacts") -public class AddressBookController { - - @Autowired - private AddressBookService addressBookService; - - @PostMapping - public void addContact(@RequestBody Contact contact) { - addressBookService.createContact(contact); - } - - @GetMapping - public List getAllContacts() { - return addressBookService.getAllContacts(); - } - -} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java deleted file mode 100644 index 235a899d7c..0000000000 --- a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.hexagonal.arch.adapter.repository; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import org.springframework.stereotype.Repository; - -import com.baeldung.hexagonal.arch.core.domain.Contact; -import com.baeldung.hexagonal.arch.port.repo.ContactRepository; - -@Repository -public class ContactRepositoryImpl implements ContactRepository { - - private Map contactsRepo = new HashMap(); - - @Override - public void createContact(Contact contact) { - contactsRepo.put(contact.getName(), contact); - } - - @Override - public List getAllContacts() { - return contactsRepo.values() - .stream() - .collect(Collectors.toList()); - } - -} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java deleted file mode 100644 index 118657a8db..0000000000 --- a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.hexagonal.arch.core.domain; - -public class Contact { - - private String name; - private String address; - private int contactNumber; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - - public int getContactNumber() { - return contactNumber; - } - - public void setContactNumber(int contactNumber) { - this.contactNumber = contactNumber; - } - -} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java deleted file mode 100644 index 4a95e5cd72..0000000000 --- a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.hexagonal.arch.core.service; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.baeldung.hexagonal.arch.core.domain.Contact; -import com.baeldung.hexagonal.arch.port.repo.ContactRepository; -import com.baeldung.hexagonal.arch.port.service.AddressBookService; - -@Service -public class AddressBookServiceImpl implements AddressBookService { - - @Autowired - ContactRepository contactRepository; - - @Override - public void createContact(Contact contact) { - contactRepository.createContact(contact); - } - - @Override - public List getAllContacts() { - return contactRepository.getAllContacts(); - } - -} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java deleted file mode 100644 index a8cc4f73ea..0000000000 --- a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.hexagonal.arch.port.repo; - -import java.util.List; - -import com.baeldung.hexagonal.arch.core.domain.Contact; - -public interface ContactRepository { - - void createContact(Contact contact); - - List getAllContacts(); - -} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java deleted file mode 100644 index b91683fbc2..0000000000 --- a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.hexagonal.arch.port.service; - -import java.util.List; - -import com.baeldung.hexagonal.arch.core.domain.Contact; - -public interface AddressBookService { - - public void createContact(Contact contact); - - public List getAllContacts(); - -} diff --git a/patterns/hexagonal-architecture/src/main/resources/application.properties b/patterns/hexagonal-architecture/src/main/resources/application.properties deleted file mode 100644 index 8b13789179..0000000000 --- a/patterns/hexagonal-architecture/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - From a844a2850a444fc702acbf138ab78830f497e0c4 Mon Sep 17 00:00:00 2001 From: Radhe Sravan Date: Sat, 23 Nov 2019 16:44:39 +0530 Subject: [PATCH 4/4] BAEL-3453 - Circular linked list Java implementation --- .../com/baeldung/list/CircularLinkedList.java | 102 ++++++++++++++++++ .../list/CircularLinkedListUnitTest.java | 60 +++++++++++ 2 files changed, 162 insertions(+) create mode 100644 data-structures/src/main/java/com/baeldung/list/CircularLinkedList.java create mode 100644 data-structures/src/test/java/com/baeldung/list/CircularLinkedListUnitTest.java diff --git a/data-structures/src/main/java/com/baeldung/list/CircularLinkedList.java b/data-structures/src/main/java/com/baeldung/list/CircularLinkedList.java new file mode 100644 index 0000000000..24c73eff16 --- /dev/null +++ b/data-structures/src/main/java/com/baeldung/list/CircularLinkedList.java @@ -0,0 +1,102 @@ +package com.baeldung.list; + +public class CircularLinkedList { + + Node head = null; + Node tail = null; + + public void addNode(int value) { + + Node newNode = new Node(value); + + // If no elements are present, make the newly addNodeed node as head + if (head == null) { + head = newNode; + } + // If there are elements already present, the existing tail should point to new node + else { + tail.nextNode = newNode; + } + + // Irrespective of whether or not elements are addNodeed, assign the + // tail to newNode and the nextNode for tail as head + tail = newNode; + tail.nextNode = head; + } + + public boolean containsNode(int searchValue) { + + // Start traversing from the head + Node currentNode = head; + + // If list is empty no need of traversal and can return false + if (head == null) { + return false; + } else { + do { + // Compares the search value with each node value present in the list + if (currentNode.value == searchValue) { + return true; + } + currentNode = currentNode.nextNode; + } while (currentNode != head); + return false; + } + } + + public void deleteNode(int valueToDelete) { + + // Start traversing from the head + Node currentNode = head; + + // If list is non empty + if (head != null) { + // If the node to delete is the head node itself, + // update the head as the next node of current head + // and the nextNode of tail as new head + if (currentNode.value == valueToDelete) { + head = head.nextNode; + tail.nextNode = head; + currentNode = null; + } else { + do { + // Fetch the next node of current node + Node nextNode = currentNode.nextNode; + // If the value to delete matches the next node's value, + // update the next node of current node as the next node of present next node + if (nextNode.value == valueToDelete) { + currentNode.nextNode = nextNode.nextNode; + nextNode = null; + break; + } + currentNode = currentNode.nextNode; + } while (currentNode != head); + } + } + } + + public void traverseList() { + + // Start traversing from the head + Node currentNode = head; + + if (head != null) { + do { + System.out.print(currentNode.value + " "); + currentNode = currentNode.nextNode; + } while (currentNode != head); + } + } + +} + +class Node { + + int value; + Node nextNode; + + public Node(int value) { + this.value = value; + } + +} \ No newline at end of file diff --git a/data-structures/src/test/java/com/baeldung/list/CircularLinkedListUnitTest.java b/data-structures/src/test/java/com/baeldung/list/CircularLinkedListUnitTest.java new file mode 100644 index 0000000000..94a1c853cf --- /dev/null +++ b/data-structures/src/test/java/com/baeldung/list/CircularLinkedListUnitTest.java @@ -0,0 +1,60 @@ +package com.baeldung.list; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class CircularLinkedListUnitTest { + + @Test + public void givenACircularLinkedList_WhenAddingElements_ThenListContainsThoseElements() { + + CircularLinkedList cll = createCircularLinkedList(); + + assertTrue(cll.containsNode(8)); + assertTrue(cll.containsNode(37)); + } + + @Test + public void givenACircularLinkedList_WhenLookingForNonExistingElement_ThenReturnsFalse() { + + CircularLinkedList cll = createCircularLinkedList(); + + assertFalse(cll.containsNode(11)); + } + + @Test + public void givenACircularLinkedList_WhenDeletingElements_ThenListDoesNotContainThoseElements() { + + CircularLinkedList cll = createCircularLinkedList(); + + assertTrue(cll.containsNode(13)); + cll.deleteNode(13); + assertFalse(cll.containsNode(13)); + + assertTrue(cll.containsNode(1)); + cll.deleteNode(1); + assertFalse(cll.containsNode(1)); + + assertTrue(cll.containsNode(46)); + cll.deleteNode(46); + assertFalse(cll.containsNode(46)); + } + + private CircularLinkedList createCircularLinkedList() { + + CircularLinkedList cll = new CircularLinkedList(); + + cll.addNode(13); + cll.addNode(7); + cll.addNode(24); + cll.addNode(1); + cll.addNode(8); + cll.addNode(37); + cll.addNode(46); + + return cll; + } + +}