Skip to content

Commit 6369641

Browse files
committed
Merge branch 'master' of https://github.com/eugenp/tutorials into task/BAEL-8824
2 parents 33e10ee + 6456fa3 commit 6369641

292 files changed

Lines changed: 5989 additions & 1655 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.algorithms.distancebetweenpoints;
2+
3+
import java.awt.geom.Point2D;
4+
5+
public class DistanceBetweenPointsService {
6+
7+
public double calculateDistanceBetweenPoints(
8+
double x1,
9+
double y1,
10+
double x2,
11+
double y2) {
12+
13+
return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
14+
}
15+
16+
public double calculateDistanceBetweenPointsWithHypot(
17+
double x1,
18+
double y1,
19+
double x2,
20+
double y2) {
21+
22+
double ac = Math.abs(y2 - y1);
23+
double cb = Math.abs(x2 - x1);
24+
25+
return Math.hypot(ac, cb);
26+
}
27+
28+
public double calculateDistanceBetweenPointsWithPoint2D(
29+
double x1,
30+
double y1,
31+
double x2,
32+
double y2) {
33+
34+
return Point2D.distance(x1, y1, x2, y2);
35+
36+
}
37+
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.algorithms.linesintersection;
2+
3+
import java.awt.Point;
4+
import java.util.Optional;
5+
6+
public class LinesIntersectionService {
7+
8+
public Optional<Point> calculateIntersectionPoint(double m1, double b1, double m2, double b2) {
9+
10+
if (m1 == m2) {
11+
return Optional.empty();
12+
}
13+
14+
double x = (b2 - b1) / (m1 - m2);
15+
double y = m1 * x + b1;
16+
17+
Point point = new Point();
18+
point.setLocation(x, y);
19+
return Optional.of(point);
20+
}
21+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.algorithms.distancebetweenpoints;
2+
3+
import org.junit.Test;
4+
5+
import com.baeldung.algorithms.distancebetweenpoints.DistanceBetweenPointsService;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class DistanceBetweenPointsServiceUnitTest {
10+
11+
private DistanceBetweenPointsService service = new DistanceBetweenPointsService();
12+
13+
@Test
14+
public void givenTwoPoints_whenCalculateDistanceByFormula_thenCorrect() {
15+
16+
double x1 = 3;
17+
double y1 = 4;
18+
double x2 = 7;
19+
double y2 = 1;
20+
21+
double distance = service.calculateDistanceBetweenPoints(x1, y1, x2, y2);
22+
23+
assertEquals(distance, 5, 0.001);
24+
25+
}
26+
27+
@Test
28+
public void givenTwoPoints_whenCalculateDistanceWithHypot_thenCorrect() {
29+
30+
double x1 = 3;
31+
double y1 = 4;
32+
double x2 = 7;
33+
double y2 = 1;
34+
35+
double distance = service.calculateDistanceBetweenPointsWithHypot(x1, y1, x2, y2);
36+
37+
assertEquals(distance, 5, 0.001);
38+
39+
}
40+
41+
@Test
42+
public void givenTwoPoints_whenCalculateDistanceWithPoint2D_thenCorrect() {
43+
44+
double x1 = 3;
45+
double y1 = 4;
46+
double x2 = 7;
47+
double y2 = 1;
48+
49+
double distance = service.calculateDistanceBetweenPointsWithPoint2D(x1, y1, x2, y2);
50+
51+
assertEquals(distance, 5, 0.001);
52+
53+
}
54+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.algorithms.linesintersection;
2+
3+
import java.awt.Point;
4+
import java.util.Optional;
5+
6+
import org.junit.Test;
7+
import static org.junit.Assert.assertTrue;
8+
import static org.junit.Assert.assertFalse;
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class LinesIntersectionServiceUnitTest {
12+
private LinesIntersectionService service = new LinesIntersectionService();
13+
14+
@Test
15+
public void givenNotParallelLines_whenCalculatePoint_thenPresent() {
16+
17+
double m1 = 0;
18+
double b1 = 0;
19+
double m2 = 1;
20+
double b2 = -1;
21+
22+
Optional<Point> point = service.calculateIntersectionPoint(m1, b1, m2, b2);
23+
24+
assertTrue(point.isPresent());
25+
assertEquals(point.get().getX(), 1, 0.001);
26+
assertEquals(point.get().getY(), 0, 0.001);
27+
}
28+
29+
@Test
30+
public void givenParallelLines_whenCalculatePoint_thenEmpty() {
31+
double m1 = 1;
32+
double b1 = 0;
33+
double m2 = 1;
34+
double b2 = -1;
35+
36+
Optional<Point> point = service.calculateIntersectionPoint(m1, b1, m2, b2);
37+
38+
assertFalse(point.isPresent());
39+
}
40+
}

‎apache-cxf/sse-jaxrs/sse-jaxrs-server/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</properties>
2121

2222
<build>
23-
<finalName>${artifactId}</finalName>
23+
<finalName>${project.artifactId}</finalName>
2424
<plugins>
2525
<plugin>
2626
<groupId>net.wasdev.wlp.maven.plugins</groupId>
Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,91 @@
11
package com.baeldung.test.dependencyinjection;
22

3-
import com.baeldung.dependencyinjection.imagefileeditors.GifFileEditor;
4-
import com.baeldung.dependencyinjection.imagefileeditors.JpgFileEditor;
5-
import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor;
6-
import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor;
7-
import com.baeldung.dependencyinjection.loggers.TimeLogger;
8-
import java.text.SimpleDateFormat;
9-
import java.util.Calendar;
103
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.within;
5+
6+
import java.text.ParseException;
7+
import java.time.LocalTime;
8+
import java.time.temporal.ChronoUnit;
9+
1110
import org.jboss.weld.environment.se.Weld;
1211
import org.jboss.weld.environment.se.WeldContainer;
1312
import org.junit.BeforeClass;
1413
import org.junit.Test;
1514

15+
import com.baeldung.dependencyinjection.imagefileeditors.PngFileEditor;
16+
import com.baeldung.dependencyinjection.imageprocessors.ImageFileProcessor;
17+
import com.baeldung.dependencyinjection.loggers.TimeLogger;
18+
1619
public class ImageProcessorUnitTest {
17-
20+
1821
private static ImageFileProcessor imageFileProcessor;
19-
private static SimpleDateFormat dateFormat;
20-
private static Calendar calendar;
21-
22-
22+
2323
@BeforeClass
2424
public static void setImageProcessorInstance() {
2525
Weld weld = new Weld();
2626
WeldContainer container = weld.initialize();
27-
imageFileProcessor = container.select(ImageFileProcessor.class).get();
27+
imageFileProcessor = container.select(ImageFileProcessor.class)
28+
.get();
2829
container.shutdown();
2930
}
30-
31-
@BeforeClass
32-
public static void setSimpleDateFormatInstance() {
33-
dateFormat = new SimpleDateFormat("HH:mm");
34-
}
35-
36-
@BeforeClass
37-
public static void setCalendarInstance() {
38-
calendar = Calendar.getInstance();
39-
}
40-
31+
4132
@Test
4233
public void givenImageProcessorInstance_whenInjectedPngFileEditorandTimeLoggerInstances_thenTwoAssertions() {
4334
assertThat(imageFileProcessor.getImageFileditor()).isInstanceOf(PngFileEditor.class);
4435
assertThat(imageFileProcessor.getTimeLogger()).isInstanceOf(TimeLogger.class);
4536
}
46-
37+
4738
@Test
48-
public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() {
49-
String currentTime = dateFormat.format(calendar.getTime());
50-
assertThat(imageFileProcessor.openFile("file1.png")).isEqualTo("Opening PNG file file1.png at: " + currentTime);
39+
public void givenImageProcessorInstance_whenCalledopenFile_thenOneAssertion() throws ParseException {
40+
LocalTime currentTime = LocalTime.now();
41+
42+
String openFileLog = imageFileProcessor.openFile("file1.png");
43+
assertThat(openFileLog).contains("Opening PNG file file1.png at: ");
44+
45+
LocalTime loggedTime = getLoggedTime(openFileLog);
46+
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES));
5147
}
52-
48+
5349
@Test
54-
public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() {
55-
String currentTime = dateFormat.format(calendar.getTime());
56-
assertThat(imageFileProcessor.editFile("file1.png")).isEqualTo("Editing PNG file file1.png at: " + currentTime);
50+
public void givenImageProcessorInstance_whenCallededitFile_thenOneAssertion() throws ParseException {
51+
LocalTime currentTime = LocalTime.now();
52+
53+
String editFileLog = imageFileProcessor.editFile("file1.png");
54+
assertThat(editFileLog).contains("Editing PNG file file1.png at: ");
55+
56+
LocalTime loggedTime = getLoggedTime(editFileLog);
57+
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES));
5758
}
58-
59+
5960
@Test
60-
public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() {
61-
String currentTime = dateFormat.format(calendar.getTime());
62-
assertThat(imageFileProcessor.writeFile("file1.png")).isEqualTo("Writing PNG file file1.png at: " + currentTime);
61+
public void givenImageProcessorInstance_whenCalledwriteFile_thenOneAssertion() throws ParseException {
62+
LocalTime currentTime = LocalTime.now();
63+
64+
String writeFileLog = imageFileProcessor.writeFile("file1.png");
65+
assertThat(writeFileLog).contains("Writing PNG file file1.png at: ");
66+
67+
LocalTime loggedTime = getLoggedTime(writeFileLog);
68+
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES));
6369
}
64-
70+
6571
@Test
66-
public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() {
67-
String currentTime = dateFormat.format(calendar.getTime());
68-
assertThat(imageFileProcessor.saveFile("file1.png")).isEqualTo("Saving PNG file file1.png at: " + currentTime);
72+
public void givenImageProcessorInstance_whenCalledsaveFile_thenOneAssertion() throws ParseException {
73+
LocalTime currentTime = LocalTime.now();
74+
75+
String saveFileLog = imageFileProcessor.saveFile("file1.png");
76+
assertThat(saveFileLog).contains("Saving PNG file file1.png at: ");
77+
78+
LocalTime loggedTime = getLoggedTime(saveFileLog);
79+
assertThat(loggedTime).isCloseTo(currentTime, within(2, ChronoUnit.MINUTES));
80+
}
81+
82+
private LocalTime getLoggedTime(String log) throws ParseException {
83+
String logTimeString = log.split("at: ")[1];
84+
85+
int hour = Integer.valueOf(logTimeString.split(":")[0]);
86+
int minutes = Integer.valueOf(logTimeString.split(":")[1]);
87+
88+
LocalTime loggedTime = LocalTime.of(hour, minutes);
89+
return loggedTime;
6990
}
7091
}

‎core-java-8/README.md‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,17 @@
1515
- [Guide to Java 8 Comparator.comparing()](http://www.baeldung.com/java-8-comparator-comparing)
1616
- [Guide To Java 8 Optional](http://www.baeldung.com/java-optional)
1717
- [Guide to the Java 8 forEach](http://www.baeldung.com/foreach-java)
18-
- [Finding Max/Min of a List or Collection](http://www.baeldung.com/java-collection-min-max)
1918
- [Java Base64 Encoding and Decoding](http://www.baeldung.com/java-base64-encode-and-decode)
2019
- [The Difference Between map() and flatMap()](http://www.baeldung.com/java-difference-map-and-flatmap)
21-
- [Copy a File with Java](http://www.baeldung.com/java-copy-file)
2220
- [Static and Default Methods in Interfaces in Java](http://www.baeldung.com/java-static-default-methods)
2321
- [Efficient Word Frequency Calculator in Java](http://www.baeldung.com/java-word-frequency)
24-
- [Fail-Safe Iterator vs Fail-Fast Iterator](http://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator)
25-
- [Shuffling Collections In Java](http://www.baeldung.com/java-shuffle-collection)
2622
- [Introduction to Spliterator in Java](http://www.baeldung.com/java-spliterator)
2723
- [Java 8 Math New Methods](http://www.baeldung.com/java-8-math)
2824
- [Overview of Java Built-in Annotations](http://www.baeldung.com/java-default-annotations)
2925
- [Finding Min/Max in an Array with Java](http://www.baeldung.com/java-array-min-max)
3026
- [Internationalization and Localization in Java 8](http://www.baeldung.com/java-8-localization)
31-
- [How to Find an Element in a List with Java](http://www.baeldung.com/find-list-element-java)
3227
- [Java Optional – orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get)
33-
- [An Introduction to Java.util.Hashtable Class](http://www.baeldung.com/java-hash-table)
3428
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
3529
- [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic)
3630
- [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference)
37-
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
3831
- [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.optional;
2+
3+
public class PersonRepository {
4+
5+
public String findNameById(String id) {
6+
return id == null ? null : "Name";
7+
}
8+
9+
}

‎core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest.java‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,22 @@
55
*/
66
package com.baeldung.nullsafecollectionstreams;
77

8+
import static org.junit.Assert.assertEquals;
9+
810
import java.util.Arrays;
911
import java.util.Collection;
1012
import java.util.Iterator;
1113
import java.util.stream.Stream;
12-
import org.junit.After;
13-
import org.junit.AfterClass;
14-
import org.junit.Before;
15-
import org.junit.BeforeClass;
14+
1615
import org.junit.Test;
17-
import static org.junit.Assert.*;
1816

1917
/**
2018
*
2119
* @author Kwaje Anthony <kwajeanthony@gmail.com>
2220
*/
2321
public class NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest {
24-
25-
private final NullSafeCollectionStreamsUsingJava8OptionalContainer instance =
26-
new NullSafeCollectionStreamsUsingJava8OptionalContainer();
22+
23+
private final NullSafeCollectionStreamsUsingJava8OptionalContainer instance = new NullSafeCollectionStreamsUsingJava8OptionalContainer();
2724

2825
@Test
2926
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
@@ -49,5 +46,5 @@ private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
4946
assertEquals(iter1.next(), iter2.next());
5047
assert !iter1.hasNext() && !iter2.hasNext();
5148
}
52-
49+
5350
}

0 commit comments

Comments
 (0)