2023-01-20 18:52:57 +01:00
|
|
|
package io.gitlab.jfronny.muscript.data.dynamic;
|
2022-06-07 12:19:58 +02:00
|
|
|
|
2022-06-29 15:41:44 +02:00
|
|
|
import io.gitlab.jfronny.commons.StringFormatter;
|
2022-06-07 12:19:58 +02:00
|
|
|
|
|
|
|
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");
|
2022-06-07 12:19:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default DNumber asNumber() {
|
|
|
|
if (this instanceof DNumber number) return number;
|
2022-06-13 13:31:54 +02:00
|
|
|
else throw new DynamicTypeConversionException("number");
|
2022-06-07 12:19:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2022-06-07 12:19:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default DList asList() {
|
|
|
|
if (this instanceof DList list) return list;
|
2022-06-13 13:31:54 +02:00
|
|
|
else throw new DynamicTypeConversionException("list");
|
2022-06-07 12:19:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default DCallable asCallable() {
|
|
|
|
if (this instanceof DCallable callable) return callable;
|
2022-06-13 13:31:54 +02:00
|
|
|
else throw new DynamicTypeConversionException("callable");
|
2022-06-07 12:19:58 +02:00
|
|
|
}
|
|
|
|
}
|