Merge branch 'master' into javabeanconstraints

This commit is contained in:
Alejandro Gervasio 2018-10-09 14:56:28 -03:00 committed by GitHub
commit 5a44cfa020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 316 additions and 107 deletions

View File

@ -0,0 +1,41 @@
package com.baeldung.algorithms.insertionsort;
public class InsertionSort {
public static void insertionSortImperative(int[] input) {
for (int i = 1; i < input.length; i++) {
int key = input[i];
int j = i - 1;
while (j >= 0 && input[j] > key) {
input[j + 1] = input[j];
j = j - 1;
}
input[j + 1] = key;
}
}
public static void insertionSortRecursive(int[] input) {
insertionSortRecursive(input, input.length);
}
private static void insertionSortRecursive(int[] input, int i) {
// base case
if (i <= 1) {
return;
}
// sort the first i - 1 elements of the array
insertionSortRecursive(input, i - 1);
// then find the correct position of the element at position i
int key = input[i - 1];
int j = i - 2;
// shifting the elements from their position by 1
while (j >= 0 && input[j] > key) {
input[j + 1] = input[j];
j = j - 1;
}
// inserting the key at the appropriate position
input[j + 1] = key;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.algorithms.insertionsort;
import com.baeldung.algorithms.insertionsort.InsertionSort;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
public class InsertionSortUnitTest {
@Test
public void givenUnsortedArray_whenInsertionSortImperative_thenSortedAsc() {
int[] input = {6, 2, 3, 4, 5, 1};
InsertionSort.insertionSortImperative(input);
int[] expected = {1, 2, 3, 4, 5, 6};
assertArrayEquals("the two arrays are not equal", expected, input);
}
@Test
public void givenUnsortedArray_whenInsertionSortRecursive_thenSortedAsc() {
int[] input = {6, 4, 5, 2, 3, 1};
InsertionSort.insertionSortRecursive(input);
int[] expected = {1, 2, 3, 4, 5, 6};
assertArrayEquals("the two arrays are not equal", expected, input);
}
}

View File

@ -48,6 +48,7 @@
- [Thread Safe LIFO Data Structure Implementations](https://www.baeldung.com/java-lifo-thread-safe)
- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list)
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
- [Differences Between Collection.clear() and Collection.removeAll()](https://www.baeldung.com/java-collection-clear-vs-removeall)
- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
- [Get the Key for a Value from a Java Map](https://www.baeldung.com/java-map-key-from-value)
- [Time Complexity of Java Collections](https://www.baeldung.com/java-collections-complexity)

View File

@ -0,0 +1,48 @@
package com.baeldung.stream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class OutputStreamExamples {
public void fileOutputStreamByteSequence(String file, String data) throws IOException {
byte[] bytes = data.getBytes();
try (OutputStream out = new FileOutputStream(file)) {
out.write(bytes);
}
}
public void fileOutputStreamByteSubSequence(String file, String data) throws IOException {
byte[] bytes = data.getBytes();
try (OutputStream out = new FileOutputStream(file)) {
out.write(bytes, 6, 5);
}
}
public void fileOutputStreamByteSingle(String file, String data) throws IOException {
byte[] bytes = data.getBytes();
try (OutputStream out = new FileOutputStream(file)) {
out.write(bytes[6]);
}
}
public void bufferedOutputStream(String file, String... data) throws IOException {
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
for (String s : data) {
out.write(s.getBytes());
out.write(" ".getBytes());
}
}
}
public void outputStreamWriter(String file, String data) throws IOException {
try (OutputStream out = new FileOutputStream(file); Writer writer = new OutputStreamWriter(out, "UTF-8")) {
writer.write(data);
}
}
}

View File

@ -0,0 +1,76 @@
package com.baeldung.stream;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
public class OutputStreamExamplesTest {
StringBuilder filePath = new StringBuilder();
@Before
public void init() {
filePath.append("src");
filePath.append(File.separator);
filePath.append("test");
filePath.append(File.separator);
filePath.append("resources");
filePath.append(File.separator);
filePath.append("output_file.txt");
}
@Test
public void givenOutputStream_whenWriteSingleByteCalled_thenOutputCreated() throws IOException {
final File file = new File(filePath.toString());
OutputStreamExamples examples = new OutputStreamExamples();
examples.fileOutputStreamByteSingle(filePath.toString(), "Hello World!");
assertTrue(file.exists());
file.delete();
}
@Test
public void givenOutputStream_whenWriteByteSequenceCalled_thenOutputCreated() throws IOException {
final File file = new File(filePath.toString());
OutputStreamExamples examples = new OutputStreamExamples();
examples.fileOutputStreamByteSequence(filePath.toString(), "Hello World!");
assertTrue(file.exists());
file.delete();
}
@Test
public void givenOutputStream_whenWriteByteSubSequenceCalled_thenOutputCreated() throws IOException {
final File file = new File(filePath.toString());
OutputStreamExamples examples = new OutputStreamExamples();
examples.fileOutputStreamByteSubSequence(filePath.toString(), "Hello World!");
assertTrue(file.exists());
file.delete();
}
@Test
public void givenBufferedOutputStream_whenCalled_thenOutputCreated() throws IOException {
final File file = new File(filePath.toString());
OutputStreamExamples examples = new OutputStreamExamples();
examples.bufferedOutputStream(filePath.toString(), "Hello", "World!");
assertTrue(file.exists());
file.delete();
}
@Test
public void givenOutputStreamWriter_whenCalled_thenOutputCreated() throws IOException {
final File file = new File(filePath.toString());
OutputStreamExamples examples = new OutputStreamExamples();
examples.outputStreamWriter(filePath.toString(), "Hello World!");
assertTrue(file.exists());
file.delete();
}
}

View File

@ -0,0 +1,3 @@
### Relevant articles
- [CDI Portable Extension and Flyway](https://www.baeldung.com/cdi-portable-extension)

View File

@ -14,4 +14,5 @@
- [Bootstrapping JPA Programmatically in Java](http://www.baeldung.com/java-bootstrap-jpa)
- [Optimistic Locking in JPA](http://www.baeldung.com/jpa-optimistic-locking)
- [Hibernate Entity Lifecycle](https://www.baeldung.com/hibernate-entity-lifecycle)
- [Mapping A Hibernate Query to a Custom Class](https://www.baeldung.com/hibernate-query-to-custom-class)
- [@JoinColumn Annotation Explained](https://www.baeldung.com/jpa-join-column)

View File

@ -118,7 +118,7 @@
<properties>
<!-- marshalling -->
<jackson.version>2.9.6</jackson.version>
<jackson.version>2.9.7</jackson.version>
<!-- util -->
<commons-lang3.version>3.8</commons-lang3.version>
<joda-time.version>2.10</joda-time.version>

View File

@ -0,0 +1,5 @@
package com.baeldung.jackson.xmlToJson;
public enum Color {
PINK, BLUE, YELLOW, RED;
}

View File

@ -0,0 +1,42 @@
package com.baeldung.jackson.xmlToJson;
public class Flower {
private String name;
private Color color;
private Integer petals;
public Flower() { }
public Flower(String name, Color color, Integer petals) {
this.name = name;
this.color = color;
this.petals = petals;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Integer getPetals() {
return petals;
}
public void setPetals(Integer petals) {
this.petals = petals;
}
}

View File

@ -0,0 +1,56 @@
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;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.IOException;
public class XmlToJsonUnitTest {
@Test
public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() {
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
try {
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));
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();
}
}
@Test
public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() {
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
try {
XmlMapper xmlMapper = new XmlMapper();
JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
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();
}
}
}

View File

@ -5,7 +5,9 @@ import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
@ -26,6 +28,17 @@ public class DateDiffUnitTest {
assertEquals(diff, 6);
}
@Test
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
LocalDate now = LocalDate.now();
LocalDate sixDaysBehind = now.minusDays(6);
Period period = Period.between(now, sixDaysBehind);
int diff = period.getDays();
assertEquals(diff, 6);
}
@Test
public void givenTwoDateTimesInJava8_whenDifferentiating_thenWeGetSix() {

View File

@ -1,5 +0,0 @@
## Relevant articles:
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
- [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api)

View File

@ -1,36 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.baeldung</groupId>
<artifactId>java-difference-date</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>java-difference-date</name>
<description>Difference between two dates in java</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<dependency>
<groupId>com.darwinsys</groupId>
<artifactId>hirondelle-date4j</artifactId>
<version>${hirondelle-date4j.version}</version>
</dependency>
</dependencies>
<properties>
<joda-time.version>2.9.9</joda-time.version>
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
</properties>
</project>

View File

@ -1,61 +0,0 @@
package com.baeldung;
import org.joda.time.DateTime;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
public class DateDiffUnitTest {
@Test
public void givenTwoDatesBeforeJava8_whenDifferentiating_thenWeGetSix() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date firstDate = sdf.parse("06/24/2017");
Date secondDate = sdf.parse("06/30/2017");
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
assertEquals(diff, 6);
}
@Test
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime sixDaysBehind = now.minusDays(6);
Duration duration = Duration.between(now, sixDaysBehind);
long diff = Math.abs(duration.toDays());
assertEquals(diff, 6);
}
@Test
public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() {
DateTime now = DateTime.now();
DateTime sixDaysBehind = now.minusDays(6);
org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind);
long diff = Math.abs(duration.getStandardDays());
assertEquals(diff, 6);
}
@Test
public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());
hirondelle.date4j.DateTime sixDaysBehind = now.minusDays(6);
long diff = Math.abs(now.numDaysFrom(sixDaysBehind));
assertEquals(diff, 6);
}
}

View File

@ -27,6 +27,7 @@
- [Compact Strings in Java 9](http://www.baeldung.com/java-9-compact-string)
- [Java Check a String for Lowercase/Uppercase Letter, Special Character and Digit](https://www.baeldung.com/java-lowercase-uppercase-special-character-digit-regex)
- [Convert java.util.Date to String](https://www.baeldung.com/java-util-date-to-string)
- [Get Substring from String in Java](https://www.baeldung.com/java-substring)
- [Converting a Stack Trace to a String in Java](https://www.baeldung.com/java-stacktrace-to-string)
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)
- [Remove Emojis from a Java String](https://www.baeldung.com/java-string-remove-emojis)

View File

@ -59,9 +59,8 @@
<validation-api.version>2.0.1.Final</validation-api.version>
<hibernate-validator.version>6.0.13.Final</hibernate-validator.version>
<javax.el.version>3.0.0</javax.el.version>
<org.springframework.version>5.0.2.RELEASE</org.springframework.version> <org.springframework.version>5.0.2.RELEASE</org.springframework.version>
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
<javax.el.version>3.0.0</javax.el.version>
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
<junit.version>4.12</junit.version>
<assertj.version>3.11.1</assertj.version>
</properties>

View File

@ -1,3 +1,4 @@
- [Jersey Filters and Interceptors](http://www.baeldung.com/jersey-filters-interceptors)
- [Jersey MVC Support](https://www.baeldung.com/jersey-mvc)
- [Bean Validation in Jersey](https://www.baeldung.com/jersey-bean-validation)
- [Set a Response Body in JAX-RS](https://www.baeldung.com/jax-rs-response)

View File

@ -11,3 +11,4 @@
- [Apache Ignite with Spring Data](http://www.baeldung.com/apache-ignite-spring-data)
- [Guide to JMapper](https://www.baeldung.com/jmapper)
- [A Guide to Apache Crunch](https://www.baeldung.com/apache-crunch)
- [Building a Data Pipeline with Flink and Kafka](https://www.baeldung.com/kafka-flink-data-pipeline)

View File

@ -665,7 +665,6 @@
<module>dubbo</module>
<module>flyway</module>
<!-- <module>grpc</module> --><!-- protobuf-maven-plugin filecopy failure -->
<module>java-difference-date</module>
<!-- <module>JGit</module> --><!-- Unit test failure -->
<module>jni</module>
<module>jooby</module>
@ -1138,7 +1137,6 @@
<module>dubbo</module>
<module>flyway</module>
<!-- <module>grpc</module> --><!-- protobuf-maven-plugin filecopy failure -->
<module>java-difference-date</module>
<!-- <module>JGit</module> --><!-- Unit test failure -->
<module>jni</module>
<module>jooby</module>