java-commons/muscript/src/main/java/io/gitlab/jfronny/muscript/data/dynamic/Dynamic.java

38 lines
1.1 KiB
Java
Raw Normal View History

2023-01-20 18:52:57 +01:00
package io.gitlab.jfronny.muscript.data.dynamic;
import io.gitlab.jfronny.commons.StringFormatter;
public interface Dynamic<T> {
T getValue();
default DBool asBool() {
if (this instanceof DBool bool) return bool;
2022-06-13 13:31:54 +02:00
else throw new DynamicTypeConversionException("bool");
}
default DNumber asNumber() {
if (this instanceof DNumber number) return number;
2022-06-13 13:31:54 +02:00
else throw new DynamicTypeConversionException("number");
}
default DString asString() {
if (this instanceof DString string) return string;
else return DFinal.of(StringFormatter.toString(getValue()));
}
default DObject asObject() {
if (this instanceof DObject object) return object;
2022-06-13 13:31:54 +02:00
else throw new DynamicTypeConversionException("object");
}
default DList asList() {
if (this instanceof DList list) return list;
2022-06-13 13:31:54 +02:00
else throw new DynamicTypeConversionException("list");
}
default DCallable asCallable() {
if (this instanceof DCallable callable) return callable;
2022-06-13 13:31:54 +02:00
else throw new DynamicTypeConversionException("callable");
}
}