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

com.xlrit.gears.engine.snel.Result Maven / Gradle / Ivy

package com.xlrit.gears.engine.snel;

import java.math.BigDecimal;
import java.util.function.Function;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.xlrit.gears.engine.meta.BasicTypes;
import com.xlrit.gears.engine.meta.PrintOptions;
import com.xlrit.gears.engine.meta.TypeInfo;

/**
 * @param type null for unknown
 */
public record Result(TypeInfo type, Object value) {
	public static final Result NULL = Result.untyped(null);

	public boolean hasValue() {
		return value != null;
	}

	public boolean isNumber() {
		return type == BasicTypes.INTEGER || type == BasicTypes.DECIMAL;
	}

	public  Result map(Function function) {
		return new Result(type, function.apply((T) value));
	}

	public static Result untyped(Object value) {
		return new Result(null, value);
	}

	public static Result typed(TypeInfo type, Object value) {
		return new Result(type, value);
	}

	public static Result booleanResult(Boolean value) {
		return new Result(BasicTypes.BOOLEAN, value);
	}

	public static Result integerResult(Long value) {
		return new Result(BasicTypes.INTEGER, value);
	}

	public static Result decimalResult(BigDecimal value) {
		return new Result(BasicTypes.DECIMAL, value);
	}

	public static Result textResult(String value) {
		return new Result(BasicTypes.TEXT, value);
	}

	public Boolean asBoolean() {
		Preconditions.checkState(type == BasicTypes.BOOLEAN);
		return (Boolean) value;
	}

	public Long asInteger() {
		Preconditions.checkState(type == BasicTypes.INTEGER);
		return (Long) value;
	}

	public Number asNumber() {
		Preconditions.checkState(isNumber());
		return (Number) value;
	}

	public String asText() {
		Preconditions.checkState(type == BasicTypes.TEXT);
		return (String) value;
	}

	public JsonNode toJson(ObjectMapper objectMapper, PrintOptions printOptions) {
		return type.serialize(value, objectMapper, printOptions);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy