[BAEL-13506] - Split or move core-java-8 module (#7790)

This commit is contained in:
Amit Pandey
2019-09-20 19:00:34 +05:30
committed by Josh Cummings
parent 7d65b1fb24
commit f2cac1ab7c
124 changed files with 640 additions and 51 deletions

View File

@@ -0,0 +1,7 @@
=========
## Core Java 8 Cookbooks and Examples
### Relevant Articles:
- [Java 8 Math New Methods](https://www.baeldung.com/java-8-math)
- [Java 8 Unsigned Arithmetic Support](https://www.baeldung.com/java-unsigned-arithmetic)

View File

@@ -0,0 +1,49 @@
<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>core-java-lang-math</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-lang-math</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-lang-math</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<asspectj.version>1.8.9</asspectj.version>
<!-- plugins -->
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
</properties>
</project>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -0,0 +1,60 @@
package com.baeldung.java8;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.junit.Assert.assertEquals;
public class UnsignedArithmeticUnitTest {
@Test
public void whenDoublingALargeByteNumber_thenOverflow() {
byte b1 = 100;
byte b2 = (byte) (b1 << 1);
assertEquals(-56, b2);
}
@Test
public void whenComparingNumbers_thenNegativeIsInterpretedAsUnsigned() {
int positive = Integer.MAX_VALUE;
int negative = Integer.MIN_VALUE;
int signedComparison = Integer.compare(positive, negative);
assertEquals(1, signedComparison);
int unsignedComparison = Integer.compareUnsigned(positive, negative);
assertEquals(-1, unsignedComparison);
assertEquals(negative, positive + 1);
}
@Test
public void whenDividingNumbers_thenNegativeIsInterpretedAsUnsigned() {
int positive = Integer.MAX_VALUE;
int negative = Integer.MIN_VALUE;
assertEquals(-1, negative / positive);
assertEquals(1, Integer.divideUnsigned(negative, positive));
assertEquals(-1, negative % positive);
assertEquals(1, Integer.remainderUnsigned(negative, positive));
}
@Test
public void whenParsingNumbers_thenNegativeIsInterpretedAsUnsigned() {
Throwable thrown = catchThrowable(() -> Integer.parseInt("2147483648"));
assertThat(thrown).isInstanceOf(NumberFormatException.class);
assertEquals(Integer.MAX_VALUE + 1, Integer.parseUnsignedInt("2147483648"));
}
@Test
public void whenFormattingNumbers_thenNegativeIsInterpretedAsUnsigned() {
String signedString = Integer.toString(Integer.MIN_VALUE);
assertEquals("-2147483648", signedString);
String unsignedString = Integer.toUnsignedString(Integer.MIN_VALUE);
assertEquals("2147483648", unsignedString);
}
}

View File

@@ -0,0 +1,89 @@
package com.baeldung.math;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class MathNewMethodsUnitTest {
@Test
public void whenAddExactToInteger_thenExpectCorrectArithmeticResult() {
assertEquals(150, Math.addExact(100, 50)); // Returns 150
}
@Test
public void whenSubstractExactFromInteger_thenExpectCorrectArithmeticResult() {
assertEquals(50, Math.subtractExact(100, 50)); // Returns 50
}
@Test
public void whenDecrementExactInteger_thenExpectCorrectArithmeticResult() {
assertEquals(99, Math.decrementExact(100)); // Returns 99
}
@Test
public void whenIncrementExactToInteger_thenExpectCorrectArithmeticResult() {
assertEquals(101, Math.incrementExact(100)); // Returns 101
}
@Test
public void whenMultiplyExactTwoIntegers_thenExpectCorrectArithmeticResult() {
assertEquals(500, Math.multiplyExact(100, 5)); // Returns 500
}
@Test
public void whenNegateExactInteger_thenExpectCorrectArithmeticResult() {
assertEquals(-100, Math.negateExact(100)); // Returns -100
}
@Test(expected = ArithmeticException.class)
public void whenAddToMaxInteger_thenThrowsArithmeticException() {
Math.addExact(Integer.MAX_VALUE, 1); // Throws ArithmeticException
}
@Test(expected = ArithmeticException.class)
public void whenDecrementMinInteger_thenThrowsArithmeticException() {
Math.decrementExact(Integer.MIN_VALUE); // Throws ArithmeticException
}
@Test(expected = ArithmeticException.class)
public void whenIncrementMaxLong_thenThrowsArithmeticException() {
Math.incrementExact(Long.MAX_VALUE); // Throws ArithmeticException
}
@Test(expected = ArithmeticException.class)
public void whenMultiplyMaxLong_thenThrowsArithmeticException() {
Math.multiplyExact(Long.MAX_VALUE, 2); // Throws ArithmeticException
}
@Test(expected = ArithmeticException.class)
public void whenNegateMinInteger_thenThrowsArithmeticException() {
Math.negateExact(Integer.MIN_VALUE); // MinInt value: 2.147.483.648, but MaxInt Value: 2.147.483.647 => Throws ArithmeticException
}
@Test(expected = ArithmeticException.class)
public void whenSubstractFromMinInteger_thenThrowsArithmeticException() {
Math.subtractExact(Integer.MIN_VALUE, 1);
}
@Test
public void whenFloorDivTwoIntegers_thenExpectCorrectArithmeticResult() {
assertEquals(3, Math.floorDiv(7, 2)); // Exact quotient is 3.5 so floor(3.5) == 3
assertEquals(-4, Math.floorDiv(-7, 2)); // Exact quotient is -3.5 so floor(-3.5) == -4
}
@Test
public void whenModDivTwoIntegers_thenExpectCorrectArithmeticResult() {
assertEquals(2, Math.floorMod(5, 3)); // Returns 2: floorMod for positive numbers returns the same as % operator
assertEquals(1, Math.floorMod(-5, 3)); // Returns 1 and not 2 because floorDiv(-5, 3) is -2 and not -1 and (-2*3) + (1) = -5
}
@Test
public void whenNextDownOfDouble_thenExpectCorrectNumber() {
double number = 3.0;
double expected = 2.999999999999;
double delta = 0.00000001;
assertEquals(expected, Math.nextDown(number), delta); // The delta defines the accepted error range
}
}

View File

@@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear