Skip to content

Commit 3a59756

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents 409f252 + 2a66773 commit 3a59756

215 files changed

Lines changed: 5578 additions & 1983 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: 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+
}

core-java/src/test/java/com/baeldung/linesintersection/LinesIntersectionServiceUnitTest.java renamed to algorithms/src/test/java/com/baeldung/algorithms/linesintersection/LinesIntersectionServiceUnitTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.linesintersection;
1+
package com.baeldung.algorithms.linesintersection;
22

33
import java.awt.Point;
44
import java.util.Optional;
@@ -14,24 +14,24 @@ public class LinesIntersectionServiceUnitTest {
1414
@Test
1515
public void givenNotParallelLines_whenCalculatePoint_thenPresent() {
1616

17-
float m1 = 0;
18-
float b1 = 0;
19-
float m2 = 1;
20-
float b2 = -1;
17+
double m1 = 0;
18+
double b1 = 0;
19+
double m2 = 1;
20+
double b2 = -1;
2121

2222
Optional<Point> point = service.calculateIntersectionPoint(m1, b1, m2, b2);
2323

2424
assertTrue(point.isPresent());
25-
assertEquals(point.get().x, 1);
26-
assertEquals(point.get().y, 0);
25+
assertEquals(point.get().getX(), 1, 0.001);
26+
assertEquals(point.get().getY(), 0, 0.001);
2727
}
2828

2929
@Test
3030
public void givenParallelLines_whenCalculatePoint_thenEmpty() {
31-
float m1 = 1;
32-
float b1 = 0;
33-
float m2 = 1;
34-
float b2 = -1;
31+
double m1 = 1;
32+
double b1 = 0;
33+
double m2 = 1;
34+
double b2 = -1;
3535

3636
Optional<Point> point = service.calculateIntersectionPoint(m1, b1, m2, b2);
3737

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)

‎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
}

‎core-java-8/src/test/java/com/baeldung/streamordering/BenchmarkUnitTest.java‎

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)