Merge branch 'eugenp:master' into danielmcnally285_last_n_characters
This commit is contained in:
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.checkiftimebetweentwotimes;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CheckIfTimeBetweenTwoTimesUnitTest {
|
||||
private LocalTime startTime = LocalTime.parse("09:00:00");
|
||||
private LocalTime endTime = LocalTime.parse("17:00:00");
|
||||
private LocalTime targetTime = LocalTime.parse("12:30:00");
|
||||
|
||||
@Test
|
||||
public void givenLocalTime_whenUsingIsAfterIsBefore_thenTimeIsBetween() {
|
||||
assertTrue(!targetTime.isBefore(startTime) && !targetTime.isAfter(endTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalTime_whenUsingCompareTo_thenTimeIsBetween() {
|
||||
assertTrue(targetTime.compareTo(startTime) >= 0 && targetTime.compareTo(endTime) <= 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDate_whenUsingAfterBefore_thenTimeIsBetween() {
|
||||
Calendar startCalendar = Calendar.getInstance();
|
||||
startCalendar.set(Calendar.HOUR_OF_DAY, 9);
|
||||
startCalendar.set(Calendar.MINUTE, 0);
|
||||
Date startTime = startCalendar.getTime();
|
||||
|
||||
Calendar endCalendar = Calendar.getInstance();
|
||||
endCalendar.set(Calendar.HOUR_OF_DAY, 17);
|
||||
endCalendar.set(Calendar.MINUTE, 0);
|
||||
Date endTime = endCalendar.getTime();
|
||||
|
||||
Calendar targetCalendar = Calendar.getInstance();
|
||||
targetCalendar.set(Calendar.HOUR_OF_DAY, 12);
|
||||
targetCalendar.set(Calendar.MINUTE, 30);
|
||||
Date targetTime = targetCalendar.getTime();
|
||||
|
||||
assertTrue(!targetTime.before(startTime) && !targetTime.after(endTime));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
=========
|
||||
|
||||
### Relevant articles:
|
||||
@@ -0,0 +1,15 @@
|
||||
<?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>
|
||||
<artifactId>core-java-lang-math-4</artifactId>
|
||||
<name>core-java-lang-math-4</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
</project>
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.baeldung.percentile;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CalculatePercentileUnitTest {
|
||||
|
||||
public static <T extends Comparable<T>> T getPercentile(Collection<T> input, double percentile) {
|
||||
if (input == null || input.isEmpty()) {
|
||||
throw new IllegalArgumentException("The input dataset cannot be null or empty.");
|
||||
}
|
||||
if (percentile < 0 || percentile > 100) {
|
||||
throw new IllegalArgumentException("Percentile must be between 0 and 100(exclusive)");
|
||||
}
|
||||
List<T> sortedList = input.stream()
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
int rank = percentile == 0 ? 1 : (int) Math.ceil(percentile / 100.0 * input.size());
|
||||
return sortedList.get(rank - 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCallingGetPercentileWithAList_thenGetExpectedResult() {
|
||||
assertThrows(IllegalArgumentException.class, () -> getPercentile(List.of(1, 2, 3), -1));
|
||||
assertThrows(IllegalArgumentException.class, () -> getPercentile(List.of(1, 2, 3), 101));
|
||||
|
||||
List<Integer> list100 = IntStream.rangeClosed(1, 100)
|
||||
.boxed()
|
||||
.collect(Collectors.toList());
|
||||
Collections.shuffle(list100);
|
||||
|
||||
assertEquals(1, getPercentile(list100, 0));
|
||||
assertEquals(10, getPercentile(list100, 10));
|
||||
assertEquals(25, getPercentile(list100, 25));
|
||||
assertEquals(50, getPercentile(list100, 50));
|
||||
assertEquals(76, getPercentile(list100, 75.3));
|
||||
assertEquals(100, getPercentile(list100, 100));
|
||||
|
||||
List<Integer> list8 = IntStream.of(-1, 200, 30, 42, -5, 7, 8, 92)
|
||||
.boxed()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(-5, getPercentile(list8, 0));
|
||||
assertEquals(-5, getPercentile(list8, 10));
|
||||
assertEquals(-1, getPercentile(list8, 25));
|
||||
assertEquals(8, getPercentile(list8, 50));
|
||||
assertEquals(92, getPercentile(list8, 75.3));
|
||||
assertEquals(200, getPercentile(list8, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCallingGetPercentileWithAnArray_thenGetExpectedResult() {
|
||||
|
||||
long[] theArray = new long[] { -1, 200, 30, 42, -5, 7, 8, 92 };
|
||||
|
||||
//convert the long[] array to a List<Long>
|
||||
List<Long> list8 = Arrays.stream(theArray)
|
||||
.boxed()
|
||||
.toList();
|
||||
|
||||
assertEquals(-5, getPercentile(list8, 0));
|
||||
assertEquals(-5, getPercentile(list8, 10));
|
||||
assertEquals(-1, getPercentile(list8, 25));
|
||||
assertEquals(8, getPercentile(list8, 50));
|
||||
assertEquals(92, getPercentile(list8, 75.3));
|
||||
assertEquals(200, getPercentile(list8, 100));
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.integersatbitlevel;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
class IntegersBitLevelUnitTest {
|
||||
|
||||
@Test
|
||||
void givenNumbers_whenBitwiseAND_thenResultIsExpected() {
|
||||
int result = 0b1100 & 0b0111;
|
||||
assertEquals(0b0100, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNumbers_whenBitwiseOR_thenResultIsExpected() {
|
||||
int result = 0b1100 | 0b0111;
|
||||
assertEquals(0b1111, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNumbers_whenBitwiseXOR_thenResultIsExpected() {
|
||||
int result = 0b1100 ^ 0b0111;
|
||||
assertEquals(0b1011, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNumber_whenBitwiseNOT_thenResultIsExpected() {
|
||||
int result = ~0b0101;
|
||||
assertEquals(-0b0110, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNumber_whenBitwiseLeftShift_thenResultIsExpected() {
|
||||
int result = 0b0101 << 2;
|
||||
assertEquals(0b10100, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNumber_whenBitwiseRightShift_thenResultIsExpected() {
|
||||
int result = 0b0101 >> 1;
|
||||
assertEquals(0b10, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenOriginalColor_whenApplyingMask_thenObtainModifiedColor() {
|
||||
int originalColor = 0xFF336699;
|
||||
|
||||
int alphaMask = 0xFF000000;
|
||||
int redMask = 0x00FF0000;
|
||||
int greenMask = 0x0000FF00;
|
||||
int blueMask = 0x000000FF;
|
||||
|
||||
int alpha = (originalColor & alphaMask) >>> 24;
|
||||
int red = (originalColor & redMask) >>> 16;
|
||||
int green = (originalColor & greenMask) >>> 8;
|
||||
int blue = originalColor & blueMask;
|
||||
|
||||
red = Math.min(255, red + 50);
|
||||
green = Math.min(255, green + 30);
|
||||
|
||||
int modifiedColor = (alpha << 24) | (red << 16) | (green << 8) | blue;
|
||||
|
||||
assertEquals(-10124135, modifiedColor);
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<validator.version>1.7</validator.version>
|
||||
<validator.version>1.8.0</validator.version>
|
||||
<emoji-java.version>5.1.1</emoji-java.version>
|
||||
<apache-commons-text.version>1.10.0</apache-commons-text.version>
|
||||
</properties>
|
||||
|
||||
@@ -144,6 +144,7 @@
|
||||
<module>core-java-lang-6</module>
|
||||
<module>core-java-lang-math</module>
|
||||
<module>core-java-lang-math-2</module>
|
||||
<module>core-java-lang-math-4</module>
|
||||
<module>core-java-lang-oop-constructors</module>
|
||||
<module>core-java-lang-oop-patterns</module>
|
||||
<module>core-java-lang-oop-generics</module>
|
||||
|
||||
Reference in New Issue
Block a user