Merge pull request #4 from eugenp/master

merge
This commit is contained in:
RanjeetKaur 2018-10-18 00:40:49 +04:00 committed by GitHub
commit 3a7c3af33a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 546 additions and 30 deletions

View File

@ -1,4 +1,4 @@
package com.baeldung.heapsort; package com.baeldung.algorithms.heapsort;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.baeldung.heapsort; package com.baeldung.algorithms.heapsort;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;

View File

@ -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<String> 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));
}
}

View File

@ -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<String> names = new ArrayList<>();
names.add("John");
names.add("Ana");
names.add("Mary");
names.add("Anthony");
names.add("Mark");
Iterator<String> i = names.iterator();
while (i.hasNext()) {
String e = i.next();
if (e.startsWith("A")) {
i.remove();
}
}
System.out.println(String.join(",", names));
}
}

View File

@ -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<String> names = new ArrayList<>();
names.add("John");
names.add("Ana");
names.add("Mary");
names.add("Anthony");
names.add("Mark");
Collection<String> filteredCollection = names
.stream()
.filter(e -> !e.startsWith("A"))
.collect(Collectors.toList());
System.out.println(String.join(",", filteredCollection));
}
}

View File

@ -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<String> names = new ArrayList<>();
names.add("John");
names.add("Ana");
names.add("Mary");
names.add("Anthony");
names.add("Mark");
Map<Boolean, List<String>> 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);
}
}

View File

@ -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<String> names;
Collection<String> expected;
Collection<String> 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<String> 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<String> filteredCollection = names
.stream()
.filter(e -> !e.startsWith("A"))
.collect(Collectors.toList());
assertThat(filteredCollection, is(expected));
}
@Test
public void givenCollectionOfNames_whenUsingStreamAndPartitioningByToFindNamesThatStartWithLetterA_shouldFind3MatchingAnd2NonMatching() {
Map<Boolean, List<String>> 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));
}
}

View File

@ -1,7 +1,5 @@
package com.baeldung.jackson.xmlToJson; package com.baeldung.jackson.xmlToJson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@ -14,43 +12,32 @@ import java.io.IOException;
public class XmlToJsonUnitTest { public class XmlToJsonUnitTest {
@Test @Test
public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() { public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() throws IOException{
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>"; String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
try { XmlMapper xmlMapper = new XmlMapper();
XmlMapper xmlMapper = new XmlMapper(); Flower poppy = xmlMapper.readValue(flowerXML, Flower.class);
Flower poppy = xmlMapper.readValue(flowerXML, Flower.class);
assertEquals(poppy.getName(), "Poppy"); assertEquals(poppy.getName(), "Poppy");
assertEquals(poppy.getColor(), Color.RED); assertEquals(poppy.getColor(), Color.RED);
assertEquals(poppy.getPetals(), new Integer(9)); assertEquals(poppy.getPetals(), new Integer(9));
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(poppy); String json = mapper.writeValueAsString(poppy);
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}"); assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}");
System.out.println(json);
} catch(IOException e) {
e.printStackTrace();
}
} }
@Test @Test
public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() { public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() throws IOException {
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>"; String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
try { XmlMapper xmlMapper = new XmlMapper();
XmlMapper xmlMapper = new XmlMapper(); JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
ObjectMapper jsonMapper = new ObjectMapper(); ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(node); String json = jsonMapper.writeValueAsString(node);
System.out.println(json); assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}");
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}");
} catch(IOException e) {
e.printStackTrace();
}
} }
} }

57
jib/pom.xml Normal file
View File

@ -0,0 +1,57 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jib</artifactId>
<version>0.1-SNAPSHOT</version>
<name>jib</name>
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>0.9.10</version>
<configuration>
<to>
<image>registry.hub.docker.com/baeldungjib/jib-spring-boot-app</image>
</to>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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));
}
}

View File

@ -711,6 +711,12 @@
<version>${snakeyaml.version}</version> <version>${snakeyaml.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.numericalmethod</groupId>
<artifactId>suanshu</artifactId>
<version>${suanshu.version}</version>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>
@ -731,6 +737,12 @@
<id>Apache Staging</id> <id>Apache Staging</id>
<url>https://repository.apache.org/content/groups/staging</url> <url>https://repository.apache.org/content/groups/staging</url>
</repository> </repository>
<repository>
<id>nm-repo</id>
<name>Numerical Method's Maven Repository</name>
<url>http://repo.numericalmethod.com/maven/</url>
<layout>default</layout>
</repository>
</repositories> </repositories>
@ -835,6 +847,7 @@
</build> </build>
<properties> <properties>
<suanshu.version>4.0.0</suanshu.version>
<snakeyaml.version>1.21</snakeyaml.version> <snakeyaml.version>1.21</snakeyaml.version>
<googleclient.version>1.23.0</googleclient.version> <googleclient.version>1.23.0</googleclient.version>
<crdt.version>0.1.0</crdt.version> <crdt.version>0.1.0</crdt.version>

View File

@ -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<? extends Number> roots = solver.solve(p);
log.info("Finding polynomial roots: {}", roots);
}
}

View File

@ -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<Employee> findEmployees()
throws InvalidRequestException {
return service.getEmployeeList();
}
}

View File

@ -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);
}
}

View File

@ -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<Employee> getEmployeeList();
}

View File

@ -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<Employee> getEmployeeList() {
List<Employee> 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;
}
}