Adds some methods for parsing Strings as other objects without resulting in exceptions. Adds a class for representing all possible stream info tags. Makes streams parse data themselves, after receiving all tags set for the stream. Changes Java version to Java 16
64 lines
1.8 KiB
Java
64 lines
1.8 KiB
Java
package net.knarcraft.ffmpegconverter.utility;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* A helper class for parsing values without causing errors
|
|
*/
|
|
public final class ValueParsingHelper {
|
|
|
|
private ValueParsingHelper() {
|
|
|
|
}
|
|
|
|
/**
|
|
* Parses an integer from a string
|
|
*
|
|
* @param input <p>The input given</p>
|
|
* @param defaultValue <p>The default value to return if no integer could be parsed</p>
|
|
* @return <p>The parsed integer, or the given default value</p>
|
|
*/
|
|
public static int parseInt(@Nullable String input, int defaultValue) {
|
|
if (input == null) {
|
|
return defaultValue;
|
|
}
|
|
|
|
try {
|
|
return Integer.parseInt(input);
|
|
} catch (NumberFormatException exception) {
|
|
return defaultValue;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parses a string
|
|
*
|
|
* @param input <p>The input string</p>
|
|
* @param defaultValue <p>The default value to use if the string is null</p>
|
|
* @return <p>The input string, or the default value</p>
|
|
*/
|
|
@NotNull
|
|
public static String parseString(@Nullable String input, @NotNull String defaultValue) {
|
|
return Objects.requireNonNullElse(input, defaultValue);
|
|
}
|
|
|
|
/**
|
|
* Parses a boolean
|
|
*
|
|
* @param input <p>The input string</p>
|
|
* @param defaultValue <p>The default value to use if the string is null</p>
|
|
* @return <p>The parsed boolean, or the default value</p>
|
|
*/
|
|
public static boolean parseBoolean(@Nullable String input, boolean defaultValue) {
|
|
if (input == null || input.isEmpty()) {
|
|
return defaultValue;
|
|
} else {
|
|
return Boolean.parseBoolean(input);
|
|
}
|
|
}
|
|
|
|
}
|