Skip to content

Commit aebb463

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents ac8742c + 3d51a0f commit aebb463

File tree

69 files changed

+1291
-48
lines changed

Some content is hidden

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

69 files changed

+1291
-48
lines changed

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)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package findItems;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.text.ParseException;
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
import org.junit.Test;
12+
13+
public class FindItemsBasedOnValues {
14+
15+
List<Employee> EmplList = new ArrayList<Employee>();
16+
List<Department> deptList = new ArrayList<Department>();
17+
18+
public static void main(String[] args) throws ParseException {
19+
FindItemsBasedOnValues findItems = new FindItemsBasedOnValues();
20+
findItems.givenDepartmentList_thenEmployeeListIsFilteredCorrectly();
21+
}
22+
23+
@Test
24+
public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly() {
25+
Integer expectedId = 1002;
26+
27+
populate(EmplList, deptList);
28+
29+
List<Employee> filteredList = EmplList.stream()
30+
.filter(empl -> deptList.stream()
31+
.anyMatch(dept -> dept.getDepartment()
32+
.equals("sales") && empl.getEmployeeId()
33+
.equals(dept.getEmployeeId())))
34+
.collect(Collectors.toList());
35+
36+
assertEquals(expectedId, filteredList.get(0)
37+
.getEmployeeId());
38+
}
39+
40+
private void populate(List<Employee> EmplList, List<Department> deptList) {
41+
Employee employee1 = new Employee(1001, "empl1");
42+
Employee employee2 = new Employee(1002, "empl2");
43+
Employee employee3 = new Employee(1003, "empl3");
44+
45+
Collections.addAll(EmplList, employee1, employee2, employee3);
46+
47+
Department department1 = new Department(1002, "sales");
48+
Department department2 = new Department(1003, "marketing");
49+
Department department3 = new Department(1004, "sales");
50+
51+
Collections.addAll(deptList, department1, department2, department3);
52+
}
53+
}
54+
55+
class Employee {
56+
Integer employeeId;
57+
String employeeName;
58+
59+
public Employee(Integer employeeId, String employeeName) {
60+
super();
61+
this.employeeId = employeeId;
62+
this.employeeName = employeeName;
63+
}
64+
65+
public Integer getEmployeeId() {
66+
return employeeId;
67+
}
68+
69+
public String getEmployeeName() {
70+
return employeeName;
71+
}
72+
73+
}
74+
75+
class Department {
76+
Integer employeeId;
77+
String department;
78+
79+
public Department(Integer employeeId, String department) {
80+
super();
81+
this.employeeId = employeeId;
82+
this.department = department;
83+
}
84+
85+
public Integer getEmployeeId() {
86+
return employeeId;
87+
}
88+
89+
public String getDepartment() {
90+
return department;
91+
}
92+
93+
}

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)