All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.datakernel.common.parse.ParserFunction Maven / Gradle / Ivy

package io.datakernel.common.parse;

import io.datakernel.common.exception.UncheckedException;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;

@FunctionalInterface
public interface ParserFunction {
	R parse(T value) throws ParseException;

	static  Function asFunction(ParserFunction fn) {
		return item -> {
			try {
				return fn.parse(item);
			} catch (ParseException e) {
				throw new UncheckedException(e);
			}
		};
	}

	static  ParserFunction of(Function fn) {
		return value -> {
			try {
				return fn.apply(value);
			} catch (Exception e) {
				throw new ParseException(e);
			}
		};
	}

	default R parseOrDefault(@Nullable T value, R defaultResult) {
		try {
			if (value != null) {
				return parse(value);
			}
		} catch (ParseException ignore) {}

		return defaultResult;
	}

	default  ParserFunction andThen(ParserFunction after) {
		return (T t) -> after.parse(parse(t));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy