Merge branch 'eugenp:master' into master
This commit is contained in:
+65
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.algorithms.pixelarray;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.DataBufferByte;
|
||||
public class GetPixelArray {
|
||||
|
||||
public static int[][] get2DPixelArraySlow(BufferedImage sampleImage) {
|
||||
int width = sampleImage.getWidth();
|
||||
int height = sampleImage.getHeight();
|
||||
int[][] result = new int[height][width];
|
||||
|
||||
for (int row = 0; row < height; row++) {
|
||||
for (int col = 0; col < width; col++) {
|
||||
result[row][col] = sampleImage.getRGB(col, row);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int[][] get2DPixelArrayFast(BufferedImage image) {
|
||||
final byte[] pixelData = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
||||
final int width = image.getWidth();
|
||||
final int height = image.getHeight();
|
||||
final boolean hasAlphaChannel = image.getAlphaRaster() != null;
|
||||
|
||||
int[][] result = new int[height][width];
|
||||
if (hasAlphaChannel) {
|
||||
final int numberOfValues = 4;
|
||||
for (int valueIndex = 0, row = 0, col = 0; valueIndex + numberOfValues - 1 < pixelData.length; valueIndex += numberOfValues) {
|
||||
// Getting the values for each pixel from the pixelData array.
|
||||
int argb = 0;
|
||||
argb += (((int) pixelData[valueIndex] & 0xff) << 24); // alpha value
|
||||
argb += ((int) pixelData[valueIndex + 1] & 0xff); // blue value
|
||||
argb += (((int) pixelData[valueIndex + 2] & 0xff) << 8); // green value
|
||||
argb += (((int) pixelData[valueIndex + 3] & 0xff) << 16); // red value
|
||||
result[row][col] = argb;
|
||||
|
||||
col++;
|
||||
if (col == width) {
|
||||
col = 0;
|
||||
row++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
final int numberOfValues = 3;
|
||||
for (int valueIndex = 0, row = 0, col = 0; valueIndex + numberOfValues - 1 < pixelData.length; valueIndex += numberOfValues) {
|
||||
int argb = 0;
|
||||
argb += -16777216; // 255 alpha value (fully opaque)
|
||||
argb += ((int) pixelData[valueIndex] & 0xff); // blue value
|
||||
argb += (((int) pixelData[valueIndex + 1] & 0xff) << 8); // green value
|
||||
argb += (((int) pixelData[valueIndex + 2] & 0xff) << 16); // red value
|
||||
result[row][col] = argb;
|
||||
|
||||
col++;
|
||||
if (col == width) {
|
||||
col = 0;
|
||||
row++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.algorithms.pixelarray;
|
||||
|
||||
import static com.baeldung.algorithms.pixelarray.GetPixelArray.get2DPixelArrayFast;
|
||||
import static com.baeldung.algorithms.pixelarray.GetPixelArray.get2DPixelArraySlow;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class GetPixelArrayUnitTest {
|
||||
@Test
|
||||
public void givenImage_whenGetPixelArray_thenBothMethodsReturnEqualValues() {
|
||||
BufferedImage sampleImage = null;
|
||||
try {
|
||||
sampleImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
int[][] firstResult = get2DPixelArraySlow(sampleImage);
|
||||
int[][] secondResult = get2DPixelArrayFast(sampleImage);
|
||||
|
||||
assertTrue(Arrays.deepEquals(firstResult, secondResult));
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.arrayindex;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
class ArrayIndex {
|
||||
static int forLoop(int[] numbers, int target) {
|
||||
for (int index = 0; index < numbers.length; index++) {
|
||||
if (numbers[index] == target) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int listIndexOf(Integer[] numbers, int target) {
|
||||
List<Integer> list = Arrays.asList(numbers);
|
||||
return list.indexOf(target);
|
||||
}
|
||||
|
||||
static int intStream(int[] numbers, int target) {
|
||||
return IntStream.range(0, numbers.length)
|
||||
.filter(i -> numbers[i] == target)
|
||||
.findFirst()
|
||||
.orElse(-1);
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package com.baeldung.arrayindex;
|
||||
|
||||
import static com.baeldung.arrayindex.ArrayIndex.forLoop;
|
||||
import static com.baeldung.arrayindex.ArrayIndex.intStream;
|
||||
import static com.baeldung.arrayindex.ArrayIndex.listIndexOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
class ArrayIndexUnitTest {
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseForLoop_thenWillGetElementIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, forLoop(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseForLoop_thenWillGetElementMinusOneIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, forLoop(numbers, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseIndexOf_thenWillGetElementIndex() {
|
||||
Integer[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, listIndexOf(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseIndexOf_thenWillGetElementMinusOneIndex() {
|
||||
Integer[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, listIndexOf(numbers, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseIntStream_thenWillGetElementIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, intStream(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseIntStream_thenWillGetElementMinusOneIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, intStream(numbers, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseBinarySearch_thenWillGetElementIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, Arrays.binarySearch(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseBinarySearch_thenWillGetUpperBoundMinusIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-6, Arrays.binarySearch(numbers, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseBinarySearch_thenWillGetInArrayMinusIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-2, Arrays.binarySearch(numbers, 15));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseBinarySearch_thenWillGetLowerBoundMinusIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, Arrays.binarySearch(numbers, -15));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseApacheCommons_thenWillGetElementIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, ArrayUtils.indexOf(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseApacheCommonsStartingFromIndex_thenWillGetNegativeIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, ArrayUtils.indexOf(numbers, 30, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseApacheCommons_thenWillGetElementMinusOneIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, ArrayUtils.indexOf(numbers, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseGuavaInts_thenWillGetElementIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(2, Ints.indexOf(numbers, 30));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenIntegerArray_whenUseGuavaInts_thenWillGetElementMinusOneIndex() {
|
||||
int[] numbers = { 10, 20, 30, 40, 50 };
|
||||
assertEquals(-1, Ints.indexOf(numbers, 100));
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.charandstring;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class DifferenceBetweenCharAndStringUnitTest {
|
||||
|
||||
@Test
|
||||
void whenPlusTwoChars_thenGetSumAsInteger() {
|
||||
char h = 'H'; // the value is 72
|
||||
char i = 'i'; // the value is 105
|
||||
assertEquals(177, h + i);
|
||||
assertInstanceOf(Integer.class, h + i);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenPlusTwoStrings_thenConcatenateThem() {
|
||||
String i = "i";
|
||||
String h = "H";
|
||||
assertEquals("Hi", h + i);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenPlusCharsAndStrings_thenGetExpectedValues() {
|
||||
char c = 'C';
|
||||
assertEquals("C", "" + c);
|
||||
|
||||
char h = 'H'; // the value is 72
|
||||
char i = 'i'; // the value is 105
|
||||
assertEquals("Hi", "" + h + i);
|
||||
assertEquals("Hi", h + "" + i);
|
||||
assertEquals("177", h + i + "");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenStringChars_thenGetCharArray() {
|
||||
char h = 'h';
|
||||
char e = 'e';
|
||||
char l = 'l';
|
||||
char o = 'o';
|
||||
|
||||
String hello = "hello";
|
||||
assertEquals(h, hello.charAt(0));
|
||||
assertEquals(e, hello.charAt(1));
|
||||
assertEquals(l, hello.charAt(2));
|
||||
assertEquals(l, hello.charAt(3));
|
||||
assertEquals(o, hello.charAt(4));
|
||||
|
||||
char[] chars = new char[] { h, e, l, l, o };
|
||||
char[] charsFromString = hello.toCharArray();
|
||||
assertArrayEquals(chars, charsFromString);
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,4 @@ This module contains articles about core Java input/output(IO) APIs.
|
||||
- [Constructing a Relative Path From Two Absolute Paths in Java](https://www.baeldung.com/java-relative-path-absolute)
|
||||
- [Java Scanner Taking a Character Input](https://www.baeldung.com/java-scanner-character-input)
|
||||
- [Get the Desktop Path in Java](https://www.baeldung.com/java-desktop-path)
|
||||
|
||||
- [Integer.parseInt(scanner.nextLine()) and scanner.nextInt() in Java](https://www.baeldung.com/java-scanner-integer)
|
||||
|
||||
+2
-2
@@ -49,7 +49,7 @@ public class JavaInputStreamToXUnitTest {
|
||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||
|
||||
final StringBuilder textBuilder = new StringBuilder();
|
||||
try (Reader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
|
||||
try (Reader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
|
||||
int c;
|
||||
while ((c = reader.read()) != -1) {
|
||||
textBuilder.append((char) c);
|
||||
@@ -63,7 +63,7 @@ public class JavaInputStreamToXUnitTest {
|
||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||
|
||||
final String text = new BufferedReader(new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())))
|
||||
final String text = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
|
||||
.lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## Core Java Lang OOP - Constructors - Part 2
|
||||
|
||||
This module contains article about constructors in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- [Different Ways to Create an Object in Java](https://www.baeldung.com/java-different-ways-to-create-objects)
|
||||
- More articles: [[<-- Prev]](/core-java-modules/core-java-lang-oop-constructors)
|
||||
@@ -0,0 +1,16 @@
|
||||
<?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-oop-constructors-2</artifactId>
|
||||
<name>core-java-lang-oop-constructors-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
</project>
|
||||
@@ -13,4 +13,4 @@ This module contains article about constructors in Java
|
||||
- [Constructor Specification in Java](https://www.baeldung.com/java-constructor-specification)
|
||||
- [Static vs. Instance Initializer Block in Java](https://www.baeldung.com/java-static-instance-initializer-blocks)
|
||||
- [Accessing Private Constructor in Java](https://www.baeldung.com/java-private-constructor-access)
|
||||
- [Different Ways to Create an Object in Java](https://www.baeldung.com/java-different-ways-to-create-objects)
|
||||
- More articles: [[next -->]](/core-java-modules/core-java-lang-oop-constructors-2)
|
||||
@@ -11,3 +11,4 @@ This module contains articles about methods in Java
|
||||
- [The Covariant Return Type in Java](https://www.baeldung.com/java-covariant-return-type)
|
||||
- [Does a Method’s Signature Include the Return Type in Java?](https://www.baeldung.com/java-method-signature-return-type)
|
||||
- [Solving the Hide Utility Class Public Constructor Sonar Warning](https://www.baeldung.com/java-sonar-hide-implicit-constructor)
|
||||
- [Best Practices for Passing Many Arguments to a Method in Java](https://www.baeldung.com/java-best-practices-many-parameters-method)
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
- [Validating URL in Java](https://www.baeldung.com/java-validate-url)
|
||||
- [Validating IPv4 Address in Java](https://www.baeldung.com/java-validate-ipv4-address)
|
||||
- [Download a Webpage in Java](https://www.baeldung.com/java-download-webpage)
|
||||
- [URL Query Manipulation in Java](https://www.baeldung.com/java-url-query-manipulation)
|
||||
|
||||
@@ -13,3 +13,4 @@ This module contains articles about performance of Java applications
|
||||
- [Capturing a Java Thread Dump](https://www.baeldung.com/java-thread-dump)
|
||||
- [JMX Ports](https://www.baeldung.com/jmx-ports)
|
||||
- [Calling JMX MBean Method From a Shell Script](https://www.baeldung.com/jmx-mbean-shell-access)
|
||||
- [External Debugging With JMXTerm](https://www.baeldung.com/java-jmxterm-external-debugging)
|
||||
|
||||
@@ -5,4 +5,5 @@
|
||||
- [Converting Camel Case and Title Case to Words in Java](https://www.baeldung.com/java-camel-case-title-case-to-words)
|
||||
- [How to Use Regular Expressions to Replace Tokens in Strings in Java](https://www.baeldung.com/java-regex-token-replacement)
|
||||
- [Creating a Java Array from Regular Expression Matches](https://www.baeldung.com/java-array-regex-matches)
|
||||
- [Getting the Text That Follows After the Regex Match in Java](https://www.baeldung.com/java-regex-text-after-match)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-regex)
|
||||
|
||||
@@ -10,3 +10,4 @@ This module contains articles about string-related algorithms.
|
||||
- [Check if the First Letter of a String is Uppercase](https://www.baeldung.com/java-check-first-letter-uppercase)
|
||||
- [Find the First Non Repeating Character in a String in Java](https://www.baeldung.com/java-find-the-first-non-repeating-character)
|
||||
- [Find the First Embedded Occurrence of an Integer in a Java String](https://www.baeldung.com/java-string-find-embedded-integer)
|
||||
- [Find the Most Frequent Characters in a String](https://www.baeldung.com/java-string-find-most-frequent-characters)
|
||||
|
||||
+37
-1
@@ -1,5 +1,9 @@
|
||||
package com.baeldung.reverse;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class ReverseStringExamples {
|
||||
@@ -46,11 +50,43 @@ public class ReverseStringExamples {
|
||||
}
|
||||
|
||||
return output.toString()
|
||||
.trim();
|
||||
.trim();
|
||||
}
|
||||
|
||||
public static String reverseTheOrderOfWordsUsingApacheCommons(String sentence) {
|
||||
return StringUtils.reverseDelimited(sentence, ' ');
|
||||
}
|
||||
|
||||
public static String reverseUsingIntStreamRangeMethod(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
char[] charArray = str.toCharArray();
|
||||
return IntStream.range(0, str.length())
|
||||
.mapToObj(i -> charArray[str.length() - i - 1])
|
||||
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
|
||||
.toString();
|
||||
}
|
||||
|
||||
public static String reverseUsingStreamOfMethod(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Stream.of(str)
|
||||
.map(string -> new StringBuilder(string).reverse())
|
||||
.collect(Collectors.joining());
|
||||
}
|
||||
|
||||
public static String reverseUsingCharsMethod(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return str.chars()
|
||||
.mapToObj(c -> (char) c)
|
||||
.reduce("", (a, b) -> b + a, (a2, b2) -> b2 + a2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+41
-7
@@ -1,10 +1,11 @@
|
||||
package com.baeldung.reverse;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ReverseStringExamplesUnitTest {
|
||||
|
||||
private static final String STRING_INPUT = "cat";
|
||||
@@ -19,7 +20,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverse(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@@ -30,7 +31,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingStringBuilder(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@@ -41,7 +42,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingApacheCommons(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@@ -52,7 +53,7 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseTheOrderOfWords(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(REVERSED_WORDS_SENTENCE, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@@ -63,7 +64,40 @@ public class ReverseStringExamplesUnitTest {
|
||||
String reversedEmpty = ReverseStringExamples.reverseTheOrderOfWordsUsingApacheCommons(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(REVERSED_WORDS_SENTENCE, reversed);
|
||||
assertEquals(null, reversedNull);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseStringUsingIntStreamRangeMethod_ThenCorrectStringIsReturned() {
|
||||
String reversed = ReverseStringExamples.reverseUsingIntStreamRangeMethod(STRING_INPUT);
|
||||
String reversedNull = ReverseStringExamples.reverseUsingIntStreamRangeMethod(null);
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingIntStreamRangeMethod(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseStringUsingCharsMethod_ThenCorrectStringIsReturned() {
|
||||
String reversed = ReverseStringExamples.reverseUsingCharsMethod(STRING_INPUT);
|
||||
String reversedNull = ReverseStringExamples.reverseUsingCharsMethod(null);
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingCharsMethod(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReverseStringUsingStreamOfMethod_ThenCorrectStringIsReturned() {
|
||||
String reversed = ReverseStringExamples.reverseUsingStreamOfMethod(STRING_INPUT);
|
||||
String reversedNull = ReverseStringExamples.reverseUsingStreamOfMethod(null);
|
||||
String reversedEmpty = ReverseStringExamples.reverseUsingStreamOfMethod(StringUtils.EMPTY);
|
||||
|
||||
assertEquals(STRING_INPUT_REVERSED, reversed);
|
||||
assertNull(reversedNull);
|
||||
assertEquals(StringUtils.EMPTY, reversedEmpty);
|
||||
}
|
||||
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.firstchardigit;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
|
||||
public class FirstCharDigit {
|
||||
|
||||
public static boolean checkUsingCharAtMethod(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char c = str.charAt(0);
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
||||
public static boolean checkUsingIsDigitMethod(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Character.isDigit(str.charAt(0));
|
||||
}
|
||||
|
||||
public static boolean checkUsingPatternClass(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Pattern.compile("^[0-9].*")
|
||||
.matcher(str)
|
||||
.matches();
|
||||
}
|
||||
|
||||
public static boolean checkUsingMatchesMethod(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return str.matches("^[0-9].*");
|
||||
}
|
||||
|
||||
public static boolean checkUsingCharMatcherInRangeMethod(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return CharMatcher.inRange('0', '9')
|
||||
.matches(str.charAt(0));
|
||||
}
|
||||
|
||||
public static boolean checkUsingCharMatcherForPredicateMethod(String str) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return CharMatcher.forPredicate(Character::isDigit)
|
||||
.matches(str.charAt(0));
|
||||
}
|
||||
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.firstchardigit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FirstCharDigitUnitTest {
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingCharAtMethod_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingCharAtMethod("12 years"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharAtMethod("years"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharAtMethod(""));
|
||||
assertFalse(FirstCharDigit.checkUsingCharAtMethod(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingIsDigitMethod_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingIsDigitMethod("10 cm"));
|
||||
assertFalse(FirstCharDigit.checkUsingIsDigitMethod("cm"));
|
||||
assertFalse(FirstCharDigit.checkUsingIsDigitMethod(""));
|
||||
assertFalse(FirstCharDigit.checkUsingIsDigitMethod(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingPatternClass_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingPatternClass("1 kg"));
|
||||
assertFalse(FirstCharDigit.checkUsingPatternClass("kg"));
|
||||
assertFalse(FirstCharDigit.checkUsingPatternClass(""));
|
||||
assertFalse(FirstCharDigit.checkUsingPatternClass(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingMatchesMethod_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingMatchesMethod("123"));
|
||||
assertFalse(FirstCharDigit.checkUsingMatchesMethod("ABC"));
|
||||
assertFalse(FirstCharDigit.checkUsingMatchesMethod(""));
|
||||
assertFalse(FirstCharDigit.checkUsingMatchesMethod(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingCharMatcherInRangeMethod_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingCharMatcherInRangeMethod("2023"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherInRangeMethod("abc"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherInRangeMethod(""));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherInRangeMethod(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenUsingCharMatcherForPredicateMethod_thenSuccess() {
|
||||
assertTrue(FirstCharDigit.checkUsingCharMatcherForPredicateMethod("100"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherForPredicateMethod("abdo"));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherForPredicateMethod(""));
|
||||
assertFalse(FirstCharDigit.checkUsingCharMatcherForPredicateMethod(null));
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.stringwithquotes;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class PrintQuotesAroundAStringUnitTest {
|
||||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
private final PrintStream originalOut = System.out;
|
||||
|
||||
@BeforeEach
|
||||
void replaceOut() {
|
||||
System.setOut(new PrintStream(outContent));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void restoreOut() {
|
||||
System.setOut(originalOut);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenWrappingAStringWithEscapedQuote_thenGetExpectedResult() {
|
||||
String theySay = "All Java programmers are cute!";
|
||||
String quoted = "\"" + theySay + "\"";
|
||||
|
||||
System.out.println(quoted);
|
||||
|
||||
//assertion
|
||||
String expected = "\"All Java programmers are cute!\"\n";
|
||||
assertEquals(expected, outContent.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCallingReplaceAll_thenGetExpectedResult() {
|
||||
String theySay = "Can you write Java code?";
|
||||
String quoted = theySay.replaceAll("^|$", "\"");
|
||||
|
||||
System.out.println(quoted);
|
||||
|
||||
//assertion
|
||||
String expected = "\"Can you write Java code?\"\n";
|
||||
assertEquals(expected, outContent.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenWrappingAStringWithQuoteChar_thenGetExpectedResult() {
|
||||
String weSay = "Yes, we can write beautiful Java codes!";
|
||||
String quoted = '"' + weSay + '"';
|
||||
System.out.println(quoted);
|
||||
|
||||
//assertion
|
||||
String expected = "\"Yes, we can write beautiful Java codes!\"\n";
|
||||
assertEquals(expected, outContent.toString());
|
||||
}
|
||||
}
|
||||
+8
-12
@@ -25,26 +25,22 @@ class HttpClientHeadersLiveTest {
|
||||
|
||||
@Test
|
||||
void whenClientUsesCustomUserAgent_thenCorrect() throws IOException {
|
||||
final CloseableHttpClient client = HttpClients.custom()
|
||||
.setUserAgent("Mozilla/5.0 Firefox/26.0")
|
||||
.build();
|
||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
||||
|
||||
try (CloseableHttpClient client = HttpClients.custom()
|
||||
.setUserAgent("Mozilla/5.0 Firefox/26.0")
|
||||
.build()) {
|
||||
|
||||
String response = client.execute(request, new BasicHttpClientResponseHandler());
|
||||
logger.info("Response -> {}", response);
|
||||
}
|
||||
String response = client.execute(request, new BasicHttpClientResponseHandler());
|
||||
logger.info("Response -> {}", response);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenRequestHasCustomUserAgent_thenCorrect() throws IOException {
|
||||
CloseableHttpClient client = HttpClients.createDefault();
|
||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
||||
request.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 Firefox/26.0");
|
||||
|
||||
try (CloseableHttpClient client = HttpClients.createDefault()) {
|
||||
String response = client.execute(request, new BasicHttpClientResponseHandler());
|
||||
logger.info("Response -> {}", response);
|
||||
}
|
||||
String response = client.execute(request, new BasicHttpClientResponseHandler());
|
||||
logger.info("Response -> {}", response);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -32,4 +32,8 @@
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<jackson.version>2.14.2</jackson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -48,7 +48,7 @@ public class JaxbIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unMashal() throws JAXBException, IOException {
|
||||
public void unmarshal() throws JAXBException, IOException {
|
||||
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||
String bookFile = this.getClass().getResource("/book.xml").getFile();
|
||||
Book unMarshallerbook = (Book) unmarshaller.unmarshal(new FileReader(bookFile));
|
||||
|
||||
@@ -68,7 +68,7 @@
|
||||
<networknt.json.schema.version>1.0.72</networknt.json.schema.version>
|
||||
<jsonb-api.version>1.0</jsonb-api.version>
|
||||
<yasson.version>1.0.1</yasson.version>
|
||||
<json.version>20211205</json.version>
|
||||
<json.version>20230227</json.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
<javax.version>1.1.2</javax.version>
|
||||
<json-unit-assertj.version>2.28.0</json-unit-assertj.version>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user