diff --git a/core-java/src/main/java/com/baeldung/heapsort/Heap.java b/algorithms/src/main/java/com/baeldung/algorithms/heapsort/Heap.java similarity index 98% rename from core-java/src/main/java/com/baeldung/heapsort/Heap.java rename to algorithms/src/main/java/com/baeldung/algorithms/heapsort/Heap.java index 95e0e1d8cd..8c98e4fc5c 100644 --- a/core-java/src/main/java/com/baeldung/heapsort/Heap.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/heapsort/Heap.java @@ -1,4 +1,4 @@ -package com.baeldung.heapsort; +package com.baeldung.algorithms.heapsort; import java.util.ArrayList; import java.util.Arrays; diff --git a/core-java/src/test/java/com/baeldung/heapsort/HeapUnitTest.java b/algorithms/src/test/java/com/baeldung/algorithms/heapsort/HeapUnitTest.java similarity index 95% rename from core-java/src/test/java/com/baeldung/heapsort/HeapUnitTest.java rename to algorithms/src/test/java/com/baeldung/algorithms/heapsort/HeapUnitTest.java index cc3e49b6c9..96e4936eaf 100644 --- a/core-java/src/test/java/com/baeldung/heapsort/HeapUnitTest.java +++ b/algorithms/src/test/java/com/baeldung/algorithms/heapsort/HeapUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.heapsort; +package com.baeldung.algorithms.heapsort; import static org.assertj.core.api.Assertions.assertThat; diff --git a/core-java-collections/src/main/java/com/baeldung/removal/CollectionRemoveIf.java b/core-java-collections/src/main/java/com/baeldung/removal/CollectionRemoveIf.java new file mode 100644 index 0000000000..2f5e91596f --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/removal/CollectionRemoveIf.java @@ -0,0 +1,19 @@ +package com.baeldung.removal; + +import java.util.ArrayList; +import java.util.Collection; + +public class CollectionRemoveIf { + + public static void main(String args[]) { + Collection names = new ArrayList<>(); + names.add("John"); + names.add("Ana"); + names.add("Mary"); + names.add("Anthony"); + names.add("Mark"); + + names.removeIf(e -> e.startsWith("A")); + System.out.println(String.join(",", names)); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/removal/Iterators.java b/core-java-collections/src/main/java/com/baeldung/removal/Iterators.java new file mode 100644 index 0000000000..86b91b3fdc --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/removal/Iterators.java @@ -0,0 +1,28 @@ +package com.baeldung.removal; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +public class Iterators { + + public static void main(String args[]) { + Collection names = new ArrayList<>(); + names.add("John"); + names.add("Ana"); + names.add("Mary"); + names.add("Anthony"); + names.add("Mark"); + + Iterator i = names.iterator(); + + while (i.hasNext()) { + String e = i.next(); + if (e.startsWith("A")) { + i.remove(); + } + } + + System.out.println(String.join(",", names)); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/removal/StreamFilterAndCollector.java b/core-java-collections/src/main/java/com/baeldung/removal/StreamFilterAndCollector.java new file mode 100644 index 0000000000..bf6db68bae --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/removal/StreamFilterAndCollector.java @@ -0,0 +1,23 @@ +package com.baeldung.removal; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.stream.Collectors; + +public class StreamFilterAndCollector { + + public static void main(String args[]) { + Collection names = new ArrayList<>(); + names.add("John"); + names.add("Ana"); + names.add("Mary"); + names.add("Anthony"); + names.add("Mark"); + + Collection filteredCollection = names + .stream() + .filter(e -> !e.startsWith("A")) + .collect(Collectors.toList()); + System.out.println(String.join(",", filteredCollection)); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/removal/StreamPartitioningBy.java b/core-java-collections/src/main/java/com/baeldung/removal/StreamPartitioningBy.java new file mode 100644 index 0000000000..c77e996616 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/removal/StreamPartitioningBy.java @@ -0,0 +1,28 @@ +package com.baeldung.removal; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class StreamPartitioningBy { + + public static void main(String args[]) { + Collection names = new ArrayList<>(); + names.add("John"); + names.add("Ana"); + names.add("Mary"); + names.add("Anthony"); + names.add("Mark"); + + Map> classifiedElements = names + .stream() + .collect(Collectors.partitioningBy((String e) -> !e.startsWith("A"))); + + String matching = String.join(",", classifiedElements.get(Boolean.TRUE)); + String nonMatching = String.join(",", classifiedElements.get(Boolean.FALSE)); + System.out.println("Matching elements: " + matching); + System.out.println("Non matching elements: " + nonMatching); + } +} diff --git a/core-java-collections/src/test/java/com/baeldung/removal/RemovalUnitTest.java b/core-java-collections/src/test/java/com/baeldung/removal/RemovalUnitTest.java new file mode 100644 index 0000000000..1b379f32de --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/removal/RemovalUnitTest.java @@ -0,0 +1,77 @@ +package com.baeldung.removal; + +import org.junit.Before; +import org.junit.Test; + +import java.util.*; +import java.util.stream.Collectors; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class RemovalUnitTest { + + Collection names; + Collection expected; + Collection removed; + + @Before + public void setupTestData() { + names = new ArrayList<>(); + expected = new ArrayList<>(); + removed = new ArrayList<>(); + + names.add("John"); + names.add("Ana"); + names.add("Mary"); + names.add("Anthony"); + names.add("Mark"); + + expected.add("John"); + expected.add("Mary"); + expected.add("Mark"); + + removed.add("Ana"); + removed.add("Anthony"); + } + + @Test + public void givenCollectionOfNames_whenUsingIteratorToRemoveAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() { + Iterator i = names.iterator(); + + while (i.hasNext()) { + String e = i.next(); + if (e.startsWith("A")) { + i.remove(); + } + } + + assertThat(names, is(expected)); + } + + @Test + public void givenCollectionOfNames_whenUsingRemoveIfToRemoveAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() { + names.removeIf(e -> e.startsWith("A")); + assertThat(names, is(expected)); + } + + @Test + public void givenCollectionOfNames_whenUsingStreamToFilterAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() { + Collection filteredCollection = names + .stream() + .filter(e -> !e.startsWith("A")) + .collect(Collectors.toList()); + assertThat(filteredCollection, is(expected)); + } + + @Test + public void givenCollectionOfNames_whenUsingStreamAndPartitioningByToFindNamesThatStartWithLetterA_shouldFind3MatchingAnd2NonMatching() { + Map> classifiedElements = names + .stream() + .collect(Collectors.partitioningBy((String e) -> !e.startsWith("A"))); + + assertThat(classifiedElements.get(Boolean.TRUE), is(expected)); + assertThat(classifiedElements.get(Boolean.FALSE), is(removed)); + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/xmlToJson/XmlToJsonUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/xmlToJson/XmlToJsonUnitTest.java index 295bb9d6e8..6329a8ed2f 100644 --- a/jackson/src/test/java/com/baeldung/jackson/xmlToJson/XmlToJsonUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/xmlToJson/XmlToJsonUnitTest.java @@ -1,7 +1,5 @@ package com.baeldung.jackson.xmlToJson; - -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper; @@ -14,43 +12,32 @@ import java.io.IOException; public class XmlToJsonUnitTest { @Test - public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() { + public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() throws IOException{ String flowerXML = "PoppyRED9"; - try { - XmlMapper xmlMapper = new XmlMapper(); - Flower poppy = xmlMapper.readValue(flowerXML, Flower.class); + XmlMapper xmlMapper = new XmlMapper(); + Flower poppy = xmlMapper.readValue(flowerXML, Flower.class); - assertEquals(poppy.getName(), "Poppy"); - assertEquals(poppy.getColor(), Color.RED); - assertEquals(poppy.getPetals(), new Integer(9)); + assertEquals(poppy.getName(), "Poppy"); + assertEquals(poppy.getColor(), Color.RED); + assertEquals(poppy.getPetals(), new Integer(9)); - ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(poppy); + ObjectMapper mapper = new ObjectMapper(); + String json = mapper.writeValueAsString(poppy); - assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}"); - System.out.println(json); - } catch(IOException e) { - e.printStackTrace(); - } + assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}"); } @Test - public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() { + public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() throws IOException { String flowerXML = "PoppyRED9"; - try { - XmlMapper xmlMapper = new XmlMapper(); - JsonNode node = xmlMapper.readTree(flowerXML.getBytes()); + XmlMapper xmlMapper = new XmlMapper(); + JsonNode node = xmlMapper.readTree(flowerXML.getBytes()); - ObjectMapper jsonMapper = new ObjectMapper(); - String json = jsonMapper.writeValueAsString(node); + ObjectMapper jsonMapper = new ObjectMapper(); + String json = jsonMapper.writeValueAsString(node); - System.out.println(json); - - assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}"); - } catch(IOException e) { - e.printStackTrace(); - } + assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}"); } } diff --git a/jib/pom.xml b/jib/pom.xml new file mode 100644 index 0000000000..e71250f157 --- /dev/null +++ b/jib/pom.xml @@ -0,0 +1,57 @@ + + 4.0.0 + jib + 0.1-SNAPSHOT + jib + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + + + 1.8 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + com.google.cloud.tools + jib-maven-plugin + 0.9.10 + + + registry.hub.docker.com/baeldungjib/jib-spring-boot-app + + + + + + + + + spring-releases + https://repo.spring.io/libs-release + + + + + spring-releases + https://repo.spring.io/libs-release + + + diff --git a/jib/src/main/java/com/baeldung/Application.java b/jib/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..8c087476bf --- /dev/null +++ b/jib/src/main/java/com/baeldung/Application.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/jib/src/main/java/com/baeldung/Greeting.java b/jib/src/main/java/com/baeldung/Greeting.java new file mode 100644 index 0000000000..62834d9752 --- /dev/null +++ b/jib/src/main/java/com/baeldung/Greeting.java @@ -0,0 +1,20 @@ +package com.baeldung; + +public class Greeting { + + private final long id; + private final String content; + + public Greeting(long id, String content) { + this.id = id; + this.content = content; + } + + public long getId() { + return id; + } + + public String getContent() { + return content; + } +} \ No newline at end of file diff --git a/jib/src/main/java/com/baeldung/GreetingController.java b/jib/src/main/java/com/baeldung/GreetingController.java new file mode 100644 index 0000000000..0b082b0001 --- /dev/null +++ b/jib/src/main/java/com/baeldung/GreetingController.java @@ -0,0 +1,20 @@ +package com.baeldung; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.concurrent.atomic.AtomicLong; + +@RestController +public class GreetingController { + + private static final String template = "Hello Docker, %s!"; + private final AtomicLong counter = new AtomicLong(); + + @GetMapping("/greeting") + public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { + return new Greeting(counter.incrementAndGet(), + String.format(template, name)); + } +} \ No newline at end of file diff --git a/libraries/pom.xml b/libraries/pom.xml index 6bbe8e6f1c..8ffd33272d 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -711,6 +711,12 @@ ${snakeyaml.version} + + com.numericalmethod + suanshu + ${suanshu.version} + + @@ -731,6 +737,12 @@ Apache Staging https://repository.apache.org/content/groups/staging + + nm-repo + Numerical Method's Maven Repository + http://repo.numericalmethod.com/maven/ + default + @@ -835,6 +847,7 @@ + 4.0.0 1.21 1.23.0 0.1.0 diff --git a/libraries/src/main/java/com/baeldung/suanshu/SuanShuMath.java b/libraries/src/main/java/com/baeldung/suanshu/SuanShuMath.java new file mode 100644 index 0000000000..46af24692d --- /dev/null +++ b/libraries/src/main/java/com/baeldung/suanshu/SuanShuMath.java @@ -0,0 +1,142 @@ +package com.baeldung.suanshu; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.numericalmethod.suanshu.algebra.linear.matrix.doubles.Matrix; +import com.numericalmethod.suanshu.algebra.linear.vector.doubles.Vector; +import com.numericalmethod.suanshu.algebra.linear.vector.doubles.dense.DenseVector; +import com.numericalmethod.suanshu.algebra.linear.matrix.doubles.matrixtype.dense.DenseMatrix; +import com.numericalmethod.suanshu.algebra.linear.matrix.doubles.operation.Inverse; +import com.numericalmethod.suanshu.analysis.function.polynomial.Polynomial; +import com.numericalmethod.suanshu.analysis.function.polynomial.root.PolyRoot; +import com.numericalmethod.suanshu.analysis.function.polynomial.root.PolyRootSolver; +import com.numericalmethod.suanshu.number.complex.Complex; + +class SuanShuMath { + + private static final Logger log = LoggerFactory.getLogger(SuanShuMath.class); + + public static void main(String[] args) throws Exception { + SuanShuMath math = new SuanShuMath(); + + math.addingVectors(); + math.scaleVector(); + math.innerProductVectors(); + math.addingIncorrectVectors(); + + math.addingMatrices(); + math.multiplyMatrices(); + math.multiplyIncorrectMatrices(); + math.inverseMatrix(); + + Polynomial p = math.createPolynomial(); + math.evaluatePolynomial(p); + math.solvePolynomial(); + } + + public void addingVectors() throws Exception { + Vector v1 = new DenseVector(new double[]{1, 2, 3, 4, 5}); + Vector v2 = new DenseVector(new double[]{5, 4, 3, 2, 1}); + Vector v3 = v1.add(v2); + log.info("Adding vectors: {}", v3); + } + + public void scaleVector() throws Exception { + Vector v1 = new DenseVector(new double[]{1, 2, 3, 4, 5}); + Vector v2 = v1.scaled(2.0); + log.info("Scaling a vector: {}", v2); + } + + public void innerProductVectors() throws Exception { + Vector v1 = new DenseVector(new double[]{1, 2, 3, 4, 5}); + Vector v2 = new DenseVector(new double[]{5, 4, 3, 2, 1}); + double inner = v1.innerProduct(v2); + log.info("Vector inner product: {}", inner); + } + + public void addingIncorrectVectors() throws Exception { + Vector v1 = new DenseVector(new double[]{1, 2, 3}); + Vector v2 = new DenseVector(new double[]{5, 4}); + Vector v3 = v1.add(v2); + log.info("Adding vectors: {}", v3); + } + + public void addingMatrices() throws Exception { + Matrix m1 = new DenseMatrix(new double[][]{ + {1, 2, 3}, + {4, 5, 6} + }); + + Matrix m2 = new DenseMatrix(new double[][]{ + {3, 2, 1}, + {6, 5, 4} + }); + + Matrix m3 = m1.add(m2); + log.info("Adding matrices: {}", m3); + } + + public void multiplyMatrices() throws Exception { + Matrix m1 = new DenseMatrix(new double[][]{ + {1, 2, 3}, + {4, 5, 6} + }); + + Matrix m2 = new DenseMatrix(new double[][]{ + {1, 4}, + {2, 5}, + {3, 6} + }); + + Matrix m3 = m1.multiply(m2); + log.info("Multiplying matrices: {}", m3); + } + + public void multiplyIncorrectMatrices() throws Exception { + Matrix m1 = new DenseMatrix(new double[][]{ + {1, 2, 3}, + {4, 5, 6} + }); + + Matrix m2 = new DenseMatrix(new double[][]{ + {3, 2, 1}, + {6, 5, 4} + }); + + Matrix m3 = m1.multiply(m2); + log.info("Multiplying matrices: {}", m3); + } + + public void inverseMatrix() { + Matrix m1 = new DenseMatrix(new double[][]{ + {1, 2}, + {3, 4} + }); + + Inverse m2 = new Inverse(m1); + log.info("Inverting a matrix: {}", m2); + log.info("Verifying a matrix inverse: {}", m1.multiply(m2)); + } + + public Polynomial createPolynomial() { + return new Polynomial(new double[]{3, -5, 1}); + } + + public void evaluatePolynomial(Polynomial p) { + // Evaluate using a real number + log.info("Evaluating a polynomial using a real number: {}", p.evaluate(5)); + // Evaluate using a complex number + log.info("Evaluating a polynomial using a complex number: {}", p.evaluate(new Complex(1, 2))); + } + + public void solvePolynomial() { + Polynomial p = new Polynomial(new double[]{2, 2, -4}); + PolyRootSolver solver = new PolyRoot(); + List roots = solver.solve(p); + log.info("Finding polynomial roots: {}", roots); + } + +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/RequestMethodController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/RequestMethodController.java new file mode 100644 index 0000000000..03cf729ddf --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/RequestMethodController.java @@ -0,0 +1,25 @@ +package com.baeldung.spring.controller; + +import com.baeldung.spring.domain.Employee; +import com.baeldung.spring.exception.InvalidRequestException; +import com.baeldung.spring.service.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping(value="/api") +public class RequestMethodController { + + @Autowired + EmployeeService service; + + @RequestMapping(value = "/employees", produces = "application/json", method={RequestMethod.GET,RequestMethod.POST}) + public List findEmployees() + throws InvalidRequestException { + return service.getEmployeeList(); + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/exception/InvalidRequestException.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/exception/InvalidRequestException.java new file mode 100644 index 0000000000..4dcbe86735 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/exception/InvalidRequestException.java @@ -0,0 +1,26 @@ +package com.baeldung.spring.exception; + +public class InvalidRequestException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = 4088649120307193208L; + + public InvalidRequestException() { + super(); + } + + public InvalidRequestException(final String message, final Throwable cause) { + super(message, cause); + } + + public InvalidRequestException(final String message) { + super(message); + } + + public InvalidRequestException(final Throwable cause) { + super(cause); + } + +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/service/EmployeeService.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/service/EmployeeService.java new file mode 100644 index 0000000000..4d87352c42 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/service/EmployeeService.java @@ -0,0 +1,12 @@ +package com.baeldung.spring.service; + +import com.baeldung.spring.domain.Employee; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public interface EmployeeService { + + List getEmployeeList(); +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/service/EmployeeServiceImpl.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/service/EmployeeServiceImpl.java new file mode 100644 index 0000000000..18f2d70795 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/service/EmployeeServiceImpl.java @@ -0,0 +1,27 @@ +package com.baeldung.spring.service; + +import com.baeldung.spring.domain.Employee; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class EmployeeServiceImpl implements EmployeeService{ + + @Override + public List getEmployeeList() { + List employeeList = new ArrayList<>(); + employeeList.add(createEmployee(100L, "Steve Martin", "333-777-999")); + employeeList.add(createEmployee(200L, "Adam Schawn", "444-111-777")); + return employeeList; + } + + private Employee createEmployee(long id, String name, String contactNumber) { + Employee employee = new Employee(); + employee.setId(id); + employee.setName(name); + employee.setContactNumber(contactNumber); + return employee; + } +}