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

org.aksw.facete.v3.api.Castable Maven / Gradle / Ivy

There is a newer version: 5.2.0-1
Show newest version
package org.aksw.facete.v3.api;

import java.util.Optional;

import org.aksw.commons.util.obj.ObjectUtils;

/**
 * A convenience interface (or rather trait) to perform inline casts
 *
 *
 * @author Claus Stadler, Dec 28, 2018
 *
 */
public interface Castable {
    default  T as(Class clazz) {
        T result = ObjectUtils.castAs(clazz, this);
        return result;
    }

    default  Optional tryAs(Class clazz) {
        Optional result = ObjectUtils.tryCastAs(clazz, this);
        return result;
    }

    default boolean canAs(Class clazz) {
        boolean result = ObjectUtils.canCastAs(clazz, this);
        return result;
    }

    /*
     * public static  T castAs(Class clazz, Object o) {
     *
     * @SuppressWarnings("unchecked") T result = (T)o; return result; }
     */
//	public static boolean canCastAs(Class clazz, Object o) {
//		boolean result = o == null ? true : clazz.isAssignableFrom(o.getClass());
//		return result;
//	}
//
//	public static  Optional tryCastAs(Class clazz, Object o) {
//		boolean canCastAs = canCastAs(clazz, o);
//		Optional result = canCastAs ? Optional.of(castAs(clazz,o)) : Optional.empty();
//		return result;
//	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy