Merge branch 'masterEugen' into heapDump

This commit is contained in:
Marcos Lopez Gonzalez
2018-09-13 22:38:12 +02:00
368 changed files with 5521 additions and 3709 deletions

View File

@@ -145,3 +145,9 @@
- [Common Java Exceptions](http://www.baeldung.com/java-common-exceptions)
- [Java Constructors vs Static Factory Methods](https://www.baeldung.com/java-constructors-vs-static-factory-methods)
- [Differences Between Final, Finally and Finalize in Java](https://www.baeldung.com/java-final-finally-finalize)
- [Static and Dynamic Binding in Java](https://www.baeldung.com/java-static-dynamic-binding)
- [Java List Initialization in One Line](https://www.baeldung.com/java-init-list-one-line)
- [Difference Between Throw and Throws in Java](https://www.baeldung.com/java-throw-throws)
- [ClassCastException: Arrays$ArrayList cannot be cast to ArrayList](https://www.baeldung.com/java-classcastexception-arrays-arraylist)
- [Throw Exception in Optional in Java 8](https://www.baeldung.com/java-optional-throw-exception)
- [Add a Character to a String at a Given Position](https://www.baeldung.com/java-add-character-to-string)

View File

@@ -0,0 +1,100 @@
package com.baeldung.removingdecimals;
import org.junit.Test;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.concurrent.TimeUnit;
/**
* This benchmark compares some of the approaches to formatting a floating-point
* value into a {@link String} while removing the decimal part.
*
* To run, simply run the {@link RemovingDecimalsManualTest#runBenchmarks()} test
* at the end of this class.
*
* The benchmark takes about 15 minutes to run. Since it is using {@link Mode#Throughput},
* higher numbers mean better performance.
*/
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 5)
@Measurement(iterations = 20)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
public class RemovingDecimalsManualTest {
@Param(value = {"345.56", "345345345.56", "345345345345345345.56"}) double doubleValue;
NumberFormat nf;
DecimalFormat df;
@Setup
public void readyFormatters() {
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
df = new DecimalFormat("#,###");
}
@Benchmark
public String whenCastToInt_thenValueIsTruncated() {
return String.valueOf((int) doubleValue);
}
@Benchmark
public String whenUsingStringFormat_thenValueIsRounded() {
return String.format("%.0f", doubleValue);
}
@Benchmark
public String whenUsingNumberFormat_thenValueIsRounded() {
nf.setRoundingMode(RoundingMode.HALF_UP);
return nf.format(doubleValue);
}
@Benchmark
public String whenUsingNumberFormatWithFloor_thenValueIsTruncated() {
nf.setRoundingMode(RoundingMode.FLOOR);
return nf.format(doubleValue);
}
@Benchmark
public String whenUsingDecimalFormat_thenValueIsRounded() {
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(doubleValue);
}
@Benchmark
public String whenUsingDecimalFormatWithFloor_thenValueIsTruncated() {
df.setRoundingMode(RoundingMode.FLOOR);
return df.format(doubleValue);
}
@Benchmark
public String whenUsingBigDecimalDoubleValue_thenValueIsTruncated() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.FLOOR);
return big.toString();
}
@Benchmark
public String whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.HALF_UP);
return big.toString();
}
@Test
public void runBenchmarks() throws Exception {
Options options = new OptionsBuilder()
.include(this.getClass().getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true).shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}

View File

@@ -0,0 +1,95 @@
package com.baeldung.removingdecimals;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* Tests that demonstrate some different approaches for formatting a
* floating-point value into a {@link String} while removing the decimal part.
*/
public class RemovingDecimalsUnitTest {
private final double doubleValue = 345.56;
@Test
public void whenCastToInt_thenValueIsTruncated() {
String truncated = String.valueOf((int) doubleValue);
assertEquals("345", truncated);
}
@Test
public void givenALargeDouble_whenCastToInt_thenValueIsNotTruncated() {
double outOfIntRange = 6_000_000_000.56;
String truncationAttempt = String.valueOf((int) outOfIntRange);
assertNotEquals("6000000000", truncationAttempt);
}
@Test
public void whenUsingStringFormat_thenValueIsRounded() {
String rounded = String.format("%.0f", doubleValue);
assertEquals("346", rounded);
}
@Test
public void givenALargeDouble_whenUsingStringFormat_thenValueIsStillRounded() {
double outOfIntRange = 6_000_000_000.56;
String rounded = String.format("%.0f", outOfIntRange);
assertEquals("6000000001", rounded);
}
@Test
public void whenUsingNumberFormat_thenValueIsRounded() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
nf.setRoundingMode(RoundingMode.HALF_UP);
String rounded = nf.format(doubleValue);
assertEquals("346", rounded);
}
@Test
public void whenUsingNumberFormatWithFloor_thenValueIsTruncated() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
nf.setRoundingMode(RoundingMode.FLOOR);
String truncated = nf.format(doubleValue);
assertEquals("345", truncated);
}
@Test
public void whenUsingDecimalFormat_thenValueIsRounded() {
DecimalFormat df = new DecimalFormat("#,###");
df.setRoundingMode(RoundingMode.HALF_UP);
String rounded = df.format(doubleValue);
assertEquals("346", rounded);
}
@Test
public void whenUsingDecimalFormatWithFloor_thenValueIsTruncated() {
DecimalFormat df = new DecimalFormat("#,###");
df.setRoundingMode(RoundingMode.FLOOR);
String truncated = df.format(doubleValue);
assertEquals("345", truncated);
}
@Test
public void whenUsingBigDecimalDoubleValue_thenValueIsTruncated() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.FLOOR);
String truncated = big.toString();
assertEquals("345", truncated);
}
@Test
public void whenUsingBigDecimalDoubleValueWithHalfUp_thenValueIsRounded() {
BigDecimal big = new BigDecimal(doubleValue);
big = big.setScale(0, RoundingMode.HALF_UP);
String truncated = big.toString();
assertEquals("346", truncated);
}
}