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

org.comroid.api.Specifiable Maven / Gradle / Ivy

The newest version!
package org.comroid.api;

import java.util.Optional;
import java.util.function.Supplier;

public interface Specifiable> extends SelfDeclared {
    default  R as(Class type, Supplier message) throws AssertionError {
        return as(type).orElseThrow(() -> new AssertionError(message.get()));
    }

    default  Optional as(Class type) {
        if (!isType(type)) {
            return Optional.empty();
        }

        return Optional.of(type.cast(self()));
    }

    default boolean isType(Class type) {
        return type.isInstance(self());
    }
}