Java HostClassAndPropertyDouble-class And Method Code Example
Here is an example of using the HostClassAndPropertyDouble
class from the logback library:
import ch.qos.logback.core.PropertyDefinerBase;
public class MyPropertyDefiner extends PropertyDefinerBase {
@Override
public String getPropertyValue() {
return System.getProperty("java.version");
}
public static void main(String[] args) {
MyPropertyDefiner definer = new MyPropertyDefiner();
HostClassAndPropertyDouble host = new HostClassAndPropertyDouble(MyPropertyDefiner.class, "javaVersion", definer);
System.out.println(host.getAsDouble());
}
}
In this example, a custom PropertyDefiner class named MyPropertyDefiner is defined that inherits from PropertyDefinerBase and overrides the getPropertyValue() method to return the value of the java.version system property.
A HostClassAndPropertyDouble
object is then instantiated, passing in the class of MyPropertyDefiner, a string property name "javaVersion" and an instance of MyPropertyDefiner. The getAsDouble() method is called on this HostClassAndPropertyDouble object which will try to convert the returned property value in a double, and then prints the result.
It is worth noticing that this example is just a simple demonstration of the use of HostClassAndPropertyDouble, in a real-world scenario you would probably use the getPropertyValue method to retrieve some other types of properties, or the getAsDouble() method to perform further processing on the returned value.