Merge branch 'eugenp:master' into master

This commit is contained in:
rvsathe
2021-05-21 15:11:13 +05:30
190 changed files with 1113 additions and 1258 deletions
@@ -7,3 +7,4 @@ This module contains articles about Java 11 core features
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
- [Guide to Java 8s Collectors](https://www.baeldung.com/java-8-collectors)
- [New Features in Java 11](https://www.baeldung.com/java-11-new-features)
- [Getting the Java Version at Runtime](https://www.baeldung.com/get-java-version-runtime)
+1 -1
View File
@@ -34,4 +34,4 @@
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
</project>
</project>
@@ -7,7 +7,7 @@
<version>0.1.0-SNAPSHOT</version>
<name>core-java-9-streams</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
@@ -25,4 +25,4 @@
</resources>
</build>
</project>
</project>
@@ -5,3 +5,4 @@ This module contains articles about core features in the Java language
- [The Java final Keyword Impact on Performance](https://www.baeldung.com/java-final-performance)
- [The package-info.java File](https://www.baeldung.com/java-package-info)
- [What are Compile-time Constants in Java?](https://www.baeldung.com/java-compile-time-constants)
- [Java Objects.hash() vs Objects.hashCode()](https://www.baeldung.com/java-objects-hash-vs-objects-hashcode)
@@ -7,3 +7,4 @@ This module contains articles about Object-oriented programming (OOP) patterns i
- [Inheritance and Composition (Is-a vs Has-a relationship) in Java](https://www.baeldung.com/java-inheritance-composition)
- [Immutable Objects in Java](https://www.baeldung.com/java-immutable-object)
- [How to Make a Deep Copy of an Object in Java](https://www.baeldung.com/java-deep-copy)
- [Using an Interface vs. Abstract Class in Java](https://www.baeldung.com/java-interface-vs-abstract-class)
@@ -0,0 +1,54 @@
package com.baeldung.ignore.pattern.metacharacters;
import static org.junit.Assert.assertEquals;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
public class IgnoringPatternMetacharactersUnitTest {
private static final String dollarAmounts = "$100.25, $100.50, $150.50, $100.50, $100.75";
private static final String patternStr = "$100.50";
@Test
public void givenPatternStringHasMetacharacters_whenPatternMatchedWithoutEscapingMetacharacters_thenNoMatchesFound() {
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(dollarAmounts);
int matches = 0;
while (matcher.find()) {
matches++;
}
assertEquals(0, matches);
}
@Test
public void givenPatternStringHasMetacharacters_whenPatternCompiledUsingManuallyMetaEscapedPattern_thenMatchingSuccessful() {
String metaEscapedPatternStr = "\\Q" + patternStr + "\\E";
Pattern pattern = Pattern.compile(metaEscapedPatternStr);
Matcher matcher = pattern.matcher(dollarAmounts);
int matches = 0;
while (matcher.find()) {
matches++;
}
assertEquals(2, matches);
}
@Test
public void givenPatternStringHasMetacharacters_whenPatternCompiledUsingLiteralPatternFromQuote_thenMatchingSuccessful() {
String literalPatternStr = Pattern.quote(patternStr);
Pattern pattern = Pattern.compile(literalPatternStr);
Matcher matcher = pattern.matcher(dollarAmounts);
int matches = 0;
while (matcher.find()) {
matches++;
}
assertEquals(2, matches);
}
}
@@ -12,4 +12,5 @@ This module contains articles about the Stream API in Java.
- [Should We Close a Java Stream?](https://www.baeldung.com/java-stream-close)
- [Returning Stream vs. Collection](https://www.baeldung.com/java-return-stream-collection)
- [Convert a Java Enumeration Into a Stream](https://www.baeldung.com/java-enumeration-to-stream)
- [When to Use a Parallel Stream in Java](https://www.baeldung.com/java-when-to-use-parallel-stream)
- More articles: [[<-- prev>]](/../core-java-streams-2)
@@ -6,4 +6,5 @@ This module contains articles about string conversions from/to another type.
- [Java String Conversions](https://www.baeldung.com/java-string-conversions)
- [Convert String to Byte Array and Reverse in Java](https://www.baeldung.com/java-string-to-byte-array)
- [Convert Character Array to String in Java](https://www.baeldung.com/java-char-array-to-string)
- [Converting String to BigDecimal in Java](https://www.baeldung.com/java-string-to-bigdecimal)
- More articles: [[<-- prev]](/core-java-string-conversions)
@@ -0,0 +1,74 @@
package com.baeldung.stringtobigdecimal;
import static org.junit.Assert.assertEquals;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import org.junit.Test;
public class StringToBigDecimalConversionUnitTest {
@Test
public void givenValidString_WhenBigDecimalObjectWithStringParameter_ThenResultIsDecimalObject() {
BigDecimal bigDecimal = new BigDecimal("123");
assertEquals(new BigDecimal(123), bigDecimal);
}
@Test(expected = NullPointerException.class)
public void givenNullString_WhenBigDecimalObjectWithStringParameter_ThenNullPointerExceptionIsThrown() {
String bigDecimal = null;
new BigDecimal(bigDecimal);
}
@Test(expected = NumberFormatException.class)
public void givenInalidString_WhenBigDecimalObjectWithStringParameter_ThenNumberFormatExceptionIsThrown() {
new BigDecimal("&");
}
@Test
public void givenValidString_WhenValueOfDoubleFromString_ThenResultIsDecimalObject() {
BigDecimal bigDecimal = BigDecimal.valueOf(Double.valueOf("123.42"));
assertEquals(new BigDecimal(123.42).setScale(2, BigDecimal.ROUND_HALF_UP), bigDecimal);
}
@Test(expected = NullPointerException.class)
public void givenNullString_WhenValueOfDoubleFromString_ThenNullPointerExceptionIsThrown() {
BigDecimal.valueOf(Double.valueOf(null));
}
@Test(expected = NumberFormatException.class)
public void givenInalidString_WhenValueOfDoubleFromString_ThenNumberFormatExceptionIsThrown() {
BigDecimal.valueOf(Double.valueOf("&"));
}
@Test
public void givenValidString_WhenDecimalFormatOfString_ThenResultIsDecimalObject() throws ParseException {
BigDecimal bigDecimal = new BigDecimal(10692467440017.111).setScale(3, BigDecimal.ROUND_HALF_UP);
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator(',');
symbols.setDecimalSeparator('.');
String pattern = "#,##0.0#";
DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
decimalFormat.setParseBigDecimal(true);
// parse the string value
BigDecimal parsedStringValue = (BigDecimal) decimalFormat.parse("10,692,467,440,017.111");
assertEquals(bigDecimal, parsedStringValue);
}
@Test(expected = NullPointerException.class)
public void givenNullString_WhenDecimalFormatOfString_ThenNullPointerExceptionIsThrown() throws ParseException {
new DecimalFormat("#").parse(null);
}
@Test(expected = ParseException.class)
public void givenInalidString_WhenDecimalFormatOfString_ThenNumberFormatExceptionIsThrown() throws ParseException {
new DecimalFormat("#").parse("&");
}
}
@@ -3,3 +3,4 @@
- [Version Comparison in Java](https://www.baeldung.com/java-comparing-versions)
- [Java (String) or .toString()?](https://www.baeldung.com/java-string-casting-vs-tostring)
- [Split Java String by Newline](https://www.baeldung.com/java-string-split-by-newline)
- [Split a String in Java and Keep the Delimiters](https://www.baeldung.com/java-split-string-keep-delimiters)