class CastUtils {

    @Nullable
    public static <T, V1, V2> T cast( @Nullable Object value, @Nonnull Class<V1> c1, @Nonnull Function<V1, T> f1, @Nonnull Class<V2> c2, @Nonnull Function<V2, T> f2 ) throws ClassCastException
    {
        if ( value == null )
        {
            return null;
        }
        if ( c1.isInstance( value ) )
        {
            return f1.apply( c1.cast( value ) );
        }
        if ( c2.isInstance( value ) )
        {
            return f2.apply( c2.cast( value ) );
        }
        throw new ClassCastException( "Value of type " + value.getClass().getName() + " cannot be handled." );
    }

    private  CastUtils();

}