[BAEL-9555] - Created a core-java-modules folder
This commit is contained in:
6
core-java-modules/core-java-jvm/README.md
Normal file
6
core-java-modules/core-java-jvm/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
=========
|
||||
|
||||
## Core Java JVM Cookbooks and Examples
|
||||
|
||||
### Relevant Articles:
|
||||
- [Method Inlining in the JVM](https://www.baeldung.com/jvm-method-inlining)
|
||||
41
core-java-modules/core-java-jvm/pom.xml
Normal file
41
core-java-modules/core-java-jvm/pom.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<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-jvm</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>core-java-jvm</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.inlining;
|
||||
|
||||
public class ConsecutiveNumbersSum {
|
||||
|
||||
private long totalSum;
|
||||
private int totalNumbers;
|
||||
|
||||
public ConsecutiveNumbersSum(int totalNumbers) {
|
||||
this.totalNumbers = totalNumbers;
|
||||
}
|
||||
|
||||
public long getTotalSum() {
|
||||
totalSum = 0;
|
||||
for (int i = 1; i <= totalNumbers; i++) {
|
||||
totalSum += i;
|
||||
}
|
||||
return totalSum;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.inlining;
|
||||
|
||||
public class InliningExample {
|
||||
|
||||
public static final int NUMBERS_OF_ITERATIONS = 15000;
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (int i = 1; i < NUMBERS_OF_ITERATIONS; i++) {
|
||||
calculateSum(i);
|
||||
}
|
||||
}
|
||||
|
||||
private static long calculateSum(int n) {
|
||||
return new ConsecutiveNumbersSum(n).getTotalSum();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.inlining;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ConsecutiveNumbersSumUnitTest {
|
||||
|
||||
private static final int TOTAL_NUMBERS = 10;
|
||||
|
||||
@Test
|
||||
public void givenTotalIntegersNumber_whenSumCalculated_thenEquals() {
|
||||
ConsecutiveNumbersSum consecutiveNumbersSum = new ConsecutiveNumbersSum(TOTAL_NUMBERS);
|
||||
long expectedSum = calculateExpectedSum(TOTAL_NUMBERS);
|
||||
|
||||
assertEquals(expectedSum, consecutiveNumbersSum.getTotalSum());
|
||||
}
|
||||
|
||||
private long calculateExpectedSum(int totalNumbers) {
|
||||
return totalNumbers * (totalNumbers + 1) / 2;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user