Skip to content

Commit 510fe26

Browse files
committed
Merge branch 'master' of https://github.com/eugenp/tutorials into task/BAEL-9467
2 parents e72e7f7 + 90978bf commit 510fe26

File tree

190 files changed

+4513
-420
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

190 files changed

+4513
-420
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ jmeter/src/main/resources/*-JMeter.csv
6262
**/dist
6363
**/tmp
6464
**/out-tsc
65+
**/nbproject/
66+
**/nb-configuration.xml

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ In additional to Spring, the following technologies are in focus: `core Java`, `
2121
Building the project
2222
====================
2323
To do the full build, do: `mvn install -Pdefault -Dgib.enabled=false`
24+
25+
26+
Building a single module
27+
====================
28+
To build a specific module run the command: `mvn clean install -Dgib.enabled=false` in the module directory

algorithms/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@
2828
- [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points)
2929
- [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines)
3030
- [Check If a String Contains All The Letters of The Alphabet](https://www.baeldung.com/java-string-contains-all-letters)
31+
- [Round Up to the Nearest Hundred](https://www.baeldung.com/java-round-up-nearest-hundred)
32+
- [Merge Sort in Java](https://www.baeldung.com/java-merge-sort)
33+
- [Calculate Percentage in Java](https://www.baeldung.com/java-calculate-percentage)

algorithms/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<artifactId>commons-math3</artifactId>
1818
<version>${commons-math3.version}</version>
1919
</dependency>
20+
<dependency>
21+
<groupId>commons-codec</groupId>
22+
<artifactId>commons-codec</artifactId>
23+
<version>${commons-codec.version}</version>
24+
</dependency>
2025
<dependency>
2126
<groupId>org.projectlombok</groupId>
2227
<artifactId>lombok</artifactId>
@@ -85,6 +90,7 @@
8590
<io.jenetics.version>3.7.0</io.jenetics.version>
8691
<org.jgrapht.core.version>1.0.1</org.jgrapht.core.version>
8792
<org.assertj.core.version>3.9.0</org.assertj.core.version>
93+
<commons-codec.version>1.11</commons-codec.version>
8894
</properties>
8995

9096
</project>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.baeldung.algorithms.conversion;
2+
3+
import java.math.BigInteger;
4+
5+
import javax.xml.bind.DatatypeConverter;
6+
7+
import org.apache.commons.codec.DecoderException;
8+
import org.apache.commons.codec.binary.Hex;
9+
10+
import com.google.common.io.BaseEncoding;
11+
12+
public class HexStringConverter {
13+
14+
/**
15+
* Create a byte Array from String of hexadecimal digits using Character conversion
16+
* @param hexString - Hexadecimal digits as String
17+
* @return Desired byte Array
18+
*/
19+
public byte[] decodeHexString(String hexString) {
20+
if (hexString.length() % 2 == 1) {
21+
throw new IllegalArgumentException("Invalid hexadecimal String supplied.");
22+
}
23+
byte[] bytes = new byte[hexString.length() / 2];
24+
25+
for (int i = 0; i < hexString.length(); i += 2) {
26+
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
27+
}
28+
return bytes;
29+
}
30+
31+
/**
32+
* Create a String of hexadecimal digits from a byte Array using Character conversion
33+
* @param byteArray - The byte Array
34+
* @return Desired String of hexadecimal digits in lower case
35+
*/
36+
public String encodeHexString(byte[] byteArray) {
37+
StringBuffer hexStringBuffer = new StringBuffer();
38+
for (int i = 0; i < byteArray.length; i++) {
39+
hexStringBuffer.append(byteToHex(byteArray[i]));
40+
}
41+
return hexStringBuffer.toString();
42+
}
43+
44+
public String byteToHex(byte num) {
45+
char[] hexDigits = new char[2];
46+
hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
47+
hexDigits[1] = Character.forDigit((num & 0xF), 16);
48+
return new String(hexDigits);
49+
}
50+
51+
public byte hexToByte(String hexString) {
52+
int firstDigit = toDigit(hexString.charAt(0));
53+
int secondDigit = toDigit(hexString.charAt(1));
54+
return (byte) ((firstDigit << 4) + secondDigit);
55+
}
56+
57+
private int toDigit(char hexChar) {
58+
int digit = Character.digit(hexChar, 16);
59+
if(digit == -1) {
60+
throw new IllegalArgumentException("Invalid Hexadecimal Character: "+ hexChar);
61+
}
62+
return digit;
63+
}
64+
65+
public String encodeUsingBigIntegerToString(byte[] bytes) {
66+
BigInteger bigInteger = new BigInteger(1, bytes);
67+
return bigInteger.toString(16);
68+
}
69+
70+
public String encodeUsingBigIntegerStringFormat(byte[] bytes) {
71+
BigInteger bigInteger = new BigInteger(1, bytes);
72+
return String.format("%0" + (bytes.length << 1) + "x", bigInteger);
73+
}
74+
75+
public byte[] decodeUsingBigInteger(String hexString) {
76+
byte[] byteArray = new BigInteger(hexString, 16).toByteArray();
77+
if (byteArray[0] == 0) {
78+
byte[] output = new byte[byteArray.length - 1];
79+
System.arraycopy(byteArray, 1, output, 0, output.length);
80+
return output;
81+
}
82+
return byteArray;
83+
}
84+
85+
public String encodeUsingDataTypeConverter(byte[] bytes) {
86+
return DatatypeConverter.printHexBinary(bytes);
87+
}
88+
89+
public byte[] decodeUsingDataTypeConverter(String hexString) {
90+
return DatatypeConverter.parseHexBinary(hexString);
91+
}
92+
93+
public String encodeUsingApacheCommons(byte[] bytes) throws DecoderException {
94+
return Hex.encodeHexString(bytes);
95+
}
96+
97+
public byte[] decodeUsingApacheCommons(String hexString) throws DecoderException {
98+
return Hex.decodeHex(hexString);
99+
}
100+
101+
public String encodeUsingGuava(byte[] bytes) {
102+
return BaseEncoding.base16()
103+
.encode(bytes);
104+
}
105+
106+
public byte[] decodeUsingGuava(String hexString) {
107+
return BaseEncoding.base16()
108+
.decode(hexString.toUpperCase());
109+
}
110+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.baeldung.algorithms.conversion;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertThat;
6+
7+
import org.apache.commons.codec.DecoderException;
8+
import org.hamcrest.text.IsEqualIgnoringCase;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import com.baeldung.algorithms.conversion.HexStringConverter;
13+
14+
public class ByteArrayConverterUnitTest {
15+
16+
private HexStringConverter hexStringConverter;
17+
18+
@Before
19+
public void setup() {
20+
hexStringConverter = new HexStringConverter();
21+
}
22+
23+
@Test
24+
public void shouldEncodeByteArrayToHexStringUsingBigIntegerToString() {
25+
byte[] bytes = getSampleBytes();
26+
String hexString = getSampleHexString();
27+
if(hexString.charAt(0) == '0') {
28+
hexString = hexString.substring(1);
29+
}
30+
String output = hexStringConverter.encodeUsingBigIntegerToString(bytes);
31+
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
32+
}
33+
34+
@Test
35+
public void shouldEncodeByteArrayToHexStringUsingBigIntegerStringFormat() {
36+
byte[] bytes = getSampleBytes();
37+
String hexString = getSampleHexString();
38+
String output = hexStringConverter.encodeUsingBigIntegerStringFormat(bytes);
39+
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
40+
}
41+
42+
@Test
43+
public void shouldDecodeHexStringToByteArrayUsingBigInteger() {
44+
byte[] bytes = getSampleBytes();
45+
String hexString = getSampleHexString();
46+
byte[] output = hexStringConverter.decodeUsingBigInteger(hexString);
47+
assertArrayEquals(bytes, output);
48+
}
49+
50+
@Test
51+
public void shouldEncodeByteArrayToHexStringUsingCharacterConversion() {
52+
byte[] bytes = getSampleBytes();
53+
String hexString = getSampleHexString();
54+
String output = hexStringConverter.encodeHexString(bytes);
55+
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
56+
}
57+
58+
@Test
59+
public void shouldDecodeHexStringToByteArrayUsingCharacterConversion() {
60+
byte[] bytes = getSampleBytes();
61+
String hexString = getSampleHexString();
62+
byte[] output = hexStringConverter.decodeHexString(hexString);
63+
assertArrayEquals(bytes, output);
64+
}
65+
66+
@Test(expected=IllegalArgumentException.class)
67+
public void shouldDecodeHexToByteWithInvalidHexCharacter() {
68+
hexStringConverter.hexToByte("fg");
69+
}
70+
71+
@Test
72+
public void shouldEncodeByteArrayToHexStringDataTypeConverter() {
73+
byte[] bytes = getSampleBytes();
74+
String hexString = getSampleHexString();
75+
String output = hexStringConverter.encodeUsingDataTypeConverter(bytes);
76+
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
77+
}
78+
79+
@Test
80+
public void shouldDecodeHexStringToByteArrayUsingDataTypeConverter() {
81+
byte[] bytes = getSampleBytes();
82+
String hexString = getSampleHexString();
83+
byte[] output = hexStringConverter.decodeUsingDataTypeConverter(hexString);
84+
assertArrayEquals(bytes, output);
85+
}
86+
87+
@Test
88+
public void shouldEncodeByteArrayToHexStringUsingGuava() {
89+
byte[] bytes = getSampleBytes();
90+
String hexString = getSampleHexString();
91+
String output = hexStringConverter.encodeUsingGuava(bytes);
92+
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
93+
}
94+
95+
@Test
96+
public void shouldDecodeHexStringToByteArrayUsingGuava() {
97+
byte[] bytes = getSampleBytes();
98+
String hexString = getSampleHexString();
99+
byte[] output = hexStringConverter.decodeUsingGuava(hexString);
100+
assertArrayEquals(bytes, output);
101+
}
102+
103+
@Test
104+
public void shouldEncodeByteArrayToHexStringUsingApacheCommons() throws DecoderException {
105+
byte[] bytes = getSampleBytes();
106+
String hexString = getSampleHexString();
107+
String output = hexStringConverter.encodeUsingApacheCommons(bytes);
108+
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
109+
}
110+
111+
@Test
112+
public void shouldDecodeHexStringToByteArrayUsingApacheCommons() throws DecoderException {
113+
byte[] bytes = getSampleBytes();
114+
String hexString = getSampleHexString();
115+
byte[] output = hexStringConverter.decodeUsingApacheCommons(hexString);
116+
assertArrayEquals(bytes, output);
117+
}
118+
119+
private String getSampleHexString() {
120+
return "0af50c0e2d10";
121+
}
122+
123+
private byte[] getSampleBytes() {
124+
return new byte[] { 10, -11, 12, 14, 45, 16 };
125+
}
126+
127+
}

aws/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
- [Integration Testing with a Local DynamoDB Instance](http://www.baeldung.com/dynamodb-local-integration-tests)
1010
- [Using the JetS3t Java Client With Amazon S3](http://www.baeldung.com/jets3t-amazon-s3)
1111
- [Managing Amazon SQS Queues in Java](http://www.baeldung.com/aws-queues-java)
12-
12+
- [Guide to AWS Aurora RDS with Java](https://www.baeldung.com/aws-aurora-rds-java)

core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

core-java-9/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@
2626
- [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range)
2727
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
2828
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
29+
- [Guide to java.lang.Process API](https://www.baeldung.com/java-process-api)

core-java-collections/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@
5252
- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
5353
- [Get the Key for a Value from a Java Map](https://www.baeldung.com/java-map-key-from-value)
5454
- [Time Complexity of Java Collections](https://www.baeldung.com/java-collections-complexity)
55+
- [Sort a HashMap in Java](https://www.baeldung.com/java-hashmap-sort)
56+
- [Finding the Highest Value in a Java Map](https://www.baeldung.com/java-find-map-max)
57+
- [Operating on and Removing an Item from Stream](https://www.baeldung.com/java-use-remove-item-stream)

0 commit comments

Comments
 (0)