[JAVA-31850] Split-or-move-core-java-lang-6 (#16484)

* [JAVA-31850] Split-or-move-core-java-lang-6

* [JAVA-31850] Split-or-move-core-java-lang-6: Fix missing dependency version of core-java-lang-7
This commit is contained in:
vunamtien
2024-04-27 04:51:19 +07:00
committed by GitHub
parent fe759e41e4
commit b17f9029a1
7 changed files with 13 additions and 6 deletions
@@ -6,11 +6,9 @@ This module contains articles about core features in the Java language
- [Convert One Enum to Another Enum in Java](https://www.baeldung.com/java-convert-enums)
- [What Is the Maximum Depth of the Java Call Stack?](https://www.baeldung.com/java-call-stack-max-depth)
- [Get a Random Element From a Set in Java](https://www.baeldung.com/java-set-draw-sample)
- [Stop Executing Further Code in Java](https://www.baeldung.com/java-stop-running-code)
- [Using the Apache Commons Lang 3 for Comparing Objects in Java](https://www.baeldung.com/java-apache-commons-lang-3-compare-objects)
- [Return First Non-null Value in Java](https://www.baeldung.com/java-first-non-null)
- [Compress and Uncompress Byte Array Using Deflater/Inflater](https://www.baeldung.com/java-compress-uncompress-byte-array)
- [Static Final Variables in Java](https://www.baeldung.com/java-static-final-variables)
- [What Is the Error: “Non-static method cannot be referenced from a static context”?](https://www.baeldung.com/java-non-static-method-cannot-be-referenced-from-a-static-context)
- [Recursively Sum the Integers in an Array](https://www.baeldung.com/java-recursive-sum-integer-array)
@@ -1,75 +0,0 @@
package com.baeldung.compressbytes;
import java.io.ByteArrayOutputStream;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class CompressByteArrayUtil {
public static byte[] compress(byte[] input) {
Deflater deflater = new Deflater();
deflater.setInput(input);
deflater.finish();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int compressedSize = deflater.deflate(buffer);
outputStream.write(buffer, 0, compressedSize);
}
return outputStream.toByteArray();
}
public static byte[] compressWithCustomLevel(byte[] input, int level) {
Deflater deflater = new Deflater();
deflater.setInput(input);
deflater.setLevel(level);
deflater.finish();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int compressedSize = deflater.deflate(buffer);
outputStream.write(buffer, 0, compressedSize);
}
return outputStream.toByteArray();
}
public static byte[] decompress(byte[] input) throws DataFormatException {
Inflater inflater = new Inflater();
inflater.setInput(input);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int decompressedSize = inflater.inflate(buffer);
outputStream.write(buffer, 0, decompressedSize);
}
return outputStream.toByteArray();
}
public static void main(String[] args) throws Exception {
String inputString = "Baeldung helps developers explore the Java ecosystem and simply be better engineers. " +
"We publish to-the-point guides and courses, with a strong focus on building web applications, Spring, " +
"Spring Security, and RESTful APIs";
byte[] input = inputString.getBytes();
// Compression
byte[] compressedData = compress(input);
// Decompression
byte[] decompressedData = decompress(compressedData);
System.out.println("Original: " + input.length + " bytes");
System.out.println("Compressed: " + compressedData.length + " bytes");
System.out.println("Decompressed: " + decompressedData.length + " bytes");
}
}
@@ -1,50 +0,0 @@
package com.baeldung.randominset;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
public class RandomInSetUtil {
public static <T> T getByRandomClass(Set<T> set) {
if (set == null || set.isEmpty()) {
throw new IllegalArgumentException("The Set cannot be empty.");
}
int randomIndex = new Random().nextInt(set.size());
int i = 0;
for (T element : set) {
if (i == randomIndex) {
return element;
}
i++;
}
throw new IllegalStateException("Something went wrong while picking a random element.");
}
public static <T> T getByThreadLocalRandom(Set<T> set) {
if (set == null || set.isEmpty()) {
throw new IllegalArgumentException("The Set cannot be empty.");
}
int randomIndex = ThreadLocalRandom.current().nextInt(set.size());
int i = 0;
for (T element : set) {
if (i == randomIndex) {
return element;
}
i++;
}
throw new IllegalStateException("Something went wrong while picking a random element.");
}
public static void main(String[] args) {
Set<String> animals = new HashSet<>();
animals.add("Lion");
animals.add("Elephant");
animals.add("Giraffe");
String randomAnimal = getByThreadLocalRandom(animals);
System.out.println("Randomly picked animal: " + randomAnimal);
}
}
@@ -1,39 +0,0 @@
package com.baeldung.compressbytes;
import org.junit.jupiter.api.Test;
import java.util.zip.DataFormatException;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CompressByteArrayUnitTest {
private static final String INPUT_STRING = "Building a REST API is not a trivial task from the high-level RESTful " +
"constraints down to the nitty-gritty of making everything work and work well." +
"Spring has made REST a first-class citizen and the platform has been maturing in leaps and bounds." +
"With this guide, my aim is to organize the mountains of information that are available on the subject and " +
"guide you through properly building an API." +
"The guide starts with the basics bootstrapping the REST API, the Spring MVC Configuration, and basic customization.";
@Test
void givenInputString_whenCompressWithDefaultLevel_thenDecompressWithSameSize() throws DataFormatException {
byte[] input = INPUT_STRING.getBytes();
byte[] compressedData = CompressByteArrayUtil.compress(input);
byte[] decompressedData = CompressByteArrayUtil.decompress(compressedData);
System.out.println("Original: " + input.length + " bytes");
System.out.println("Compressed: " + compressedData.length + " bytes");
System.out.println("Decompressed: " + decompressedData.length + " bytes");
assertEquals(input.length, decompressedData.length);
}
@Test
void givenInputString_whenCompressWithCustomLevel_thenDecompressWithSameSize() throws DataFormatException {
byte[] input = INPUT_STRING.getBytes();
byte[] compressedData = CompressByteArrayUtil.compressWithCustomLevel(input, 1);
byte[] decompressedData = CompressByteArrayUtil.decompress(compressedData);
System.out.println("Original: " + input.length + " bytes");
System.out.println("Compressed: " + compressedData.length + " bytes");
System.out.println("Decompressed: " + decompressedData.length + " bytes");
assertEquals(input.length, decompressedData.length);
}
}